欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java web实现自动登录功能

程序员文章站 2024-04-01 23:50:28
本文实例为大家分享了java web实现自动登录功能的具体代码,供大家参考,具体内容如下 主要思路就是:当用户访问网站的首页时,浏览器端会先检擦浏览器中存在的cookie...

本文实例为大家分享了java web实现自动登录功能的具体代码,供大家参考,具体内容如下

主要思路就是:当用户访问网站的首页时,浏览器端会先检擦浏览器中存在的cookie中是否又登录的用户的用户名,如果有,则直接跳转至用户登录好的界面,如果没有,则重定向至登录界面,在服务器端创建该用户登录的cookie,响应时,将创建的cookie返回至浏览器端保存。

一、用户访问首页时检查cookie是否存在。

package ahpudong.com;

import java.io.ioexception;
import java.io.printwriter;
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.cookie;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

@webservlet("/index.do")

public class index extends httpservlet {
 public index() {
  super();
 }
 public void dopost(httpservletrequest request, httpservletresponse response)
   throws servletexception, ioexception {
  cookie[] cookies=request.getcookies();
  if(cookies!=null){
   for(cookie cookie:cookies){
    string name=cookie.getname();
    string value=cookie.getvalue();
    if("name".equals(name) && "chendong".equals(value)){
     request.setattribute(name, value);
     request.getrequestdispatcher("user.view").forward(request, response);
     return;
    }
   } 
  }
   response.sendredirect("login.jsp");
 }
}

二、确定用户登录的cookie不存在,在手动登录的过程中创建用户登录cookie。

package ahpudong.com;

import java.io.ioexception;
import java.io.printwriter;
import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.cookie;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

@webservlet("/login.do")

public class login extends httpservlet {
 public login() {
  super();
 }
 public void dopost(httpservletrequest request, httpservletresponse response)
   throws servletexception, ioexception {
  response.setcontenttype("text/html");
  request.setcharacterencoding("utf-8");
  response.setcharacterencoding("utf-8");
  printwriter out = response.getwriter();
  string name=request.getparameter("name");
  string pwd=request.getparameter("pwd");
  string login=request.getparameter("auto");
  if("chendong".equals(name) && "123456".equals(pwd)){
   if("on".equals(login)){
    cookie cookie=new cookie("name","chendong");
    cookie.setmaxage(7*24*60*60);//有效期为一个星期
    response.addcookie(cookie);
    request.setattribute("name",name);
    request.getrequestdispatcher("user.view").forward(request, response);    
   }
  }else{
   response.sendredirect("login.jsp");
  }
 }
}

三、登录成功时的视图层

package ahpudong.com;

import java.io.ioexception;
import java.io.printwriter;

import javax.servlet.servletexception;
import javax.servlet.annotation.webservlet;
import javax.servlet.http.httpservlet;
import javax.servlet.http.httpservletrequest;
import javax.servlet.http.httpservletresponse;

@webservlet("/user.view")
public class user extends httpservlet {
 public user() {
  super();
 }
 public void doget(httpservletrequest request, httpservletresponse response)
   throws servletexception, ioexception {
  processrequest(request,response);

 }
 public void dopost(httpservletrequest request, httpservletresponse response)
   throws servletexception, ioexception {
  processrequest(request,response);
 }
 public void processrequest(httpservletrequest request,httpservletresponse response)
   throws servletexception, ioexception{
  response.setcharacterencoding("utf-8");
  printwriter out=response.getwriter();
  string name=(string) request.getattribute("name");
  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.println("<h1>"+"welcome "+name+" login"+"</h1>");
  out.println(" </body>");
  out.println("</html>");
  out.flush();
  out.close(); 
 }
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。