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

在上一篇《Servlet中的Cookie使用》中,我们学习了很多有关Cookie的知识,例如,如何创建Cookie,如何删除Cookie,如何获取Cookie等。

接下来,我们将使用Servlet Cookie创建一个登录和注销的示例。

1 编写主页面

index.jsp:

    
    <%@ page contentType="text/html;charset=UTF-8" language="java" %>
    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>一点教程网-使用Cookie完成登录注销案例</title>
    </head>
    <body>
    <h1>使用Cookie完成登录注销案例</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>一点教程网-使用Cookie完成登录注销案例</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>一点教程网-使用Cookie完成登录注销案例</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("你已经成功登录!");
                out.print("<br>欢迎你, "+name);
    
                Cookie ck=new Cookie("name",name);
                response.addCookie(ck);
            }else{
                out.print("抱歉,用户名或密码错误!");
                request.getRequestDispatcher("login.jsp").include(request, response);
            }
    
            out.close();
        }
    }

5 编写注销Servlet

LogoutServlet:

    
    import javax.servlet.ServletException;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    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);
    
            //删除Cookie
            Cookie ck=new Cookie("name","");
            ck.setMaxAge(0);
            response.addCookie(ck);
    
            out.print("你已经成功注销!");
        }
    }

6 编写个人中心Servlet

ProfileServlet:

    
    import javax.servlet.ServletException;
    import javax.servlet.http.Cookie;
    import javax.servlet.http.HttpServlet;
    import javax.servlet.http.HttpServletRequest;
    import javax.servlet.http.HttpServletResponse;
    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);
    
            Cookie ck[]=request.getCookies();
    
            String name = null;
    
            for(Cookie c:ck){
                if(c.getName().equals("name")) {
                    name = c.getValue();
                }
            }
    
            if(name!=null){
                out.print("<b>欢迎进入个人中心</b>");
                out.print("<br>欢迎你, "+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/

20210421083110_1.png2)点击“个人中心”,提示如下:

2021042108316_2.png3)点击“登录”,进入登录页面,如下:

2021042108311_3.png

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

202104210835_4.png

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

202104210834_5.png

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

202104210834_6.png

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

202104210834_7.png

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