Mega Code Archive

 
Categories / Java Tutorial / Servlet
 

Servlet Session Max Inactive Interval

import java.util.*; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class MyServlet extends HttpServlet {       public void doGet(HttpServletRequest req, HttpServletResponse res)                                throws ServletException, IOException {     res.setContentType("text/html");     PrintWriter out = res.getWriter();     // Get the current session object, create one if necessary     HttpSession session = req.getSession();     out.println("<HTML><HEAD><TITLE>SessionTimer</TITLE></HEAD>");     out.println("<BODY><H1>Session Timer</H1>");     // Display the previous timeout     out.println("The previous timeout was " +                 session.getMaxInactiveInterval());     out.println("<BR>");     // Set the new timeout     session.setMaxInactiveInterval(2*60*60);  // two hours     // Display the new timeout     out.println("The newly assigned timeout is " +                 session.getMaxInactiveInterval());     out.println("</BODY></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>