• Unit07: 状态管理-Session


        Unit07: 状态管理-Session    

     web

     package web;
    
    import java.io.IOException;
    import java.io.PrintWriter;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    public class FindCostServlet extends HttpServlet {
    
        //相当于MainServlet.findCost()
        @Override
        protected void service(
            HttpServletRequest req, 
            HttpServletResponse res) throws ServletException, IOException {
            //获取旧的session,此session是在
            //登录请求中服务器自动创建的.
            HttpSession session = req.getSession();
            String code = (String) 
                session.getAttribute("code");
            //输出数据
            res.setContentType(
                "text/html;charset=utf-8");
            PrintWriter out = res.getWriter();
            out.println(code);
            out.close();
        }
        
    }
    FindCostServlet.java
    package web;
    
    import java.io.IOException;
    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    
    public class LoginServlet extends HttpServlet {
    
        //相当于MainServlet.login()
        @Override
        protected void service(
            HttpServletRequest req, 
            HttpServletResponse res) throws ServletException, IOException {
            //接收参数
            String code = req.getParameter("code");
            //存入session
            //第一次访问session时服务器自动创建它,
            //当前登录就是第一次访问,就创建session
            HttpSession session = req.getSession();
            //session中可以存任意类型的数据
            session.setAttribute("code", code);
            //服务器向浏览器发送响应时,会自动创建
            //一个cookie,并将SID存入此cookie,再
            //将此cookie发送给浏览器.服务器会将
            //此cookie的有效路径设置为项目.
        }
    
    }
    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>jsp4</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>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>
      
      <!-- 配置session的ID释放时长 -->
      <session-config>
          <session-timeout>1</session-timeout>
      </session-config>
      
    </web-app>
    web.xml
  • 相关阅读:
    0x01 虚拟环境搭建
    python操作mysql8windows环境
    Navicat 导入sql文件执行失败问题的处理
    mysql8.0.16免安装教程
    zend studio 9.0.3 注册码
    oneplus8手机蓝牙连接tws耳机无法双击退出语音助手
    竞品分析
    源码阅读方法
    Tomcat内核1
    Asp.NetCore3.1开源项目升级为.Net6.0
  • 原文地址:https://www.cnblogs.com/tangshengwei/p/6445906.html
Copyright © 2020-2023  润新知