HTTP是基于请求/响应的无状态的通信协议。
使服务器记得此次请求与之后请求关系的方式,叫做会话管理。
隐藏域:由浏览器在每次请求时主动告知服务器多次请求间必要的信息。仅适用于一些简单的状态 管理,如在线问卷。
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by N3verL4nd on 2017/2/21.
*/
@WebServlet("/questionnaire")
public class HiddenFieldServlet extends HttpServlet {
private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
//response.setHeader("Content-Type", "text/html; charset=UTF-8");
//response.addHeader("Content-Type", "text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println(
""
+ ""
+""
+"问卷调查 "
+""
+""
);
String page = request.getParameter("page");
out.println("");
out.println("");
out.println("");
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
}
cookie:
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by N3verL4nd on 2017/2/23.
*/
@WebServlet(name = "SetServlet", urlPatterns = {"/setcookies.do"})
public class SetServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
for (int i = 0; i < 5; i++){
Cookie cookie = new Cookie(String.valueOf(i), "xiya");
response.addCookie(cookie);
}
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("设置cookies" + "
");
out.println("showcookies");
out.close();
}
}
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by N3verL4nd on 2017/2/23.
*/
@WebServlet(name = "ShowServlet", urlPatterns = {"/showcookies.do"})
public class ShowServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("");
out.println("显示Cookies ");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("");
out.println("Cookie Name ");
out.println("Cookie Value ");
out.println(" ");
Cookie[] cookies = request.getCookies();
if(cookies != null){
for (Cookie cookie : cookies){
String key = cookie.getName();
String value = cookie.getValue();
out.println("");
out.println("" + key + " ");
out.println("" + value + " ");
out.println(" ");
}
}
out.println("
");
out.println("");
out.println("");
}
}
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by N3verL4nd on 2017/2/22.
*/
@WebServlet(name = "SearchServlet", urlPatterns = {"/search.do"})
public class SearchServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("");
out.println("调查 ");
out.println("");
out.println("");
String start = request.getParameter("start");
if (start == null){
start = "1";
}
int count = Integer.parseInt(start);
int begin = 10 * count - 9;
int end = 10 * count;
out.println("第 " + begin + " 到 " + end + " 搜索结果
");
out.println("");
for (int i = begin; i <= end; i++){
out.println("- 搜索结果" + i + "
");
}
out.println("
");
for (int i = 1; i < 10; i++){
if (i == count){
out.println(i);
continue;
}
out.println("" + i + "");
}
out.println("");
out.println("");
}
}
在Servlet/JSP中可以使用HttpSession来进行会话管理。
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
/**
* Created by N3verL4nd on 2017/2/22.
*/
@WebServlet(name = "SessionServlet", urlPatterns = {"/session.do"})
public class SessionServlet extends HttpServlet {
private void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
request.setCharacterEncoding("UTF-8");
response.setContentType("text/html; charset=UTF-8");
PrintWriter out = response.getWriter();
out.println(
""
+ ""
+""
+"问卷调查 "
+""
+""
);
String page = request.getParameter("page");
out.println("");
out.println("");
out.println("");
out.close();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
}
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
/**
* Created by N3verL4nd on 2017/2/23.
*/
@WebServlet(name = "SessionInfoServlet", urlPatterns = {"/SessionInfoServlet"})
public class SessionInfoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html; charset=UTF-8");
HttpSession session = request.getSession();
//session.setMaxInactiveInterval(10);//多少秒失效
PrintWriter out = response.getWriter();
out.println("");
out.println("");
out.println("");
out.println("SessionInfoServlet ");
out.println("");
out.println("");
out.println("New Session:" + session.isNew() + "
");
out.println("Session ID:" + session.getId() + "
");
out.println("getCreationTime:" + new Date(session.getCreationTime()) + "
");
out.println("getLastAccessedTime:" + new Date(session.getLastAccessedTime()) + "
");
out.println("getRequestedSessionId:" + request.getRequestedSessionId() + "
");
out.println("isRequestedSessionIdFromURL:" + request.isRequestedSessionIdFromURL() + "
");
out.println("isRequestedSessionIdFromCookie:" + request.isRequestedSessionIdFromCookie() + "
");
out.println("isRequestedSessionIdValid:" + request.isRequestedSessionIdValid() + "
");
out.println("
刷新
");
}
}
对于以上程序,我们使用Fiddler抓包观察下:
第一次调用该Servlet会返回Set-Cookie,第二次调用我们会向HTTP服务器发送该cookie来获取Web容器内对应的HttpSession。