home *** CD-ROM | disk | FTP | other *** search
Wrap
Java Source | 1998-10-25 | 4.5 KB | 88 lines
import java.io.*; import java.util.Enumeration; import java.util.Date; import javax.servlet.*; import javax.servlet.http.*; public class sessiondemo extends HttpServlet { public String getServletInfo() { return "SessionDemo version 1.06: a sample servlet from vqSoft";} public void dostuff (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { HttpSession tsession=req.getSession(true); res.setContentType("text/html"); PrintWriter os=res.getWriter(); Integer sessioncounter=(Integer) tsession.getValue("sessioncounter"); if (sessioncounter==null) sessioncounter=new Integer(1); else sessioncounter=new Integer(sessioncounter.intValue()+1); tsession.putValue("sessioncounter", sessioncounter); if (req.getParameter("submitdata")!=null) { tsession.putValue(req.getParameter("name"), req.getParameter("value"));} os.println("<head><title>Session demonstration</title></head>"); os.println("<body bgcolor=#add8af>"); os.println("<table border=0 cellspacing=0 cellpadding=2 cols=3 width=100%>"); os.println("<tr><td valign=top>"); os.println("<h1 align=center>Session demonstration</h1>"); os.println("<hr>"); 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>. "); os.println("<i>SessionDemo</i> illustrates <i>vq</i>Server's support for JavaSoft sessions. "); os.println("During a session, the server can keep track of data between HTTP requests.</p>"); os.println("<p><a href="+res.encodeUrl("/servlet/sessiondemo")+">"); os.println("Access this servlet and session using URL encoding.</a><p>"); os.println("<hr>"); os.println("<h3>Request parameters</h3>"); os.println("Requested session ID: "+req.getRequestedSessionId()+"<br>"); os.println("Session ID from cookie: "+req.isRequestedSessionIdFromCookie()+"<br>"); os.println("Session ID from URL: "+req.isRequestedSessionIdFromUrl()+"<br>"); os.println("Valid session requested: "+req.isRequestedSessionIdValid()+"<br>"); os.println("<hr>"); os.println("<h3>Session parameters</h3>"); os.println("Actual session ID: "+tsession.getId()+"<br>"); os.println("Creation time: "+(new Date(tsession.getCreationTime())).toString()+"<br>"); os.println("Last access time: "+(new Date(tsession.getLastAccessedTime())).toString()+"<br>"); os.println("New session: "+tsession.isNew()+"<br>"); os.println("<hr>"); os.println("<h3>Session data</h3>"); os.println("Counter: "+sessioncounter.toString()); String[] tdata=tsession.getValueNames(); for (int i=0; i<tdata.length; i++) if (!tdata[i].equals("sessioncounter")) os.println("<br>"+tdata[i]+": "+tsession.getValue(tdata[i]).toString()); os.println("<hr>"); os.println("<h3>Add data to this session</h3>"); os.println("<form action="+res.encodeUrl("/servlet/sessiondemo")+" method=post>"); os.println("<table border=0>"); os.println("<tr>"); os.println("<td align=right>Name:</td>"); os.println("<td align=left><input type=edit name=name size=40></td>"); os.println("</tr>"); os.println("<tr>"); os.println("<td align=right>Value:</td>"); os.println("<td align=left><input type=edit name=value size=40></td>"); os.println("</tr>"); os.println("<tr>"); os.println("<td></td>"); os.println("<td align=center><input type=reset name=reset value=Reset><input type=submit name=submitdata value=Ok></td>"); os.println("</tr>"); os.println("</table>"); os.println("<p><hr></p>"); 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."); os.println("</td>"); os.println("<td width=20></td>"); os.println("<td valign=top width=150>"); os.println("<a href=/index.html><img src=/vq/server/icons/utab.gif border=0 height=15 width=15>Home page</A>"); os.println("</td>"); os.println("</table>"); os.println("</body></html>"); os.flush();} public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { dostuff(req, res);} public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { dostuff(req, res);}}