The following examples show the newer session tracking API in use within Servlets.
The first time a user runs the "Barman" servlet, it sets up a session for him and prompts for his name. On subsequent visits, it addresses him by name straight away, and also keeps a tally of how many times he's visited.
The "Landlord" servlet does the same as the Barman, but in addition it maintains a static vector of sessions so that it can (and does ;-) ) report on everyone in the bar and how much they've had to drink.
Note - "Barman" keeps the users apart, which is typically what you would do with a shopping cart ... "Landlord" provides a management information service too ....
::::::::::::::
Barman.java
::::::::::::::
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Simple Session Tracking
*/
public class Barman extends HttpServlet {
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
HttpSession session = request.getSession(true);
Integer count = (Integer)session.getAttribute("mycounter");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body bgcolor=\"white\">");
if (count == null) {
count = new Integer(0);
out.print("<h1>Welcome. Please enter your name</h1>");
out.print("<form action=/nan/servlet/Barman>");
out.print("<input name=whoyouare>");
out.print("</form>");
} else {
String wanted = request.getParameter("whoyouare");
if (wanted != null) {
session.setAttribute("who",wanted);
} else {
wanted = (String)session.getAttribute("who");
}
count = new Integer(count.intValue() + 1);
out.print("<h1>Welcome back "+wanted+"</h1>");
out.println("This is your visit no. "+count+"<BR>");
}
session.setAttribute("mycounter",count);
out.println("</body>");
out.println("</html>");
}
}
::::::::::::::
Landlord.java
::::::::::::::
import java.io.*;
import java.text.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
/**
* Simple Session Tracking
* Also reports on all users
*/
public class Landlord extends HttpServlet {
public static Vector People;
public void init(ServletConfig cc) throws ServletException {
super.init(cc);
People = new Vector();
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException
{
HttpSession session = request.getSession(true);
Integer count = (Integer)session.getAttribute("mycounter");
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html>");
out.println("<body bgcolor=\"white\">");
if (count == null) {
count = new Integer(0);
out.print("<h1>Welcome. Please enter your name</h1>");
out.print("<form action=/nan/servlet/Landlord>");
out.print("<input name=whoyouare>");
out.print("</form>");
People.addElement(session);
} else {
String wanted = request.getParameter("whoyouare");
if (wanted != null) {
session.setAttribute("who",wanted);
} else {
wanted = (String)session.getAttribute("who");
}
count = new Integer(count.intValue() + 1);
out.print("<h1>Welcome back "+wanted+"</h1>");
out.println("This is your visit no. "+count+"<BR>");
out.println("<BR><H2>Those here present ...</h2>");
for (int i=0; i<People.size();i++) {
HttpSession present = (HttpSession)(People.elementAt(i));
String ere = (String)present.getAttribute("who");
out.print(ere+ " is here and has drunk ");
int ii = ((Integer)present.getAttribute("mycounter")).intValue();
out.print("" + ii + " previously<BR>");
}
}
session.setAttribute("mycounter",count);
out.println("</body>");
out.println("</html>");
}
}
See also
Servlets in more detail
Please note that articles in this section of our
web site were current and correct to the best of our ability when published,
but by the nature of our business may go out of date quite quickly. The
quoting of a price, contract term or any other information in this area of
our website is NOT an offer to supply now on those terms - please check
back via
our main web site
Java - Servlets [1909] - ()
[2652] - ()
[3044] - ()
[3997] - ()
[4432] - ()
Java - Servlets in More Detail [479] - ()
[1495] - ()
[1550] - ()
[1909] - ()
[2183] - ()
[2652] - ()
[2717] - ()
[3044] - ()
[3293] - ()
[4431] - ()
[4432] - ()
resource index - Java
Solutions centre home page
You'll find shorter technical items at
The Horse's Mouth and
delegate's questions answered at
the
Opentalk forum.
At Well House Consultants, we provide
training courses on
subjects such as Ruby, Lua, Perl, Python, Linux, C, C++,
Tcl/Tk, Tomcat, PHP and MySQL. We're asked (and answer)
many questions, and answers to those which are of general
interest are published in this area of our site.