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 18, 2019 in Java by Neha
• 6,300 points
19,430 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 18, 2019 by Frankie
• 9,830 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
906 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, 2020 in Java by Sri
• 3,190 points
1,282 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,620 points
1,562 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
• 59,230 points
1,078 views
0 votes
0 answers

java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver

I am getting this exception when I ...READ MORE

May 7, 2022 in Java by narikkadan
• 63,720 points
2,398 views
0 votes
0 answers

java.lang.NumberFormatException: null i

I am getting this error while doing ...READ MORE

May 13, 2022 in Java by Kichu
• 19,050 points
1,978 views
0 votes
1 answer

Alternate solution for RSA security ID

RSA Security ID is a widely used ...READ MORE

answered Apr 19, 2023 in Cyber Security & Ethical Hacking by Edureka
• 12,690 points
324 views
+5 votes
4 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 7, 2018 by Omkar 80,929 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,830 points
2,485 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
• 59,230 points
12,927 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP