servlet session(1)
2666 단어 servlet session
package com.bjsxt;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.util.*;
//Session
public class ShowSession extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Session Tracking Example";
HttpSession session = request.getSession(true);
String heading;
// Use getAttribute instead of getValue in version 2.2.
Integer accessCount = (Integer) session.getAttribute("accessCount");
if (accessCount == null) {
accessCount = new Integer(0);
heading = "Welcome, Newcomer";
} else {
heading = "Welcome Back";
accessCount = new Integer(accessCount.intValue() + 1);
}
// Use setAttribute instead of putValue in version 2.2.
session.setAttribute("accessCount", accessCount);
out.println("<html><head><title>Session </title></head>"
+ "<BODY BGCOLOR=\"#FDF5E6\">
" + "<H1 ALIGN=\"CENTER\">"
+ heading + "</H1>
"
+ "<H2>Information on Your Session:</H2>
"
+ "<TABLE BORDER=1 ALIGN=\"CENTER\">
"
+ "<TR BGCOLOR=\"#FFAD00\">
" + " <TH>Info Type<TH>Value
"
+ "<TR>
" + " <TD>ID
" + " <TD>" + session.getId() + "
"
+ "<TR>
" + " <TD>Creation Time
" + " <TD>"
+ new Date(session.getCreationTime()) + "
" + "<TR>
"
+ " <TD>Time of Last Access
" + " <TD>"
+ new Date(session.getLastAccessedTime()) + "
" + "<TR>
"
+ " <TD>Number of Previous Accesses
" + " <TD>"
+ accessCount + "
" + "</TABLE>
" + "</BODY></HTML>");
}
/** Handle GET and POST requests identically. */
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}