home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / xampp / xampp-tomcat-addon-1.4.9-installer.exe / SessionExample.java < prev    next >
Encoding:
Java Source  |  2004-08-28  |  5.0 KB  |  141 lines

  1. /*
  2. * Copyright 2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. *     http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. /* $Id: SessionExample.java,v 1.4 2004/03/18 16:40:33 jfarcand Exp $
  17.  *
  18.  */
  19.  
  20. import java.io.*;
  21. import java.text.*;
  22. import java.util.*;
  23. import javax.servlet.*;
  24. import javax.servlet.http.*;
  25.  
  26. import util.HTMLFilter;
  27.  
  28. /**
  29.  * Example servlet showing request headers
  30.  *
  31.  * @author James Duncan Davidson <duncan@eng.sun.com>
  32.  */
  33.  
  34. public class SessionExample extends HttpServlet {
  35.  
  36.     ResourceBundle rb = ResourceBundle.getBundle("LocalStrings");
  37.     
  38.     public void doGet(HttpServletRequest request,
  39.                       HttpServletResponse response)
  40.         throws IOException, ServletException
  41.     {
  42.         response.setContentType("text/html");
  43.  
  44.         PrintWriter out = response.getWriter();
  45.         out.println("<html>");
  46.         out.println("<body bgcolor=\"white\">");
  47.         out.println("<head>");
  48.  
  49.         String title = rb.getString("sessions.title");
  50.         out.println("<title>" + title + "</title>");
  51.         out.println("</head>");
  52.         out.println("<body>");
  53.  
  54.         // img stuff not req'd for source code html showing
  55.     // relative links everywhere!
  56.  
  57.         // XXX
  58.         // making these absolute till we work out the
  59.         // addition of a PathInfo issue 
  60.     
  61.         out.println("<a href=\"../sessions.html\">");
  62.         out.println("<img src=\"../images/code.gif\" height=24 " +
  63.                     "width=24 align=right border=0 alt=\"view code\"></a>");
  64.         out.println("<a href=\"../index.html\">");
  65.         out.println("<img src=\"../images/return.gif\" height=24 " +
  66.                     "width=24 align=right border=0 alt=\"return\"></a>");
  67.  
  68.         out.println("<h3>" + title + "</h3>");
  69.  
  70.         HttpSession session = request.getSession(true);
  71.         out.println(rb.getString("sessions.id") + " " + session.getId());
  72.         out.println("<br>");
  73.         out.println(rb.getString("sessions.created") + " ");
  74.         out.println(new Date(session.getCreationTime()) + "<br>");
  75.         out.println(rb.getString("sessions.lastaccessed") + " ");
  76.         out.println(new Date(session.getLastAccessedTime()));
  77.  
  78.         String dataName = request.getParameter("dataname");
  79.         String dataValue = request.getParameter("datavalue");
  80.         if (dataName != null && dataValue != null) {
  81.             session.setAttribute(dataName, dataValue);
  82.         }
  83.  
  84.         out.println("<P>");
  85.         out.println(rb.getString("sessions.data") + "<br>");
  86.         Enumeration names = session.getAttributeNames();
  87.         while (names.hasMoreElements()) {
  88.             String name = (String) names.nextElement(); 
  89.             String value = session.getAttribute(name).toString();
  90.             out.println(HTMLFilter.filter(name) + " = " 
  91.                         + HTMLFilter.filter(value) + "<br>");
  92.         }
  93.  
  94.         out.println("<P>");
  95.         out.print("<form action=\"");
  96.     out.print(response.encodeURL("SessionExample"));
  97.         out.print("\" ");
  98.         out.println("method=POST>");
  99.         out.println(rb.getString("sessions.dataname"));
  100.         out.println("<input type=text size=20 name=dataname>");
  101.         out.println("<br>");
  102.         out.println(rb.getString("sessions.datavalue"));
  103.         out.println("<input type=text size=20 name=datavalue>");
  104.         out.println("<br>");
  105.         out.println("<input type=submit>");
  106.         out.println("</form>");
  107.  
  108.         out.println("<P>GET based form:<br>");
  109.         out.print("<form action=\"");
  110.     out.print(response.encodeURL("SessionExample"));
  111.         out.print("\" ");
  112.         out.println("method=GET>");
  113.         out.println(rb.getString("sessions.dataname"));
  114.         out.println("<input type=text size=20 name=dataname>");
  115.         out.println("<br>");
  116.         out.println(rb.getString("sessions.datavalue"));
  117.         out.println("<input type=text size=20 name=datavalue>");
  118.         out.println("<br>");
  119.         out.println("<input type=submit>");
  120.         out.println("</form>");
  121.  
  122.         out.print("<p><a href=\"");
  123.     out.print(response.encodeURL("SessionExample?dataname=foo&datavalue=bar"));
  124.     out.println("\" >URL encoded </a>");
  125.     
  126.         out.println("</body>");
  127.         out.println("</html>");
  128.         
  129.         out.println("</body>");
  130.         out.println("</html>");
  131.     }
  132.  
  133.     public void doPost(HttpServletRequest request,
  134.                       HttpServletResponse response)
  135.         throws IOException, ServletException
  136.     {
  137.         doGet(request, response);
  138.     }
  139.  
  140. }
  141.