What are the different methods of session management in servlets?

0 votes
I am just starting with Advanced Java. Could you please help me with session management techniques
Feb 17 in Java by Neha
• 6,280 points
1,036 views

1 answer to this question.

0 votes

Session is a conversational state between client and server and it can consists of multiple request and response between client and server. Since HTTP and Web Server both are stateless, the only way to maintain a session is when some unique information about the session (session id) is passed between server and client in every request and response.

Some of the common ways of session management in servlets are:

  1. User Authentication
  2. HTML Hidden Field
  3. Cookies
  4. URL Rewriting
  5. Session Management API
answered Feb 17 by Frankie
• 9,810 points
@frankie can you explain about these?

Cookie 

cookie is a small piece of information that is persisted between the multiple client requests.

A cookie has a name, a single value, and optional attributes such as a comment, path and domain qualifiers, a maximum age, and a version number.

URL Rewriting

n URL rewriting, we append a token or identifier to the URL of the next Servlet or the next resource. We can send parameter name/value pairs using the following format:

url?name1=value1&name2=value2&??

A name and a value is separated using an equal = sign, a parameter name/value pair is separated from another parameter using the ampersand(&). When the user clicks the hyperlink, the parameter name/value pairs will be passed to the server. From a Servlet, we can use getParameter() method to obtain a parameter value.

HTML Hidden Field

In case of Hidden Form Field a hidden (invisible) textfield is used for maintaining the state of a user.

In such a case, we store the information in the hidden field and get it from another servlet. This approach is better if we have to submit form in all the pages and we don't want to depend on the browser.

Let's see the code to store value in hidden field.

<input type="hidden" name="uname" value="Edureka">  

Here, uname is the hidden field name and Edureka is the hidden field value.

Session Management API

Session Management API is built on top of the above methods for session tracking. Some of the major disadvantages of all the above methods are:

  • Most of the time we don’t want to only track the session, we have to store some data into the session that we can use in future requests. This will require a lot of effort if we try to implement this.
  • All the above methods are not complete in themselves, all of them won’t work in a particular scenario. So we need a solution that can utilize these methods of session tracking to provide session management in all cases.

That’s why we need Session Management API and J2EE Servlet technology comes with session management API that we can use.

Related Questions In Java

0 votes
1 answer

What are the different ways of comparing Strings in Java?

The different ways of comparing string in ...READ MORE

answered Mar 5 in Java by Wasim
62 views
0 votes
1 answer

What are the different types of constructor in StringBuilder class?

There are different types of StringBuilder constructor ...READ MORE

answered Mar 5 in Java by Mariyam
60 views
0 votes
1 answer

What is the concept of Immutability for strings in Java ? Why are strings immutable ?

According to Effective Java, chapter 4, page 73, ...READ MORE

answered May 11, 2018 in Java by Rishabh
• 3,540 points
184 views
0 votes
2 answers

What are all the different ways to create an object in Java?

There are different ways you could do this ...READ MORE

answered Aug 19 in Java by Sirajul
• 37,930 points
93 views
+5 votes
3 answers

How to execute a python file with few arguments in java?

You can use Java Runtime.exec() to run python script, ...READ MORE

answered Mar 27, 2018 in Java by DragonLord999
• 8,380 points

edited Nov 6, 2018 by Omkar 10,262 views
0 votes
1 answer

How to handle drop downs using Selenium WebDriver in Java

First, find an XPath which will return ...READ MORE

answered Mar 27, 2018 in Selenium by nsv999
• 5,110 points
2,290 views
0 votes
1 answer

What are the differences between getText() and getAttribute() functions in Selenium WebDriver?

See, both are used to retrieve something ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,110 points
5,964 views
0 votes
1 answer

Selenium JARS(Java) missing from downloadable link

Nothing to worry about here. In the ...READ MORE

answered Apr 5, 2018 in Selenium by nsv999
• 5,110 points
702 views
0 votes
1 answer

What are the different types of database testing?

The 3 types of Database Testing are Structural ...READ MORE

answered May 9 in Java by Frankie
• 9,810 points
78 views
0 votes
2 answers

In Java, what is the best way to determine the size of an object?

I happened to find a java class "jdk.nashorn.internal.ir.debug.ObjectSizeCalculator", ...READ MORE

answered Aug 19 in Java by Sirajul
• 37,930 points
179 views