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, 2019 in Java by Neha
• 6,300 points
4,650 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, 2019 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, 2019 in Java by Wasim
156 views
0 votes
2 answers

What are the different types of constructor in StringBuilder class?

StringBuilder  we 4  as mentioned below. READ MORE

answered Sep 1 in Java by Sri
• 2,970 points
188 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,580 points
426 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, 2019 in Java by Sirajul
• 58,130 points
208 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,450 points

edited Nov 6, 2018 by Omkar 31,662 views
+1 vote
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,520 points
4,516 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,520 points
10,510 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,520 points
1,922 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, 2019 in Java by Frankie
• 9,810 points
473 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, 2019 in Java by Sirajul
• 58,130 points
2,752 views