Java Servlet Programing Laungage

Java Servlet Projects

Servlet Project 3

adplus-dvertising
Create an application for getting information about session
Previous Home Next

In this servlet program, session can be tracker into servlet, and count number of time a same page can be access form session.

Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" 
 xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
 <servlet>
  <servlet-name>SessionTrackerServlet</servlet-name>
  <servlet-class>r4r.servlet.SessionTrackerServlet</servlet-class>
 </servlet>
 <servlet-mapping>
  <servlet-name>SessionTrackerServlet</servlet-name>
  <url-pattern>/SessionTrackerServlet</url-pattern>
 </servlet-mapping>
 <session-config>
  <session-timeout>
   30
  </session-timeout>
 </session-config>
</web-app>

Index.jsp

<%-- 
Document   : index.jsp
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
 <html>
  <head>
  <meta http-equiv="Content-Type" 
     content="text/html; charset=UTF-8">
  <title>r4r.co.in-servlet</title>
  </head>
  <body>
  <form>
   <h1>Access Servlet by servlet's Name!</h1>
   <a href="SessionTrackerServlet">SessionTrackerServlet</a>
  </form>
  </body>
</html>

Servlet Program

/*
 * Save as a SessionTrackerServlet.java
 */
package r4r.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 R4R
 */
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();
}
}

@Override
protected void doGet(HttpServletRequest request,
 HttpServletResponse response)throws ServletException,IOException
	{
processRequest(request, response);
}
@Override
protected 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 doPost
printHeader(out);
printSessionData(out, count, string, session);
printFooter(out);
}

@Override
public String getServletInfo() {
return "r4r.co.in-SessionTrackerServlet";
}

/* -- Private Method -- */
// Method handle Output Form header
private 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 center
private 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 Footer 
private void printFooter(PrintWriter out) {
out.println("</body>");
out.println("</html>");
out.println
("<BR><BR><a href=\"SessionTrackerServlet\">Return to Home Page</a>");
}
}
Output of Program
Previous Home Next