Session Tracking Example using Servlets




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(” < h 1 >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 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 > “);
}
}

Random Posts

  • JSF Important Interview Questions
  • Explain Java Comments
  • J2EE Interview Questions
  • Define Session
  • Java Swings Interview Questions

Post a Comment