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(
"<!DOCTYPE html>"
+ "<html>"
+"<head>"
+"<title>问卷调查</title>"
+"</head>"
+"<body>"
);
String page = request.getParameter("page");
out.println("<form action='questionnaire' method='post'>");
if (page == null){
out.println("问题一:<input type='text' name='p1q1' /><br />");
out.println("问题二:<input type='text' name='p1q2' /><br />");
out.println("<input type='submit' name='page' value='下一页' />");
}else if ("下一页".equals(page)){
String p1q1 = request.getParameter("p1q1");
String p1q2 = request.getParameter("p1q2");
out.println("问题三:<input type='text' name='p2q1' />");
out.println("<input type='hidden' name='p1q1' value='" + p1q1 + "' />");
out.println("<input type='hidden' name='p1q2' value='" + p1q2 + "' />");
out.println("<input type='submit' name='page' value='完成' />");
}else if ("完成".equals(page)){
out.println(request.getParameter("p1q1") + "<br />");
out.println(request.getParameter("p1q2") + "<br />");
out.println(request.getParameter("p2q1") + "<br />");
}
out.println("</form>");
out.println("</body>");
out.println("</html>");
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" + "<br />");
out.println("<a href='showcookies.do'>showcookies</a>");
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("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>显示Cookies</title>");
out.println("</head>");
out.println("<body>");
out.println("<body>");
out.println("<table>");
out.println("<tr>");
out.println("<th>Cookie Name</th>");
out.println("<th>Cookie Value</th>");
out.println("</tr>");
Cookie[] cookies = request.getCookies();
if(cookies != null){
for (Cookie cookie : cookies){
String key = cookie.getName();
String value = cookie.getValue();
out.println("<tr>");
out.println("<td>" + key + "</td>");
out.println("<td>" + value + "</td>");
out.println("</tr>");
}
} out.println("</table>");
out.println("</body>");
out.println("</html>");
}
}

url重写:其实就是GET请求参数的应用,当服务器响应浏览器上一次请求时,将某些相关信息以超链接方式响应给浏览器,超链接中包含参数信息。

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("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>调查</title>");
out.println("</head>");
out.println("<body>");
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 + " 搜索结果<br />");
out.println("<ul>");
for (int i = begin; i <= end; i++){
out.println("<li>搜索结果" + i + "</li>");
}
out.println("</ul>");
for (int i = 1; i < 10; i++){
if (i == count){
out.println(i);
continue;
}
out.println("<a href='search.do?start=" + i + "'>" + i + "</a>");
}
out.println("</body>");
out.println("</html>");
}
}

在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(
"<!DOCTYPE html>"
+ "<html>"
+"<head>"
+"<title>问卷调查</title>"
+"</head>"
+"<body>"
);
String page = request.getParameter("page");
out.println("<form action='session.do' method='post'>");
if (page == null){
out.println("问题一:<input type='text' name='p1q1' /><br />");
out.println("问题二:<input type='text' name='p1q2' /><br />");
out.println("<input type='submit' name='page' value='下一页' />");
}else if ("下一页".equals(page)){
String p1q1 = request.getParameter("p1q1");
String p1q2 = request.getParameter("p1q2");
HttpSession session = request.getSession();
System.out.println(session.getId());
session.setAttribute("p1q1", p1q1);
session.setAttribute("p1q2", p1q2);
out.println("问题三:<input type='text' name='p2q1' /><br />");
out.println("<input type='submit' name='page' value='完成' />");
}else if ("完成".equals(page)){
String p2q1 = request.getParameter("p2q1");
HttpSession session = request.getSession();
System.out.println(session.getId());
session.setAttribute("p2q1", p2q1); out.println(session.getAttribute("p1q1") + "<br />");
out.println(session.getAttribute("p1q2") + "<br />");
out.println(session.getAttribute("p2q1") + "<br />");
}
out.println("</form>");
out.println("</body>");
out.println("</html>");
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);
}
}

如上所示,HttpSession是通过HttpServletRequest获得的,但是该实例存在于Web容器内。

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("<!DOCTYPE html>");
out.println("<html>");
out.println("<head>");
out.println("<title>SessionInfoServlet</title>");
out.println("</head>");
out.println("<body>"); out.println("New Session:" + session.isNew() + "<br />");
out.println("Session ID:" + session.getId() + "<br />");
out.println("getCreationTime:" + new Date(session.getCreationTime()) + "<br />");
out.println("getLastAccessedTime:" + new Date(session.getLastAccessedTime()) + "<br />");
out.println("getRequestedSessionId:" + request.getRequestedSessionId() + "<br />");
out.println("isRequestedSessionIdFromURL:" + request.isRequestedSessionIdFromURL() + "<br />");
out.println("isRequestedSessionIdFromCookie:" + request.isRequestedSessionIdFromCookie() + "<br />");
out.println("isRequestedSessionIdValid:" + request.isRequestedSessionIdValid() + "<br />");
out.println("<br /><a href='" + response.encodeURL("SessionInfoServlet") + "'>刷新</a><br />");
}
}

对于以上程序,我们使用Fiddler抓包观察下:

