Rakesh KR
Rakesh KR

Reputation: 6527

In how many ways we can track the sessions in servlet?

What are the different ways to track the session in servlet. Is it is possible by using hidden files ?

Upvotes: 0

Views: 2986

Answers (8)

Madhusudan Joshi
Madhusudan Joshi

Reputation: 45

There are various session tracking mechanisms

  1. Session API
  2. Cookies
  3. URL Rewriting
  4. Hidden Form Fields

Hidden Form Fields ->

In this case, we use hidden text-field for maintaining the state of a user. It is just programmer's trick to remember client information.


Upvotes: 0

user2932140
user2932140

Reputation:

Suppose a servlet that implements HTTP sessions receives HTTP requests from three different clients (browsers). For each client request, the servlet must be able to determine the HTTP session to which the client request pertains. Each client request belongs to just one of the three client sessions being tracked by the servlet. Currently, the product offers three ways to track sessions:

With cookies
With URL rewriting
With SSL information

Upvotes: 0

Krushna
Krushna

Reputation: 6050

Session can be maintained in flowing ways

  1. Hidden form field :- Use full when the form are generated dynamically
  2. cookies
  3. URL rewriting
  4. HttpSession

The HttpSession depend on the cookies. It uses cookies to store the session id in client system.

Upvotes: 0

Omiye Jay Jay
Omiye Jay Jay

Reputation: 449

The different ways to track session in servlet are:

  1. Using the session API: a sample code for that is:

    //store the username object in the session-scope
    HttpSession session = request.getSession();
    session.setAttribute("username",username);
    

    To retrieve the session attribute,use the session.getAttribute()

    Username username = (Username) session.getAttribute("username");
    
  2. Using the cookies API: sample code:

    String name = request.getParameter("username");
    Cookie c = new Cookie("username",name);
    response.addCookie(c);
    

    It should be noted that a major disadvantage of using cookies for session management is that sometimes the client might have cookies turned off.

  3. Using URL-Rewriting: The URL-rewriting strategy is not as transparrent as the cookie strategy. It could be implemented in a form as below:

    //present the form
    out.println("<form action='"+response.encodeURL("login.")+"'");
    
  4. Hidden form field can also be used.

Upvotes: 2

Prasad Kharkar
Prasad Kharkar

Reputation: 13556

There are three ways

  • hidden form fields
  • cookies
  • url encoding.

One of the ways is to use HttpSession You can create session using

HttpSession session = request.getSession();

or you can use HttpSession session = request.getSession(true). Both statements mean that If there is associated session with this user, then return that one or create a new session. If false is passed, then new session is not created.

Upvotes: 6

Prabhaker A
Prabhaker A

Reputation: 8473

These are the ways to tracking Session

1.With cookies
2.With URL rewriting
3.With SSL information

For more details click here

Upvotes: 0

SpringLearner
SpringLearner

Reputation: 13844

sessions can be maintained in the following ways

  • hidden form field.
  • cookies
  • encodeURL
  • session object have a look at here link

Upvotes: 3

Suresh Atta
Suresh Atta

Reputation: 121998

Yes,it is possible with hidden fields.

And there are other ways too

  • User authorization
  • Hidden fields
  • URL rewriting
  • Cookies
  • Session tracking API

Upvotes: 1

Related Questions