import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class UsingContexts extends HttpServlet
{
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
response.setContentType("text/html");
PrintWriter out = response.getWriter();
HttpSession session = request.getSession(true);
Integer counter2 = (Integer) getServletContext().getAttribute ("counter2");
if (counter2 == null) {
counter2 = new Integer(1);
} else {
counter2 = new Integer(counter2.intValue() + 1);
}
getServletContext().setAttribute("counter2", counter2);
Integer counter = (Integer) session.getAttribute("counter");
if (counter == null) {
counter = new Integer(1);
} else {
counter = new Integer(counter.intValue() + 1);
}
session.setAttribute("counter", counter);
out.println("");
out.println("");
out.println("");
out.println("Using Contexts");
out.println("");
out.println("");
out.println("");
out.println("Using Contexts
");
out.println("Welcome! You have been here " + counter + " times.
");
out.println("Total page accesses: " + counter2 + "
");
if(session.isNew()){
out.println("This is a new session.
");
} else {
out.println("This is not a new session.
");
}
out.println("The session ID: " + session.getId() + "
");
out.println("Last time accessed: " + new Date(session.getLastAccessedTime()) + "
");
out.println("Creation time: " + new Date(session.getCreationTime()) + "
");
out.println("Timeout length: " + session.getMaxInactiveInterval() + " seconds
");
out.println("");
out.println("");
}
}
UsingContextsServlet.zip( 89 k)