home *** CD-ROM | disk | FTP | other *** search
/ IT.SOFT 22 / ITSOFTCD_22.iso / pc / shareware22 / file3 / VQSERVER.ZIP / servlets / session / sessiondemo.java < prev   
Encoding:
Java Source  |  1998-10-25  |  4.5 KB  |  88 lines

  1. import java.io.*;
  2. import java.util.Enumeration;
  3. import java.util.Date;
  4.  
  5. import javax.servlet.*;
  6. import javax.servlet.http.*;
  7.  
  8. public class sessiondemo extends HttpServlet { 
  9.  
  10.   public String getServletInfo() {    
  11.     return "SessionDemo version 1.06: a sample servlet from vqSoft";}
  12.  
  13.   public void dostuff (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  14.     HttpSession tsession=req.getSession(true);
  15.       res.setContentType("text/html");
  16.       PrintWriter os=res.getWriter();
  17.     Integer sessioncounter=(Integer) tsession.getValue("sessioncounter");
  18.     if (sessioncounter==null)
  19.       sessioncounter=new Integer(1);
  20.     else
  21.       sessioncounter=new Integer(sessioncounter.intValue()+1);
  22.     tsession.putValue("sessioncounter", sessioncounter);
  23.     if (req.getParameter("submitdata")!=null) {
  24.       tsession.putValue(req.getParameter("name"), req.getParameter("value"));}
  25.     os.println("<head><title>Session demonstration</title></head>");
  26.       os.println("<body bgcolor=#add8af>");
  27.     os.println("<table border=0 cellspacing=0 cellpadding=2 cols=3 width=100%>");
  28.     os.println("<tr><td valign=top>");      
  29.       os.println("<h1 align=center>Session demonstration</h1>");
  30.     os.println("<hr>");
  31.     os.println("<p>This page was generated by <i>SessionDemo</i> version 1.06, a sample servlet supplied with <a href=http://www/vqsoft.com/vq/server/index.html><i>vq</i>Server</a>. ");
  32.     os.println("<i>SessionDemo</i> illustrates <i>vq</i>Server's support for JavaSoft sessions. ");
  33.     os.println("During a session, the server can keep track of data between HTTP requests.</p>");
  34.       os.println("<p><a href="+res.encodeUrl("/servlet/sessiondemo")+">");
  35.     os.println("Access this servlet and session using URL encoding.</a><p>");
  36.       os.println("<hr>");
  37.     os.println("<h3>Request parameters</h3>");
  38.     os.println("Requested session ID: "+req.getRequestedSessionId()+"<br>");
  39.       os.println("Session ID from cookie: "+req.isRequestedSessionIdFromCookie()+"<br>");
  40.       os.println("Session ID from URL: "+req.isRequestedSessionIdFromUrl()+"<br>");
  41.       os.println("Valid session requested: "+req.isRequestedSessionIdValid()+"<br>");
  42.       os.println("<hr>");
  43.     os.println("<h3>Session parameters</h3>");
  44.     os.println("Actual session ID: "+tsession.getId()+"<br>");
  45.     os.println("Creation time: "+(new Date(tsession.getCreationTime())).toString()+"<br>");
  46.     os.println("Last access time: "+(new Date(tsession.getLastAccessedTime())).toString()+"<br>");
  47.     os.println("New session: "+tsession.isNew()+"<br>");
  48.     os.println("<hr>");
  49.     os.println("<h3>Session data</h3>");
  50.     os.println("Counter: "+sessioncounter.toString());
  51.     String[] tdata=tsession.getValueNames();
  52.     for (int i=0; i<tdata.length; i++)
  53.       if (!tdata[i].equals("sessioncounter"))
  54.         os.println("<br>"+tdata[i]+": "+tsession.getValue(tdata[i]).toString());
  55.     os.println("<hr>");
  56.     os.println("<h3>Add data to this session</h3>");
  57.     os.println("<form action="+res.encodeUrl("/servlet/sessiondemo")+" method=post>");
  58.     os.println("<table border=0>");
  59.     os.println("<tr>");
  60.     os.println("<td align=right>Name:</td>");
  61.     os.println("<td align=left><input type=edit name=name size=40></td>");
  62.     os.println("</tr>");
  63.     os.println("<tr>");
  64.     os.println("<td align=right>Value:</td>");
  65.     os.println("<td align=left><input type=edit name=value size=40></td>");
  66.     os.println("</tr>");
  67.     os.println("<tr>");
  68.     os.println("<td></td>");
  69.     os.println("<td align=center><input type=reset name=reset value=Reset><input type=submit name=submitdata value=Ok></td>");
  70.     os.println("</tr>");
  71.     os.println("</table>");
  72.     os.println("<p><hr></p>");
  73.     os.println("<p><i>SessionDemo</i> version 1.06. <i>SessionDemo</i> and <a href=http://www/vqsoft.com/vq/server/index.html><i>vq</i>Server</a> are copyright © <i>vq</i>Soft and Steve Shering 1998.");
  74.     os.println("</td>");    
  75.     os.println("<td width=20></td>");
  76.     os.println("<td valign=top width=150>");
  77.     os.println("<a href=/index.html><img src=/vq/server/icons/utab.gif border=0 height=15 width=15>Home page</A>");
  78.     os.println("</td>");
  79.     os.println("</table>");
  80.     os.println("</body></html>");
  81.     os.flush();}
  82.     
  83.   public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  84.     dostuff(req, res);}
  85.  
  86.   public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException {
  87.     dostuff(req, res);}}
  88.