Get Set Session Value in a Servlet : Session « Servlet « Java Tutorial






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

public class MyServlet extends HttpServlet {
   
    public void doGet(HttpServletRequest request, HttpServletResponse response) 
        throws ServletException, IOException 
    {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        HttpSession session = request.getSession(true);

        Integer counter2 = (Integer) getServletContext().getAttribute ("counter2");

        if (counter2 == null) {
            counter2 = new Integer(1);
        } else {
            counter2 = new Integer(counter2.intValue() + 1);
        }

        getServletContext().setAttribute("counter2", counter2);

        Integer counter = (Integer) session.getAttribute("counter");

        if (counter == null) {
            counter = new Integer(1);
        } else {
            counter = new Integer(counter.intValue() + 1);
        } 

        session.setAttribute("counter", counter);

        out.println("<HTML>");
        out.println("<HEAD>");
        out.println("<TITLE>");
        out.println("Using Contexts");
        out.println("</TITLE>");
        out.println("</HEAD>");
        out.println("<BODY>");
        out.println("<H1>Using Contexts</H1>");

        out.println("Welcome! You have been here " + counter + " times.<BR>");

        out.println("Total page accesses: " + counter2 + "<BR>");

        if(session.isNew()){
            out.println("This is a new session.<BR>");
        } else {
            out.println("This is not a new session.<BR>");
        }

        out.println("The session ID: " + session.getId() + "<BR>");
        out.println("Last time accessed: " + new Date(session.getLastAccessedTime()) + "<BR>"); 
        out.println("Creation time: " + new Date(session.getCreationTime()) + "<BR>");
        out.println("Timeout length: " + session.getMaxInactiveInterval() + " seconds<BR>");

        out.println("</BODY>");
        out.println("</HTML>");
    } 
}
<?xml version="1.0" encoding="ISO-8859-1"?>

<!DOCTYPE web-app
    PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN"
    "http://java.sun.com/j2ee/dtds/web-app_2_2.dtd">

<web-app>
    <servlet><servlet-name>MyServletName</servlet-name>
             <servlet-class>MyServlet</servlet-class>

             
    </servlet>
    
    <servlet-mapping><servlet-name>MyServletName</servlet-name>
        <url-pattern>/index.html</url-pattern>
    </servlet-mapping>
</web-app>
  Download:  ServletGetSetValueToSession.zip( 89 k)








25.5.Session
25.5.1.Servlet Session Info
25.5.2.Servlet Session Snoop
25.5.3.Servlet Session Max Inactive Interval
25.5.4.Servlet Session Creation Last Access Time
25.5.5.Servlet Session Check
25.5.6.Get Set Session Value in a Servlet
25.5.7.Get Set SessionAttributes in Servlet
25.5.8.use the Servlet API to manage session information
25.5.9.Session Events