• JSP-4(Session)


    Session(⽤户会话)

    服务器⽆法识别每⼀次 HTTP 请求的出处(不知道来⾃于哪个终端),它只会接受到⼀个请求信号,所以就存在⼀个问题:将⽤户的响应发送给其他⼈,必须有⼀种技术来让服务器知道请求来⾃哪,这就是会话技术。
    会话:就是客户端和服务器之间发⽣的⼀系列连续的请求和响应的过程,打开浏览器进⾏操作到关闭浏览器的过程。
    会话状态:指服务器和浏览器在会话过程中产⽣的状态信息,借助于会话状态,服务器能够把属于同⼀次会话的⼀系列请求和响应关联起来。
     

    实现会话有两种⽅式:
    session
    cookie
    区别为: session为服务端保存信息,cookie在客户端保存信息
    属于同⼀次会话的请求都有⼀个相同的标识符,sessionID
    session 常⽤的⽅法:
    String getId() 获取 sessionID
    void setMaxInactiveInterval(int interval) 设置 session 的失效时间,单位为秒
    int getMaxInactiveInterval() 获取当前 session 的失效时间
    void invalidate() 设置 session ⽴即失效
    void setAttribute(String key,Object value) 通过键值对的形式来存储数据
    Object getAttribute(String key) 通过键获取对应的数据
    void removeAttribute(String key) 通过键删除对应的数据

    一个例子

    与上次的登录界面要求差不多,先用request来转发用户名信息,再用session来存储读取

    login.jsp(用了表单,通过servlet来处理逻辑)

    <html>
    <head>
        <title>Title</title>
    </head>
    <body>
        <form action="/login" method="post">
            <table>
                <tr>
                    <td>用户名:</td>
                    <td><input type="text" name="username"></td>
                </tr>
                <tr>
                    <td> 密码:</td>
                    <td><input type="text" name="password"></td>
                </tr>
                <tr>
                    <td><input type="submit" value="登录"></td>
                    <td><input type="submit" value="重置"></td>
                </tr>
            </table>
        </form>
    </body>
    </html>

    通过映射来设置用户初始值

        <servlet>
            <servlet-name>Login</servlet-name>
            <servlet-class>com.servlet.LoginServlet</servlet-class>
            <init-param>
                <param-name>username</param-name>
                <param-value>admin</param-value>
            </init-param>
            <init-param>
                <param-name>password</param-name>
                <param-value>123123</param-value>
            </init-param>
                
        </servlet>
        <servlet-mapping>
            <servlet-name>Login</servlet-name>
            <url-pattern>/login</url-pattern>
        </servlet-mapping>
     

    在LoginServlet中进行比对,跳转的逻辑处理

    public class LoginServlet extends HttpServlet {
        private String myusername;
        private String mypassword;
        @Override
        public void init(ServletConfig config) throws ServletException {
            myusername=config.getInitParameter("username");
            mypassword=config.getInitParameter("password");
    } //通过init弄出config,然后由config调用web.xml中设置的初始值 @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String username=req.getParameter("username"); String password=req.getParameter("password"); if(username.equals(myusername) && password.equals(mypassword)){ HttpSession session=req.getSession(); //通过req.getSession方法调出session session.setAttribute("username",username); //由session保存用户名 resp.sendRedirect("welcome.jsp"); }else{ resp.sendRedirect("login.jsp"); } } }

    welcome.jsp

    welcome!<%=session.getAttribute("username")%> <br/>
    <
    a href="/logout" >退出登录</a>

    LogoutServlet

    加注解 @WebServlet("/logout")

      protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
            HttpSession session=req.getSession();
            session.invalidate();  //销毁session
            resp.sendRedirect("login.jsp");
        }

    用request保存信息以及用session保存信息的区别

       现象:当用request去传递username时,重新访问welcome.jsp后(request变了)发现用户信息失效了

       原因:request生命周期就是一个请求响应

      

     

      用session存取之后的结果

     

  • 相关阅读:
    samba
    sed用法
    Jenkins流水线项目发布流程
    Jenkins
    CI/CD
    tomcat
    gitlab
    rsync
    HAPROXY
    基于LVS的web集群部署(http)
  • 原文地址:https://www.cnblogs.com/hanabi-521/p/14306692.html
Copyright © 2020-2023  润新知