java left logo
java middle logo
java right logo
 

Home arrow Java EE Tips arrow Java Servlet arrow How to get/set values from servlet page to session object
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4100
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
How to get/set values from servlet page to session object E-mail
User Rating: / 66
PoorBest 

Applications like UserManagement, Shopping Cart etc require to store values in cache so that when a user returns his information is restored from cache.

There are three ways to implement sessions in Java Servlets:

  • Cookies: HTTP Cookies to store information. Methods response.addCookie(cookie) will add cookie and request.getCookies() will return value of cookies.
  • URL Rewriting: Append data on the end of each URL that identifies the session like http://localhost/yourservlet?session-id=value.
  • Hidden form fields: HTML forms can have
    <INPUT TYPE="HIDDEN" NAME="session" VALUE="...">.

Example below displays the session information like how many time user has accessed the page, session id, creation time etc.

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;
import java.util.*;

public class sessionServlet extends HttpServlet 
{
  public void doGet(HttpServletRequest request,HttpServletResponse response)
  throws ServletException, IOException 
  {
    HttpSession session = request.getSession(true);
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    String title = "Session Information Servlet";
    String heading;
    int cnt =0;
    if (session.isNew()) 
    {
      heading = "Welcome, New";
    
    else 
    {
      heading = "Welcome Back";
      int oldcnt = (int)session.getValue("accessCount")
      if (oldcnt != null
      {
        cnt =oldcnt + 1;
      }
    }
    session.putValue("accessCount", cnt)
    
    out.println(ServletUtilities.headWithTitle(title+
      "<H1 ALIGN=\"CENTER\">" + heading + "</H1>\n" +
      "<TABLE BORDER=1 ALIGN=CENTER>\n" +
      "<TR>\n" +
        "  <TD>ID\n" +
        "  <TD>" + session.getId() "\n" +
      "<TR>\n" +
        "  <TD>Creation Time\n" +
        "  <TD>" new Date(session.getCreationTime()) "\n" +
      "<TR>\n" +
        "  <TD>Time of Last Access\n" +
        "  <TD>" new Date(session.getLastAccessedTime()) "\n" +
      "<TR>\n" +
        "  <TD>Number of Accesses\n" +
        "  <TD>" + accessCount + "\n" +
      "</TABLE>\n" +
      "</BODY></HTML>");

  }

  public void doPost(HttpServletRequest request,HttpServletResponse response)
  throws ServletException, IOException 
  {
    doGet(request, response);
  }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.