• Unit06: 状态管理-cookie


        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();
            }
        }
    
    }
    FindCostServlet.java
    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();
            }
        }
    
    }
    IndexServlet.java
    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);
        }
    
    }
    LoginServlet.java
    <?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>
    web.xml
  • 相关阅读:
    sed思维导图
    Golang计算文件MD5
    grpc入门
    解决使用fastjson时,null值在转JSONObject时丢失的问题
    ArcGIS空间分析案例教程网格和标记生成
    在 VirtualBox VM 中安装 OpenWRT
    aws中各个系统的用户名
    emacs简单配置和scheme环境
    Vue 中引入 json 的三种方法
    Lodash节流和防抖总结
  • 原文地址:https://www.cnblogs.com/tangshengwei/p/6445901.html
Copyright © 2020-2023  润新知