SessionExample.java
上传用户:bj_pst
上传日期:2019-07-07
资源大小:7353k
文件大小:5k
源码类别:

Java编程

开发平台:

Java

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