使用Cookie完成登录注销案例

在上一篇《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/

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

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

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

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

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

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

 

 

一点教程,一个分享编程知识的公众号。跟着站长一起学习和进步。

通俗易懂,深入浅出,一篇文章只讲一个知识点。

在公交、在地铁、在厕所都可以阅读,随时随地涨姿势。

文章不涉及代码,不烧脑细胞,人人都可以学习。

当你决定关注「一点教程」,你已然超越了90%的程序员!

一点教程二维码