cookie自动登录java_5.servlet cookie自动登录的实例

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

33b231a1be179d6b0c64ab30e9e7c3e8.png  2.建一张登陆表格 index.jsp

String path = request.getContextPath();

String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";

%>

My JSP 'index.jsp' starting page
Username:
Password:
Autologin:

76f318c3f7364690487def6d64b90743.png

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

packagecom.amaker.servlet;importjava.io.IOException;importjava.io.PrintWriter;importjavax.servlet.ServletException;importjavax.servlet.http.Cookie;importjavax.servlet.http.HttpServlet;importjavax.servlet.http.HttpServletRequest;importjavax.servlet.http.HttpServletResponse;public class PrepareLogin extendsHttpServlet {/****/

private static final long serialVersionUID = -8306532188182712167L;/*** Constructor of the object.*/

publicPrepareLogin() {super();

}/*** Destruction of the servlet.
*/

public voiddestroy() {super.destroy(); //Just puts "destroy" string in log//Put your code here

}public voiddoGet(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

Cookie[] cs=request.getCookies();if(cs!=null&&cs.length>0){

for(int i=0;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 voiddoPost(HttpServletRequest request, HttpServletResponse response)throwsServletException, IOException {

doGet(request, response);

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

}

}

4.修改index.html中

Username:Password:

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.

*/

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(""-//W3C//DTD HTML 4.01 Transitional//EN\">");

out.println("");

out.println("

A Servlet");

out.println("

");

out.print(" This is ");

out.print(this.getClass());

out.println(", using the GET method");

out.println("welcome,"+username+":"+password);

out.println(" ");

out.println("");

out.flush();

out.close();

}

public void doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException {

doGet(request, response);

}

/**

* Initialization of the servlet.

*

* @throws ServletException if an error occurs

*/

public void init() throws ServletException {

// Put your code here

}

}

617f45dcf35115e23f53ea60ec3ff410.png

1f30fda22c75af01c830a3b36259fbaa.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值