Unit06: 状态管理-cookie
web
package web; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FindCostServlet extends HttpServlet { //相当于MainServlet.findCost() @Override protected void service( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { Cookie[] cs = req.getCookies(); if(cs != null) { res.setContentType( "text/html;charset=utf-8"); PrintWriter out = res.getWriter(); for(Cookie c : cs) { out.println( c.getName()+":"+c.getValue()); } out.close(); } } }
package web; import java.io.IOException; import java.io.PrintWriter; import java.net.URLDecoder; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class IndexServlet extends HttpServlet { //相当于MainServlet.toIndex() @Override protected void service( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { //浏览器再次访问此服务器时,会自动传入 //之前保存的cookie,现在获取此cookie. Cookie[] cs = req.getCookies(); //将cookie中的数据显示到页面上 if(cs != null) { res.setContentType( "text/html;charset=utf-8"); PrintWriter out = res.getWriter(); for(Cookie c : cs) { String value = URLDecoder.decode( c.getValue(),"utf-8"); out.println( c.getName()+":"+value); } out.close(); } } }
package web; import java.io.IOException; import java.net.URLEncoder; import javax.servlet.ServletException; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class LoginServlet extends HttpServlet { //相当于MainServlet.login() @Override protected void service( HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { //接收传入的参数 String code = req.getParameter("code"); //存入cookie,给其他功能使用 //1.一个cookie对象中只能存一个数据 //2.所存的数据必须是字符串 //3.可以创建更多个cookie以保存更多的数据 Cookie c1 = new Cookie("code",code); //设置cookie的生存时间 c1.setMaxAge(600000); //将cookie存入response,在服务器响应时, //会自动把cookie发送给浏览器. res.addCookie(c1); //向cookie中存中文 Cookie c2 = new Cookie("city", URLEncoder.encode("北京", "utf-8")); res.addCookie(c2); //设置cookie的有效路径 Cookie c3 = new Cookie("name","Tarena"); c3.setPath("/jsp3"); res.addCookie(c3); } }
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> <display-name>jsp3</display-name> <welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> <welcome-file>default.html</welcome-file> <welcome-file>default.htm</welcome-file> <welcome-file>default.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>login</servlet-name> <servlet-class>web.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>login</servlet-name> <url-pattern>/main/login</url-pattern> </servlet-mapping> <servlet> <servlet-name>index</servlet-name> <servlet-class>web.IndexServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>index</servlet-name> <url-pattern>/main/index</url-pattern> </servlet-mapping> <servlet> <servlet-name>findCost</servlet-name> <servlet-class>web.FindCostServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>findCost</servlet-name> <url-pattern>/cost/find</url-pattern> </servlet-mapping> </web-app>