1 17 20 21 import java.io.*; 22 import java.text.*; 23 import java.util.*; 24 import javax.servlet.*; 25 import javax.servlet.http.*; 26 27 import util.HTMLFilter; 28 29 34 35 public class SessionExample extends HttpServlet { 36 37 ResourceBundle rb = ResourceBundle.getBundle("LocalStrings"); 38 39 public void doGet(HttpServletRequest request, 40 HttpServletResponse response) 41 throws IOException, ServletException 42 { 43 response.setContentType("text/html"); 44 45 PrintWriter out = response.getWriter(); 46 out.println("<html>"); 47 out.println("<body bgcolor=\"white\">"); 48 out.println("<head>"); 49 50 String title = rb.getString("sessions.title"); 51 out.println("<title>" + title + "</title>"); 52 out.println("</head>"); 53 out.println("<body>"); 54 55 58 62 out.println("<a HREF=\"../sessions.html\">"); 63 out.println("<img SRC=\"../images/code.gif\" height=24 " + 64 "width=24 align=right border=0 alt=\"view code\"></a>"); 65 out.println("<a HREF=\"../index.html\">"); 66 out.println("<img SRC=\"../images/return.gif\" height=24 " + 67 "width=24 align=right border=0 alt=\"return\"></a>"); 68 69 out.println("<h3>" + title + "</h3>"); 70 71 HttpSession session = request.getSession(true); 72 out.println(rb.getString("sessions.id") + " " + session.getId()); 73 out.println("<br>"); 74 out.println(rb.getString("sessions.created") + " "); 75 out.println(new Date(session.getCreationTime()) + "<br>"); 76 out.println(rb.getString("sessions.lastaccessed") + " "); 77 out.println(new Date(session.getLastAccessedTime())); 78 79 String dataName = request.getParameter("dataname"); 80 String dataValue = request.getParameter("datavalue"); 81 if (dataName != null && dataValue != null) { 82 session.setAttribute(dataName, dataValue); 83 } 84 85 out.println("<P>"); 86 out.println(rb.getString("sessions.data") + "<br>"); 87 Enumeration names = session.getAttributeNames(); 88 while (names.hasMoreElements()) { 89 String name = (String ) names.nextElement(); 90 String value = session.getAttribute(name).toString(); 91 out.println(HTMLFilter.filter(name) + " = " 92 + HTMLFilter.filter(value) + "<br>"); 93 } 94 95 out.println("<P>"); 96 out.print("<form action=\""); 97 out.print(response.encodeURL("SessionExample")); 98 out.print("\" "); 99 out.println("method=POST>"); 100 out.println(rb.getString("sessions.dataname")); 101 out.println("<input type=text size=20 name=dataname>"); 102 out.println("<br>"); 103 out.println(rb.getString("sessions.datavalue")); 104 out.println("<input type=text size=20 name=datavalue>"); 105 out.println("<br>"); 106 out.println("<input type=submit>"); 107 out.println("</form>"); 108 109 out.println("<P>GET based form:<br>"); 110 out.print("<form action=\""); 111 out.print(response.encodeURL("SessionExample")); 112 out.print("\" "); 113 out.println("method=GET>"); 114 out.println(rb.getString("sessions.dataname")); 115 out.println("<input type=text size=20 name=dataname>"); 116 out.println("<br>"); 117 out.println(rb.getString("sessions.datavalue")); 118 out.println("<input type=text size=20 name=datavalue>"); 119 out.println("<br>"); 120 out.println("<input type=submit>"); 121 out.println("</form>"); 122 123 out.print("<p><a HREF=\""); 124 out.print(response.encodeURL("SessionExample?dataname=foo&datavalue=bar")); 125 out.println("\" >URL encoded </a>"); 126 127 out.println("</body>"); 128 out.println("</html>"); 129 130 out.println("</body>"); 131 out.println("</html>"); 132 } 133 134 public void doPost(HttpServletRequest request, 135 HttpServletResponse response) 136 throws IOException, ServletException 137 { 138 doGet(request, response); 139 } 140 141 } 142 | Popular Tags |