1.保存会话的两种技术
Cookie
Cookie测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37
| public class CookieDemo extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setContentType("text/html"); resp.setCharacterEncoding("utf-8"); PrintWriter out = resp.getWriter(); Cookie[] cookies = req.getCookies(); if (cookies != null){ for (Cookie c : cookies){ if (c.getName().equals("name")){ out.write("尊敬的" + c.getValue() + "用户,欢迎登录!"); } if (c.getName().equals("lastLoginTime")){ Long lastLoginTime = Long.parseLong(c.getValue()); SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); String dataStr = dateFormat.format(lastLoginTime); out.write("您上一次登录的时间为:" + dataStr); } } }else{ out.write("这是您第一次访问本站!"); } Cookie cookie1 = new Cookie("name","狂神说Java"); Cookie cookie2 = new Cookie("lastLoginTime",System.currentTimeMillis()+""); resp.addCookie(cookie1); resp.addCookie(cookie2); }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
|

查看HTTP的请求和响应报文,可以发现有Cookie字段。
- 一个Cookie只能保存一个信息
- 一个web站点可以给浏览器发送多个cookie,最多存放20个左右cookie
- Cookie大小有限制4kb
- 浏览器上限:300个左右cookie
删除Cookie;
- 方法1:不设置有效期,关闭浏览器,自动失效
- 方法2:设置有效期时间为 0
解决中文的乱码问题:
1 2
| URLEncoder.encode("秦疆","UTF-8") URLDecoder.decode(cookie.getValue(),"UTF-8")
|
Session(重点)
什么是Session:
- 服务器会给每一个用户(浏览器)创建一个Seesion对象;
- Session是基于Cookie的一种会话机制;
- 一个Seesion独占一个浏览器,只要浏览器没有关闭,这个Session就存在;
- 用户登录之后,整个网站它都可以访问!–> 保存用户的信息;保存购物车的信息……
Session测试:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| public class SessionTest extends HttpServlet { @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { resp.setCharacterEncoding("UTF-8"); req.setCharacterEncoding("UTF-8"); resp.setContentType("text/html;charset=utf-8"); HttpSession session = req.getSession();
session.setAttribute("name","Java"); String sessionId = session.getId();
if (session.isNew()){ resp.getWriter().write("session创建成功!ID:" + sessionId); }else{ resp.getWriter().write("session在服务器中已经存在,ID:" + sessionId); } }
@Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
|
测试结果:

注销Session:
1 2 3 4
| HttpSession session = req.getSession(); session.removeAttribute("name");
session.invalidate();
|
会话自动过期:web.xml配置
1 2 3 4 5
| <session-config> <session-timeout>15</session-timeout> </session-config>
|
2.JSP原理剖析

我们可以使用JSP,它和Servlet一样用于开发动态Web。
JSP还是有必要了解一下,不需要花很多时间,知道即可。
JSP是Java Server Pages的缩写,它的文件必须放到/src/main/webapp
下,文件名必须以.jsp
结尾,整个文件与HTML并无太大区别,但需要插入变量,或者动态输出的地方,使用特殊指令<% ... %>
。
JSP到底是怎么执行的?
在服务器内部工作:
tomcat中有一个work目录;
IDEA中使用Tomcat的会在IDEA的tomcat中生产一个work目录。
工作目录下有2个文件:index_jsp.class
和index_jsp.java
。
也就是说index.jsp
页面最终转变成了Java程序。
浏览器向服务器发送请求,不管访问什么资源,其实都是在访问Servlet!
JSP最终也会被转换成为一个Java类!
在pom.xml
中引入jsp-api 2.2
:
1 2 3 4 5 6
| <dependency> <groupId>javax.servlet.jsp</groupId> <artifactId>jsp-api</artifactId> <version>2.2</version> <scope>provided</scope> </dependency>
|
JSP内置的一些对象:可以直接使用了
1 2 3 4 5 6 7 8
| final javax.servlet.jsp.PageContext pageContext; javax.servlet.http.HttpSession session = null; final javax.servlet.ServletContext application; final javax.servlet.ServletConfig config; javax.servlet.jsp.JspWriter out = null; final java.lang.Object page = this; HttpServletRequest request HttpServletResponse response
|
JSP本质上就是一个Servlet,将一些相关对象实例化了,所以可以内嵌Java代码。
