User session counter servlet

The UserSessionCounter servlet ( Example 1 ) demonstrates how to keep a session counter by user, using the HttpSession tracking technique....


The UserSessionCounter servlet (Example 1) demonstrates how to keep a session counter by user, using the HttpSession tracking technique.

The flow of this servlet can be described in the following steps:

❑  We get a handle to a session object using the getSession method of the request. This method returns the current valid session associated with this request and user. This method takes a boolean argument, true means a new session should be created if none exists, false only returns an existing session, or null.

❑  If it is a new session, or if the session does not contain our object, we must add an object into the session of the type that we want to keep around, using the putValue method of HttpSession.

❑  We now have to create a reference to the SaveServletStats object in the servlet. We use the getValue method of HttpServlet to retrieve this object. Once you have a reference to your object through getValue, you can just manipulate the object as needed; updates to the object are automatically stored as part of the session object.

Example 1
package itso.servjsp.servletapi;
import java.io.*;
import java.util.*;
import java.lang.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class UserSessionCounter extends HttpServlet {
private int calledCount;
public void init(ServletConfig config) throws ServletException {
super.init(config);
calledCount = 0;
}
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
res.setContentType("TEXT/HTML");
PrintWriter out = res.getWriter();
HttpSession session = req.getSession(true);
if (session.isNew() || session.getValue("usersession")==null) {
session.putValue("usersession", new SaveServletStats());
}
SaveServletStats ustats =
(SaveServletStats)session.getValue("usersession");
calledCount++;
ustats.calledCount++;
out.println("<HTML><TITLE>SessionCounter</TITLE><BODY>");
out.println("<H4>This servlet has been called: </H4><BR>");
out.println("<B>" + calledCount + "</B> times since the servlet was loaded
THIS servlet life-cycle session<BR>");
out.println("<B>" + ustats.calledCount + "</B> times since the servlet was
loaded by this user<BR>");
out.println("</BODY></HTML>");
out.close();
}
}
Name

ADO,131,ASP,3,C++,61,CORE JAVA,1,CSS,115,HTML,297,index,5,JAVASCRIPT,210,OS,47,PHP,65,SAD,53,SERVLETS,23,SOFTWARE ENGINEERING,245,SQL,71,TCP/IP,1,XHTML,9,XML,18,
ltr
item
Best Online Tutorials | Source codes | Programming Languages: User session counter servlet
User session counter servlet
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/05/user-session-counter-servlet.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/05/user-session-counter-servlet.html
true
357226456970214079
UTF-8
Loaded All Posts Not found any posts VIEW ALL Readmore Reply Cancel reply Delete By Home PAGES POSTS View All RECOMMENDED FOR YOU LABEL ARCHIVE SEARCH ALL POSTS Not found any post match with your request Back Home Sunday Monday Tuesday Wednesday Thursday Friday Saturday Sun Mon Tue Wed Thu Fri Sat January February March April May June July August September October November December Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec just now 1 minute ago $$1$$ minutes ago 1 hour ago $$1$$ hours ago Yesterday $$1$$ days ago $$1$$ weeks ago more than 5 weeks ago Followers Follow THIS PREMIUM CONTENT IS LOCKED STEP 1: Share to a social network STEP 2: Click the link on your social network Copy All Code Select All Code All codes were copied to your clipboard Can not copy the codes / texts, please press [CTRL]+[C] (or CMD+C with Mac) to copy Table of Content