package test; import javax.servlet.*; import javax.servlet.http.*; import java.io.*; /** * The ColorSessionServlet demonstrates the session management * mechanisms built into the servlet API. A session is * established and the client's preferred background color * as well as the number of times they have requested the * servlet are stored. * * @author Dustin R. Callaway * @version 1.0, 08/05/98 */ public class ColorSessionServlet extends HttpServlet { /** * init method is called when servlet is first loaded. */ public void init(ServletConfig config) throws ServletException { super.init(config); //pass ServletConfig to parent } /** * service generates HTML pages that allows the client to * choose a color and then remembers the color on future * requests. */ public void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name; //name of user String color; //user's color preference Integer hitCount; //# of times user has requested servlet //get current session, create if it doesn't exist HttpSession mySession = request.getSession(true); //MIME type to return is HTML response.setContentType("text/html"); //get a handle to the output stream PrintWriter out = response.getWriter(); if (mySession.isNew()) //first time client requests page { //generate HTML form requesting name and color preference out.println(""); out.println("
"); out.println("