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.
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();
}
}
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();
}
}