Cookie
cookie是保存在客户端的一个“键值对”,用来存储用户的一些信息
cookie的应用:
-在电子商务会话中标识用户
-对网站进行定制,比如你经常浏览哪些内容,就展示哪些页面给你
-网站广告,比如百度联盟,你搜索了什么关键字,在各个网站就可以看到那个关键字的广告
创建cookie
package servletdemo;import java.io.IOException; 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;/*** Servlet implementation class TestCookie*/ @WebServlet("/TestCookie") public class TestCookie extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public TestCookie() {super();// TODO Auto-generated constructor stub }/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub//构造函数第一个参数是key,第二个是valueCookie c=new Cookie("name", "henry"); //设置时间,最低级是秒,此处就是60秒乘以60,也就是一个小时c.setMaxAge(60*60);//将创建的cookie添加到客户端 response.addCookie(c);}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub doGet(request, response);}}
获取cookie
package servletdemo;import java.io.IOException; import java.util.Iterator;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;/*** Servlet implementation class TestGetCookie*/ @WebServlet("/TestGetCookie") public class TestGetCookie extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public TestGetCookie() {super();// TODO Auto-generated constructor stub }/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubCookie [] cookies=request.getCookies();if(cookies!=null){for(Cookie c:cookies){String name=c.getName();String value=c.getValue();System.out.println(name+":"+value);}}}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub doGet(request, response);}}
运行结果
Session
HTTP协议是无状态的,那么我们如何将对象数据持久化的保存呢,java有两个解决办法,一个是cookie,一个就是session,它是用来跟踪用户状态的一种机制,是针对浏览器和服务器的一对一关系
Session接口HttpSession,通常我们会使用它,接口的实现由WEB容器来完成
-可以从HttpServletRequest中获取HttpSession,request.getSession();返回一个HttpSession接口
-将信息保存在HttpSession中,session.setAttribute("usersession",obj);
-从HttpSession中获得信息,session.getAttribute("usersession");
-使session失效,session.invalidate();
创建session
package servletdemo;import java.io.IOException; 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;/*** Servlet implementation class SetSession*/ @WebServlet("/SetSession") public class SetSession extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public SetSession() {super();// TODO Auto-generated constructor stub }/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubHttpSession session=request.getSession();session.setAttribute("session_name", "session_value");}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub doGet(request, response);}}
获得session
package servletdemo;import java.io.IOException; 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;/*** Servlet implementation class GetSession*/ @WebServlet("/GetSession") public class GetSession extends HttpServlet {private static final long serialVersionUID = 1L;/*** @see HttpServlet#HttpServlet()*/public GetSession() {super();// TODO Auto-generated constructor stub }/*** @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)*/protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stubHttpSession session=request.getSession();String value=(String)session.getAttribute("session_name");System.out.println("value:"+value);}/*** @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)*/protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {// TODO Auto-generated method stub doGet(request, response);}}