使用Cookies的Servlet登录和登出示例

Cookie是一种存储在客户端的信息。

在前一页中,我们学习了很多关于Cookie的知识,例如如何创建Cookie、如何删除Cookie、如何获取Cookie等。

这里,我们将创建一个使用servlet cookies进行登录和登出的示例。

在这个示例中,我们创建了3个链接:登录、登出和个人资料。用户在登录之前无法访问个人资料页面。如果用户已登出,他需要重新登录才能访问个人资料。

在这个应用程序中,我们创建了以下文件。

  1. index.html
  2. link.html
  3. login.html
  4. LoginServlet.java
  5. LogoutServlet.java
  6. ProfileServlet.java
  7. web.xml

文件:index.html

<!DOCTYPE html>  
<html>  
<head>  
<meta charset="ISO-8859-1">  
<title>Servlet Login Example</title>  
</head>  
<body>  
  
<h1>Welcome to Login App by Cookie</h1>  
<a href="login.html">Login</a>|  
<a href="LogoutServlet">Logout</a>|  
<a href="ProfileServlet">Profile</a>  
  
</body>  
</html>  

File: link.html

<a href="login.html">Login</a> |  
<a href="LogoutServlet">Logout</a> |  
<a href="ProfileServlet">Profile</a>  
<hr>  

File: login.html

<form action="LoginServlet" method="post">  
Name:<input type="text" name="name"><br>  
Password:<input type="password" name="password"><br>  
<input type="submit" value="login">  
</form>  

File: LoginServlet.java

package cn.javatiku;  
  
import java.io.IOException;  
import java.io.PrintWriter;  
import javax.servlet.ServletException;  
import javax.servlet.http.Cookie;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
public class LoginServlet extends HttpServlet {  
    protected void doPost(HttpServletRequest request, HttpServletResponse response)  
                           throws ServletException, IOException {  
        response.setContentType("text/html");  
        PrintWriter out=response.getWriter();  
          
        request.getRequestDispatcher("link.html").include(request, response);  
          
        String name=request.getParameter("name");  
        String password=request.getParameter("password");  
          
        if(password.equals("admin123")){  
            out.print("You are successfully logged in!");  
            out.print("<br>Welcome, "+name);  
              
            Cookie ck=new Cookie("name",name);  
            response.addCookie(ck);  
        }else{  
            out.print("sorry, username or password error!");  
            request.getRequestDispatcher("login.html").include(request, response);  
        }  
          
        out.close();  
    }  
  
}  

File: LogoutServlet.java

package cn.javatiku;  
  
import java.io.IOException;  
import java.io.PrintWriter;  
import javax.servlet.ServletException;  
import javax.servlet.http.Cookie;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
public class LogoutServlet extends HttpServlet {  
    protected void doGet(HttpServletRequest request, HttpServletResponse response)  
                        throws ServletException, IOException {  
        response.setContentType("text/html");  
        PrintWriter out=response.getWriter();  
          
          
        request.getRequestDispatcher("link.html").include(request, response);  
          
        Cookie ck=new Cookie("name","");  
        ck.setMaxAge(0);  
        response.addCookie(ck);  
          
        out.print("you are successfully logged out!");  
    }  
}  

File: ProfileServlet.java

package cn.javatiku;  
  
import java.io.IOException;  
import java.io.PrintWriter;  
import javax.servlet.ServletException;  
import javax.servlet.http.Cookie;  
import javax.servlet.http.HttpServlet;  
import javax.servlet.http.HttpServletRequest;  
import javax.servlet.http.HttpServletResponse;  
public class ProfileServlet extends HttpServlet {  
    protected void doGet(HttpServletRequest request, HttpServletResponse response)  
                          throws ServletException, IOException {  
        response.setContentType("text/html");  
        PrintWriter out=response.getWriter();  
          
        request.getRequestDispatcher("link.html").include(request, response);  
          
        Cookie ck[]=request.getCookies();  
        if(ck!=null){  
         String name=ck[0].getValue();  
        if(!name.equals("")||name!=null){  
            out.print("<b>Welcome to Profile</b>");  
            out.print("<br>Welcome, "+name);  
        }  
        }else{  
            out.print("Please login first");  
            request.getRequestDispatcher("login.html").include(request, response);  
        }  
        out.close();  
    }  
}  

File: web.xml

<?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" id="WebApp_ID" version="2.5">  
    
  <servlet>  
    <description></description>  
    <display-name>LoginServlet</display-name>  
    <servlet-name>LoginServlet</servlet-name>  
    <servlet-class>cn.javatiku.LoginServlet</servlet-class>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>LoginServlet</servlet-name>  
    <url-pattern>/LoginServlet</url-pattern>  
  </servlet-mapping>  
  <servlet>  
    <description></description>  
    <display-name>ProfileServlet</display-name>  
    <servlet-name>ProfileServlet</servlet-name>  
    <servlet-class>cn.javatiku.ProfileServlet</servlet-class>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>ProfileServlet</servlet-name>  
    <url-pattern>/ProfileServlet</url-pattern>  
  </servlet-mapping>  
  <servlet>  
    <description></description>  
    <display-name>LogoutServlet</display-name>  
    <servlet-name>LogoutServlet</servlet-name>  
    <servlet-class>cn.javatiku.LogoutServlet</servlet-class>  
  </servlet>  
  <servlet-mapping>  
    <servlet-name>LogoutServlet</servlet-name>  
    <url-pattern>/LogoutServlet</url-pattern>  
  </servlet-mapping>  
</web-app>  

输出:
cookieloginoutput1.png

cookieloginoutput2.png

cookieloginoutput3.png

cookieloginoutput4.png

cookieloginoutput6.png

标签: java, Java Servlet, Java Servlet语言, Java Servlet开发, Java Servlet语法, Java Servlet脚本, Java Servlet教程, Java Servlet入门, Java Servlet入门教程, Java Servlet进阶, Java Servlet宝典, Java Servlet学习, Java Servlet指南, Java Servlet大全