In this method, we rewrite the URL and append the information to the URL to keep the track of the values. URL Rewriting can be used to maintain the session. We can maintain session using URL rewriting.
There are several methods to maintain session between client and server, see all types of method here
Lets see an example
<%--Document:index Created on:16Aug,2012,4:22:58PM Author:Abhishek--%>
User Authorization is the most common way to maintain session between client and server.
A user has to sign-in into the application using their credentials. Once the user submits the form by filling the correct credentials a session gets created on the server that will be used during the entire user session.
Use these credentials to make this form work:
Once you run the code, you will see a cookie has been created with name “JSESSIONID”. This JSESSIONID is assigned by the apache tomcat server to the client or browser.
Expiration of this JSESSIONID can be configured in web.xml file.
A cookie will be set, when you click on the ‘Cookies’ link. You can give any name to the link, Apple, for example.
This cookie is created using the code above.
In this port of Playing with Session in Java, we will explore some of the concepts of session and try to understand what session is:
What is session?
Session is a concept that is use to maintain connection between client and server.
In easy words its client and server interacts using session. Server recognizes client by validating clients session information.
Why session is needed?
As HTTP is a stateless protocol so a method/way was needed to maintain session or to know recognize the client.
Sessions do not last forever. A session either expires automatically, after a set time of inactivity (for the Java Web Server the default is 30 minutes), or manually by explicitly invalidating using a servlet. When a session expires (or is invalidated), the HttpSession object and the data values it contains are removed from the system.
How to maintain session?
2. User Authorization
3. URL rewriting
4. Hidden Fields
5. Session tracking API
We will discuss of these methods with example, but before that we would like to clear some basic precision’s about session
* You don’t need login/logout mechanisms in order to have sessions.
* In java servlets, HTTP sessions are tracked using two mechanisms, HTTP cookie (the most commonly used) or URL rewriting (to support browser without cookies or with disabled cookies). Using only cookies is simple, you don’t have to do anything special. For URL re-writing, you need to modify all URLs pointing back to your servlets/filters.
* Each time you call request.getSession(true), the HttpRequest object will be inspected in order to find a session ID encoded either in a cookie OR/AND in the URL path parameter (what’s following a semi-colon). If the session ID cannot be found, a new session will be created by the servlet container (i.e. the server).
* The session ID is added to the response as a Cookie. If you want to support URL re-writing also, the links in your HTML documents should be modified using the response.encodeURL() method. Calling request.getSession(false) or simply request.getSession() will return null in the event the session ID is not found or the session ID refers to an invalid session.
* There is a single HTTP session by visit, as Java session cookies are not stored permanently in the browser. So sessions object are not shared between clients. Each user has his own private session.
Sessions are destroyed automatically if not used for a given time. The time-out value can be configured in the web.xml file.
* A given session can be explicitly invalidated using the invalidate() method.
When people are talking about JSESSIONID, they are referring to the standard name of the HTTP cookie used to do session-tracking in Java.
Now lets see the methods in which session can be maintained: 1. Cookies: Maintaining session using cookies is the simplest method, you don’t have to do anything special. In java when you opens a website or webpage a session is assign by the browser and these cookies has some expiration time.
See the below screen shot. Here a simple jsp page is displayed and Apache tomcat itself gives it a session id.
You can also create set your own cookie if you want to save anything in browser’s cookie, just like shopping carts do to save the products visited by the user. This helps to keep track the products visited by the user even the user not is logged in.
3. URL rewriting: In this method you need to modify all URLs pointing back to your servlets/filters.
View URL Rewriting Example
4. Hidden Fields
This method is use when there is a need to send some values with the form on submit.
The value set in the hidden field can not be seen on the GUI but present as hidden values.
Syntax of representing hidden value in the form:
View Hidden Field Example
5. Session tracking API
NOTE: This project is on Google drive so, to download this project you first need to add this to your Google drive then you will get the option to download it.
Collection introduction. Lets Start with History:
Java did not have Collection framework in earlier versions. Instead it defined classes and a interface to store objects.
These older classes are known are legacy classes
What is Collection?
Collection is a set of fourteen collection interfaces and classes(implementations of these interfaces, and algorithms to manipulate them).
All the collection classes and interface are contained in java.util package.
You can use these classes according to the need.
Collections : A utility class that is a member of the Java Collections Framework.
Collection : The root interface of Java Collections Framework/hierarchy.
This means that every single class implementing Collection in any form is part of the Java Collections Framework.
Q: One question arise in mind that may confuse, It is collection framework or collections framework?
A: Framework name is collections and the interface name is Collection. Do not get confused.
The collections framework consists of:
Collection Interfaces – Represent different types of collections, such as sets, lists and maps. These interfaces form the basis of the framework.
General-purpose Implementations – Primary implementations of the collection interfaces.
Legacy Implementations – The collection classes from earlier releases, Vector and Hashtable, have been retrofitted to implement the collection interfaces.
Special-purpose Implementations – Implementations designed for use in special situations. These implementations display nonstandard performance characteristics, usage restrictions, or behavior.
Concurrent Implementations – Implementations designed for highly concurrent use.
Wrapper Implementations – Add functionality, such as synchronization, to other implementations.
Convenience Implementations – High-performance “mini-implementations” of the collection interfaces.
Abstract Implementations – Partial implementations of the collection interfaces to facilitate custom implementations.
Algorithms – Static methods that perform useful functions on collections, such as sorting a list.
Infrastructure – Interfaces that provide essential support for the collection interfaces.
Array Utilities – Utility functions for arrays of primitives and reference objects. Not, strictly speaking, a part of the Collections Framework, this functionality was added to the Java platform at the same time and relies on some of the same infrastructure.
Q: Map is a collection or not or Why does Map not extend Collection interface?
A: Collection assume elements of one value. Map assumes entries of key/value pairs. They could have been engineered to re-use the same common interface however some methods they implement are incompatible e.g.
Collection.remove(Object) – removes an element.
Map.remove(Object) – removes by key, not by entry.
You could model a Map as a collection of entries, which is what Map.entrySet() does.
There are some methods in common; size(), isEmpty(), clear(), putAll/addAll() but these are unlikely to have much value as a stand alone interface. (Again Map.entrySet() can be used instead)