第一次调用该Servlet会返回Set-Cookie,第二次调用我们会向HTTP服务器发送该cookie来获取Web容器内对应的HttpSession。

JSP&Servlet学习笔记----第4章的更多相关文章

  1. JSP&Servlet学习笔记----第3章

    Web容器是JSP/Servlet唯一认识的HTTP服务器. HTTP是基于请求/响应的无状态通信协议. 流程: 1.请求来到HTTP服务器 2.HTTP服务器将请求转交给Web容器 3.Web容器创 ...

  2. JSP&Servlet学习笔记----第6章

    JSP与Servlet是一体两面的关系. JSP最终还是被编译为Servlet. <%@page contentType="text/html;charset=UTF-8" ...

  3. JSP&Servlet学习笔记----第5章

    Servlet进阶API 每个Servlet都必须由web容器读取Servlet设置信息(标注或者web.xml).初始化. 对于每个Servlet的设置信息,web容器会为其生成一个ServletC ...

  4. JSP&Servlet学习笔记----第1/2章

    HTML(HyperText Markup Language):超文本标记语言 HTTP(HyperText Transfer Protocol):超文本传输协议 URL(Uniform Resour ...

  5. jsp&servlet学习笔记

    1.路径引用问题 一个css.jsp.html.或者javascript文件从从一个工程复制到另一工程,如果引用的时候使用的时相对路径,看似没有错误,但是却一直引用不进来,这时候要使用绝对路径,这样才 ...

  6. 【JSP&Servlet学习笔记】5.Servlet进阶AIP、过滤器与监听器

    Servlet接口上,与生命周期及请求服务相关的三个方法是init().service()与destory()方法.当Web容器加载Servlet类并实例化之后,会生成ServletConfig对象并 ...

  7. JSP Servlet学习笔记——使用fileupload上传文件

    关键代码如下: index.jsp <body> <center> <h3>文件上传</h3> <font color="red&quo ...

  8. 【JSP&Servlet学习笔记】4.会话管理

    Http本身是无状态通信协议,要进行会话管理的基本原理,就是将需要维护的状态回应给浏览器,由浏览器在下次请求时主动发送状态信息,让Web应用程序“得知”请求之间的关联. 隐藏字段是将状态信息以窗体中看 ...

  9. # jsp及servlet学习笔记

    目录 jsp及servlet学习笔记 JSP(Java Server Page Java服务端网页) 指令和动作: servlet(小服务程序) jsp及servlet学习笔记 JSP(Java Se ...

随机推荐

  1. 使用Django进行后端开发

    目录 更换pip源 搭建虚拟环境 项目目录设置 项目相关配置 更换pip源 更换pip源 搭建虚拟环境 搭建虚拟开发环境 项目目录设置 项目目录设置 项目相关配置 项目相关配置

  2. 如何选择API测试工具

    没有最好,只有最合适. 如今,越来越多的公司正在向DevOps的方向左转,以实现持续集成和持续部署开发.这意味着我们的反馈需要比以往更快,以便确定我们的应用程序是否准备好交付.这就是API测试如此重要 ...

  3. Linux磁盘管理之LVM

    一.LVM介绍 在我们管理Linux磁盘的时候,通常会遇到这么一种情况.在最初规划Linux的磁盘的时候,我们给某个分区划分了一定量的磁盘空间,使用一段时间后,发现我们规划的磁盘空间不足了,这个时候怎 ...

  4. Activity启动过程源码分析(Android 8.0)

    Activity启动过程源码分析 本文来Activity的启动流程,一般我们都是通过startActivity或startActivityForResult来启动目标activity,那么我们就由此出 ...

  5. red note8 pro谷歌套件

    谷歌核心Apps(即Google官方应用“全家桶”),包括YouTube,Google Now,Google Play store,Google Play Games,Google Maps等: 基于 ...

  6. css部分概念

    1.层叠 规则之间属性相同,值不同的时候就会发生声明冲突,这个时候层叠就会起作用了,层叠会将我们声明的不同的值进行保留,相同的值进行比较,选权重值更高的一个来运行.具体情境如下:假设我们定义了一个di ...

  7. .Net Core - AgileHttp

    2020年新年将至,先预祝.Net Core越来越好. 做了这么多年一线开发,经常跟Http打交道.比如调用三方的Webservice,比如集成微信支付的时候服务端发起Prepay支付.特别是现在分布 ...

  8. Spring Cloud 如何动态刷新 Git 仓库配置?

    有时候在配置中心有些参数是需要修改的,这时候如何不重启而达到实时生效的效果呢? 本文基于以下讲解: Spring Cloud Greenwich.SR3 Spring Boot 2.1.7.RELEA ...

  9. [Golang] 剑走偏锋 -- IoComplete ports

    前言 Golang 目前的主要應用領域還是後臺微服務,雖然在業務領域也有所應用但仍然是比較小衆的選擇.大多數的服務運行環境都是linux,而在windows中golang應用更少,而作者因爲特殊情況, ...

  10. 谈谈Java的Collection接口

    目录 谈谈Collection 前言 Collection 方法 1.boolean add(E) 2.void clear() 3.boolean contains(Object o) 4.bool ...