Home Accessibility Courses Diary The Mouth Forum Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
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
  [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.


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

edit your own (not yet published) comments

Average page ranking - 4.0

© WELL HOUSE CONSULTANTS LTD., 2024: Well House Manor • 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • FAX: 01144 1225 793803 • 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