5.servlet cookie自动登录的实例

  1.要建的文档,.java用servlet创建

  2.建一张登陆表格 index.jsp

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <base href="<%=basePath%>">
   
    <title>My JSP 'index.jsp' starting page</title>
 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">    
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->
  </head>
 
  <body>
    <form name="f1" id="f1" action="<%=path %>/servlet/LoginServlet" method="post">
      <table border="0">
        <tr>
          <td>Username:</td>
          <td><input type="text" name="username" value="${un}"></td>
        </tr>
        <tr>
          <td>Password:</td>
          <td><input type="password" name="password" value="${pw}"></td>
        </tr>
        <tr>
         <td>Autologin:</td>
         <td><input type="checkbox" name="auto" value="1"></td>
        </tr>
        <tr>
          <td colspan="2" align="center"><input type="submit" value="Login"></td>
        </tr>
      </table>
    </form>
  </body>
</html>

  

  3.建立预备登陆servlet,判断该用户名和密码是否已经存在,若是存在则呈现已经有用户名和密码的值。PrepareLogin.java

package com.amaker.servlet;

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 PrepareLogin extends HttpServlet {

 /**
  *
  */
 private static final long serialVersionUID = -8306532188182712167L;

 /**
  * Constructor of the object.
  */
 public PrepareLogin() {
  super();
 }

 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }


 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  Cookie[] cs=request.getCookies();
  if(cs!=null&&cs.length>0){
   for(int i=0;i<cs.length;i++){
    Cookie c=cs[i];
    if(c.getName().equals("username")){
     String value=c.getValue();
     request.setAttribute("un", value);
    }
    if(c.getName().equals("password")){
     String password=c.getValue();
     request.setAttribute("pw", password);
    }
   
   }
  }
  request.getRequestDispatcher("/index.jsp").forward(request, response);
 
 
 }


 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {

  doGet(request, response);

 }


 public void init() throws ServletException {
  // Put your code here
 }

}

  4.修改index.html中

     <TR>
      <td>Username:</td> <td><input type="text" name="username" value="${un}"></td> </tr> <tr> <td>Password:</td> <td><input type="password" name="password" value="${pw}"></td>
     </tr>

  

  5.建立登陆的servlet,在提交后转到显示之前输入的显示用户名和密码的页面。LoginServet.java

package com.amaker.servlet;
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 {
 /**
  * Constructor of the object.
  */
 public LoginServlet() {
  super();
 }
 /**
  * Destruction of the servlet. <br>
  */
 public void destroy() {
  super.destroy(); // Just puts "destroy" string in log
  // Put your code here
 }

 public void doGet(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
 
  String username=request.getParameter("username");
  String password=request.getParameter("password");
  String auto=request.getParameter("auto");
  if(auto!=null&&auto.equals("1")){
   Cookie c1=new Cookie("username", username);
   Cookie c2=new Cookie("password", password);
   c1.setMaxAge(60*60*24*7);//保存的时间为一周
   c2.setMaxAge(60*60*24*7);
   response.addCookie(c1);
   response.addCookie(c2);
   
   
  }
 
 
  response.setContentType("text/html");
  PrintWriter out = response.getWriter();
  out.println("<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">");
  out.println("<HTML>");
  out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
  out.println("  <BODY>");
  out.print("    This is ");
  out.print(this.getClass());
  out.println(", using the GET method");
  out.println("welcome,"+username+":"+password);
  out.println("  </BODY>");
  out.println("</HTML>");
  out.flush();
  out.close();
 }

 public void doPost(HttpServletRequest request, HttpServletResponse response)
   throws ServletException, IOException {
  doGet(request, response);
 }
 /**
  * Initialization of the servlet. <br>
  *
  * @throws ServletException if an error occurs
  */
 public void init() throws ServletException {
  // Put your code here
 }
}

  

------------------------------------------------------------------------------------------------------------------------------本娃的学习日记@lily园
原文地址:https://www.cnblogs.com/yanglicyfsdm/p/4362602.html