Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
20.9.2014 - We have just updated our course layouts and descriptions and added our 2015 schedule.

Sessions in Servlets

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

Related Material

Java - Servlets
  [3997] Servlet v JSP (Java Server Page). What is the difference? - (2013-02-06)
  [3044] Changing a Servlet - more that just editing and compiling - (2010-11-12)
  [2652] Reading and writing cookies in Java Servlets and JSPs - (2010-02-26)
  [1909] javax.servlet cannot be resolved - how to solve - (2008-11-26)

Java - Servlets in More Detail
  [3293] Distributing the server load - yet ensuring that each user return to the same system (Apache httpd and Tomcat) - (2011-05-18)
  [3044] Changing a Servlet - more that just editing and compiling - (2010-11-12)
  [2717] The Multiple Inheritance Conundrum, interfaces and mixins - (2010-04-11)
  [2652] Reading and writing cookies in Java Servlets and JSPs - (2010-02-26)
  [2183] Servlet life cycle, and Java Servlet variable scope - (2009-05-16)
  [1909] javax.servlet cannot be resolved - how to solve - (2008-11-26)
  [1550] Java (JSP and Servlet examples) live on our server - (2008-02-23)
  [1495] Single login and single threaded models - Java and PHP - (2008-01-04)
  [479] New servlet from old - (2005-10-28)

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.


Visitor Ranking 4.0 (5=excellent, 1=poor)

edit your own (not yet published) comments

Average page ranking - 4.0

© WELL HOUSE CONSULTANTS LTD., 2014: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 899360 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/solutions/java-ses ... vlets.html • PAGE BUILT: Wed Mar 28 07:47:11 2012 • BUILD SYSTEM: wizard