package ch15;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Enumeration;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
public class ShowSession extends HttpServlet {
/*
* (non-Javadoc)
*
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest, HttpServletResponse)
*/
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
/*
* (non-Javadoc)
*
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest, HttpServletResponse)
*/
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session = request.getSession();
boolean clear = request.getParameter("clear") != null;
if (clear)
session.invalidate();
else {
String name = request.getParameter("Name");
String value = request.getParameter("Value");
if (name != null && value != null)
session.setAttribute(name, value);
}
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<html><head><title>Show Session</title></head><body>");
if (clear)
out.println("<h1>Session Cleared:</h1>");
else {
out.println("<h1>In this session:</h1><ul>");
Enumeration names = session.getAttributeNames();
while (names.hasMoreElements()) {
String name = (String) names.nextElement();
out.println("<li>" + name + " = " + session.getAttribute(name));
}
}
out.println("</ul><p><hr><h1>Add String</h1>" + "<form method=\"POST\" action=\"" + request.getRequestURI()
+ "\">" + "Name: <input name=\"Name\" size=20><br>" + "Value: <input name=\"Value\" size=20><br>"
+ "<br><input type=\"submit\" value=\"Submit\">"
+ "<input type=\"submit\" name=\"clear\" value=\"Clear\"></form>");
}
}
'Java > Java for the Web' 카테고리의 다른 글
A Simple Filter (0) | 2013.02.12 |
---|---|
Cookie정보 보기 (0) | 2013.02.12 |
Hello Servlet (0) | 2013.02.12 |
Servlet Filter 개요 (2) | 2013.02.03 |
SSL 통신원리 (0) | 2013.02.03 |