📜  Servlet – 凭证 Cookie

📅  最后修改于: 2022-05-13 01:55:01.646000             🧑  作者: Mango

Servlet – 凭证 Cookie

cookie是保存在客户端计算机上的一种数据。为了检测重复用户,您必须经历三个阶段

  1. 服务器脚本将一组 cookie 发送到浏览器。例如,姓名、年龄或身份证号码。
  2. 此信息由浏览器保存在本地系统中以供将来使用。
  3. 当浏览器下一次向网络服务器提交请求时,它会将 cookie 信息传输到服务器,服务器使用这些信息来识别用户。

我们将在这里使用 servlet cookie 开发登录和注销示例。以下文件是为此应用程序开发的。

  1. 索引.html
  2. 链接.html
  3. 登录.html
  4. Servlet 登录。Java
  5. 小服务程序注销。Java
  6. Servlet 配置文件。Java
  7. web.xml

例子

项目结构将如下所示。

索引.html:

HTML
  
  
  
  
GeeksForgeeks Credential Cookies Example  
  
  
    
  

Welcome to GeeksForGeeks

     Login|     Logout|     Profile       


HTML
Login  
Logout   
Profile  



HTML
   Name:
   Password:
     


Java
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 ServletLogin 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("geeksforgeeks")){  
            out.print("You are successfully logged in to GFG!");  
            out.print("
Welcome, "+name);                                Cookie ck=new Cookie("name",name);               response.addCookie(ck);           }else{               out.print("sorry, username or password is wrong");               request.getRequestDispatcher("login.html").include(request, response);           }           out.close();       }     }


Java
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 ServletLogout 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!");  
    }  
}


Java
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 ServletProfile 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("Welcome to GfG Profile");  
            out.print("
Welcome, "+name);           }           }else{               out.print("Login first");               request.getRequestDispatcher("login.html").include(request, response);           }           out.close();       }   }


XML
   
    
      
    ServletLogin  
    ServletLogin  
    ServletLogin  
    
    
    ServletLogin  
    /ServletLogin  
    
    
      
    ServletProfile  
    ServletProfile  
    ServletProfile  
    
    
    ServletProfile  
    /ServletProfile  
    
    
      
    ServletLogout  
    ServletLogout  
    ServletLogout  
    
    
    ServletLogout  
    /ServletLogout  
    


链接.html:

HTML

Login  
Logout   
Profile  

  

登录.html:

HTML

   Name:
   Password:
     

Servlet 登录。Java:

Java

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 ServletLogin 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("geeksforgeeks")){  
            out.print("You are successfully logged in to GFG!");  
            out.print("
Welcome, "+name);                                Cookie ck=new Cookie("name",name);               response.addCookie(ck);           }else{               out.print("sorry, username or password is wrong");               request.getRequestDispatcher("login.html").include(request, response);           }           out.close();       }     }

小服务程序注销。Java:

Java

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 ServletLogout 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!");  
    }  
}

Servlet 配置文件。Java:

Java

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 ServletProfile 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("Welcome to GfG Profile");  
            out.print("
Welcome, "+name);           }           }else{               out.print("Login first");               request.getRequestDispatcher("login.html").include(request, response);           }           out.close();       }   }

网页.xml:

XML

   
    
      
    ServletLogin  
    ServletLogin  
    ServletLogin  
    
    
    ServletLogin  
    /ServletLogin  
    
    
      
    ServletProfile  
    ServletProfile  
    ServletProfile  
    
    
    ServletProfile  
    /ServletProfile  
    
    
      
    ServletLogout  
    ServletLogout  
    ServletLogout  
    
    
    ServletLogout  
    /ServletLogout  
    

输出:

运行 index.html 文件,你会得到以下输出

点击个人资料而不登录

现在点击登录链接并输入用户名和密码

登录成功后会显示此画面

现在登录后再次单击个人资料,您现在可以看到个人资料页面

单击注销后将显示此屏幕