2021-04-21 08:34  阅读(93)
文章分类:Servlet 简单教程 文章标签:Servlet
©  原文作者:一点教程 原文地址:http://www.yiidian.com/servlet/

我们可以将对象绑定到HttpSession实例上,并使用setAttribute和getAttribute方法获取对象。

在上一篇《Servlet中HttpSession使用》中,我们了解了什么是HttpSession,如何从会话对象存储和获取数据等。

本文我们将创建一个不使用数据库版本的用户登录和注销应用程序。我们假设密码为yiidian。大致效果是:用户必须先登录才能进入个人中心页面。如果用户已注销,则需要再次登录才能访问个人资料。


为了让大家更加理解Cooke与HttpSession的区别,可以查看Cookie版本的用户登录注销程序:

《使用Cookie完成登录注销案例》


1 编写主页面

index.jsp:

    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>一点教程网-HttpSession完成用户登录和注销</title>
    </head>
    <body>
    <h1>HttpSession完成用户登录和注销</h1>
    <a href="login.jsp">登录</a>|
    <a href="LogoutServlet">注销</a>|
    <a href="ProfileServlet">个人中心</a>
    </body>
    </html>

2 编写通用链接页面

link.jsp:

    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>一点教程网-HttpSession完成用户登录和注销</title>
    </head>
    <body>
    <a href="login.jsp">登录</a>|
    <a href="LogoutServlet">注销</a>|
    <a href="ProfileServlet">个人中心</a>
    <hr/>
    </body>
    </html>

3 编写登录页面

login.jsp:

    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>一点教程网-HttpSession完成用户登录和注销</title>
    </head>
    <body>
    <form action="LoginServlet" method="post">
        用户名:<input type="text" name="name"><br>
        密码:<input type="password" name="password"><br>
        <input type="submit" value="登录">
    </form>
    </body>
    </html>

4 编写登录Servlet

LoginServlet:

    
    import java.io.*;
    import javax.servlet.*;
    import javax.servlet.http.*;
    
    /**
     * 一点教程网 - http://www.yiidian.com
     */
    public class LoginServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        protected void doPost(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out=response.getWriter();
            request.getRequestDispatcher("link.jsp").include(request, response);
    
            String name=request.getParameter("name");
            String password=request.getParameter("password");
    
            if(password.equals("yiidian")){
                out.print("欢迎你, "+name);
                HttpSession session=request.getSession();
                session.setAttribute("name",name);
            }
            else{
                out.print("抱歉, 用户名或密码错误!");
                request.getRequestDispatcher("login.jsp").include(request, response);
            }
            out.close();
        }
    }

5 编写注销Servlet

LogoutServlet:

    
    import javax.servlet.ServletException;
    import javax.servlet.http.*;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    /**
     * 一点教程网 - http://www.yiidian.com
     */
    public class LogoutServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out=response.getWriter();
    
            request.getRequestDispatcher("link.jsp").include(request, response);
    
            HttpSession session=request.getSession();
            session.invalidate();
    
            out.print("你已经成功注销!");
    
            out.close();
        }
    }

6 编写个人中心Servlet

ProfileServlet:

    
    import javax.servlet.ServletException;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    import javax.servlet.http.HttpSession;
    import java.io.IOException;
    import java.io.PrintWriter;
    
    /**
     * 一点教程网 - http://www.yiidian.com
     */
    public class ProfileServlet extends HttpServlet {
        private static final long serialVersionUID = 1L;
    
        protected void doGet(HttpServletRequest request, HttpServletResponse response)
                throws ServletException, IOException {
    
            response.setContentType("text/html;charset=utf-8");
            PrintWriter out=response.getWriter();
            request.getRequestDispatcher("link.jsp").include(request, response);
    
            HttpSession session=request.getSession(false);
            if(session!=null && session.getAttribute("name")!=null){
                String name=(String)session.getAttribute("name");
    
                out.print("你好, "+name+" 欢迎进入个人中心");
            }
            else{
                out.print("请先登录");
                request.getRequestDispatcher("login.jsp").include(request, response);
            }
            out.close();
        }
    }

7 配置web.xml

web.xml:

    
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
             version="3.1">
    
        <servlet>
            <servlet-name>LoginServlet</servlet-name>
            <servlet-class>LoginServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>LoginServlet</servlet-name>
            <url-pattern>/LoginServlet</url-pattern>
        </servlet-mapping>
        <servlet>
            <servlet-name>ProfileServlet</servlet-name>
            <servlet-class>ProfileServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>ProfileServlet</servlet-name>
            <url-pattern>/ProfileServlet</url-pattern>
        </servlet-mapping>
        <servlet>
            <servlet-name>LogoutServlet</servlet-name>
            <servlet-class>LogoutServlet</servlet-class>
        </servlet>
        <servlet-mapping>
            <servlet-name>LogoutServlet</servlet-name>
            <url-pattern>/LogoutServlet</url-pattern>
        </servlet-mapping>
    
    
    </web-app>

8 运行测试

1)首先访问index.jsp页面,http://localhost:8080/

20210421083114_1.png

2)点击“个人中心”,提示如下:

20210421083110_2.png

3)点击“登录”,进入登录页面,如下:

2021042108314_3.png

4)输入错误的密码,提示如下:

202104210837_4.png

5)输入正确的密码(yiidian),提示如下:

202104210835_5.png

6)点击“个人中心”,如下:

202104210835_6.png

7)点击“注销”,如下:

202104210835_7.png

点赞(0)
版权归原创作者所有,任何形式转载请联系作者; Java 技术驿站 >> 使用Session完成登录注销案例
上一篇
Servlet中HttpSession使用
下一篇
使用Servlet完成CRUD