JSP Page:index.jsp
<%--Document : indexCreated on : Feb 28, 2012, 3:32:07 PMAuthor : dell_pc--%><%@page contentType="text/html" pageEncoding="UTF-8"%><!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>JSP Page</title></head><body><h1>Access Servlet by servlet's Name!</h1><a href="SessionTrackerServlet">SessionTrackerServlet</a><br><a href="enumerationServlet">enumerationServlet</a><br></body></html>
Servlet:SessionTrackerServlet.java
package servlet;import java.io.IOException;import java.io.PrintWriter;import java.util.Date;import javax.servlet.ServletException;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;/**
** @author dell_pc*/public class SessionTrackerServlet extends HttpServlet {protected void processRequest(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();try {/* TODO output your page here */out.println("<html>");out.println("<head>");out.println("<title>" + getServletInfo() + "</title>");out.println("</head>");out.println("<body>");out.println("<h1>Servlet SessionTrackerServlet at " + request.getContextPath() + "</h1>");out.println(" <form name=\"form\" action=\"SessionTrackerServlet\" method=\"POST\">");out.println("<BR><input type=\"submit\" value=\"Session Tracker \" />");out.println("</form>");out.println("</body>");out.println("</html>");} finally {out.close();}}@Overrideprotected void doGet(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {processRequest(request, response);}@Overrideprotected void doPost(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {response.setContentType("text/html;charset=UTF-8");PrintWriter out = response.getWriter();/*
* Get the no. of time access this page, and store value into* client's session under the name session.active*/HttpSession session = request.getSession();String string;Integer count;synchronized (session) {count = (Integer) session.getAttribute("session.active");if (count == null) {count = new Integer(1);string = "Welcome User";} else {count = new Integer((count.intValue()) + 1);string = "Welcome Back, User";}session.setAttribute("session.active", count);}// Invoke methods in doPostprintHeader(out);printSessionData(out, count, string, session);printFooter(out);}@Overridepublic String getServletInfo() {return "SessionTrackerServlet";}/* -- Private Method -- */// Method handle Output Form headerprivate void printHeader(PrintWriter out) {out.println("<html>");out.println("<head>");out.println("<title> Session tracker </title>");out.println("</head>");out.println("<body>");out.println("<h1>Information about session</h1>");}// Method handle output Form centerprivate void printSessionData(PrintWriter out, Integer count, String string, HttpSession session) {out.println("<b><I>" + string + "</I></b>");out.println("<BR>No. of time page visit : ");out.println("<b>" + count + ((count.intValue() == 1) ? " times." : " times.") + "</b>");out.println(" <hr align=\"left\" width=\"35%\"/>");out.println("<b>Other Session Property</b>");out.println("<BR>Current Session id: " + session.getId() + " <I>(keep it secret)</I>");out.println("<BR>Max Timeout: " + session.getMaxInactiveInterval());out.println("<I>(" + session.getMaxInactiveInterval() / 60 + " minutes)</I>");out.println("<BR>Creation time: " + session.getCreationTime());out.println("<I>(" + new Date(session.getCreationTime()) + ")</I>");out.println("<BR>Last access time: " + session.getLastAccessedTime());out.println("<I>(" + new Date(session.getLastAccessedTime()) + ")</I>");}// Method handel Output Form Footerprivate void printFooter(PrintWriter out) {out.println("</body>");out.println("</html>");out.println("<BR><BR><a href=\"SessionTrackerServlet\">Return to Home Page</a>");}}
Output:
Comments
Post a Comment