动手学servlet(四) cookie和session
Cookie
? cookie是保存在客戶(hù)端的一個(gè)“鍵值對(duì)”,用來(lái)存儲(chǔ)用戶(hù)的一些信息
? ?cookie的應(yīng)用:
? ? ? ?-在電子商務(wù)會(huì)話(huà)中標(biāo)識(shí)用戶(hù)
? ? ? ?-對(duì)網(wǎng)站進(jìn)行定制,比如你經(jīng)常瀏覽哪些內(nèi)容,就展示哪些頁(yè)面給你
? ? ? ?-網(wǎng)站廣告,比如百度聯(lián)盟,你搜索了什么關(guān)鍵字,在各個(gè)網(wǎng)站就可以看到那個(gè)關(guān)鍵字的廣告
? 創(chuàng)建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//構(gòu)造函數(shù)第一個(gè)參數(shù)是key,第二個(gè)是valueCookie c=new Cookie("name", "henry"); //設(shè)置時(shí)間,最低級(jí)是秒,此處就是60秒乘以60,也就是一個(gè)小時(shí)c.setMaxAge(60*60);//將創(chuàng)建的cookie添加到客戶(hù)端 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);}} 代碼??運(yùn)行結(jié)果
?
Session
? HTTP協(xié)議是無(wú)狀態(tài)的,那么我們?nèi)绾螌?duì)象數(shù)據(jù)持久化的保存呢,java有兩個(gè)解決辦法,一個(gè)是cookie,一個(gè)就是session,它是用來(lái)跟蹤用戶(hù)狀態(tài)的一種機(jī)制,是針對(duì)瀏覽器和服務(wù)器的一對(duì)一關(guān)系
? Session接口HttpSession,通常我們會(huì)使用它,接口的實(shí)現(xiàn)由WEB容器來(lái)完成
? -可以從HttpServletRequest中獲取HttpSession,request.getSession();返回一個(gè)HttpSession接口
? -將信息保存在HttpSession中,session.setAttribute("usersession",obj);
? -從HttpSession中獲得信息,session.getAttribute("usersession");
? -使session失效,session.invalidate();
? 創(chuàng)建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);}} 代碼轉(zhuǎn)載于:https://www.cnblogs.com/SamFlynn/p/4480202.html
總結(jié)
以上是生活随笔為你收集整理的动手学servlet(四) cookie和session的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: mysql注入漏洞修复方案_注入漏洞修复
- 下一篇: 设计oa系统mysql数据库设计_企业办