欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

java web实现自动登录功能

时间:2021-01-14 11:13:45|栏目:JAVA代码|点击:

本文实例为大家分享了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(); 
 }
}

上一篇:Java实现动态代理

栏    目:JAVA代码

下一篇:Java经典排序算法之插入排序

本文标题:java web实现自动登录功能

本文地址:http://www.codeinn.net/misctech/44985.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有