Session level scope-Servlets

Session objects may be shared among other servlets and resources as well, within the scope of the same user. The only restriction is that ...


Session objects may be shared among other servlets and resources as well, within the scope of the same user. The only restriction is that the session object can only be shared among servlets that are within the same application server context of the original session; but this is true for all our servlet resources.

Session scope sharing example

In this example, we use the UserSessionCounterSetter  to generate and update the session counter with a variable each time that servlet is called by the user.

We also create another servlet, UserSessionCounterGetter, which gets the counter set in the UserSessionCounterSetter servlet. This demonstrates how these different servlets interact with the same SaveServletStats session object, through the HttpSession object.

We have also used a variable named calledCount, used in many examples thus far, to demonstrate the different values that are set when a counter is incremented by the servlet instance verses the user instance.

package itso.servjsp.servletapi
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class UserSessionCounterSetter 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: Session level scope-Servlets
Session level scope-Servlets
Best Online Tutorials | Source codes | Programming Languages
https://www.1000sourcecodes.com/2012/05/session-level-scope-servlets.html
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/
https://www.1000sourcecodes.com/2012/05/session-level-scope-servlets.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