Servlet使用cookie来自动登录
来源:互联网 发布:非诚勿扰李纳的淘宝店 编辑:程序博客网 时间:2024/06/16 22:34
简述一下过程,文件命名挺乱的,说一下访问流程
1.首先在浏览器中访问LoginCookie这个Servlet,当访问时这个Servlet首先会判断一下浏览器中是否有cookie,若是存在则对其进行验证,验证通过再将其保存到session中,进行跳转到shopping.jsp这个文件中,否则,login.jsp页面中进行登录;
2.假如没有cookie,它会跳转到login.jsp页面中让你登录,表单会提交到Login.java这个Servlet中,它会对数据进行验证,若验证通过,则新建cookie,并将用户名和密码保存到浏览器的cookie中,直到下一次访问,再进行1;
Login.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>login</title>
</head>
<body>
<form action="<%=request.getContextPath()%>/login" align="center" method="post">
姓名:<input name="userName" type="text" >
<br>
密码:<input name="userPwd" type="password">
<br>
不保存用户名:<input type="radio" name="ch" value="0" checked="checked"><br>
保存用户名:<input type="radio" name="ch" value="60">一分钟
<input type="radio" name="ch" value="3600">一小时
<input value="提交" type="submit">
</form>
</body>
</html>
Login.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;
import javax.servlet.http.HttpSession;
public class Login extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
HttpSession session = request.getSession();
String ch = request.getParameter("ch");
int flag = Integer.parseInt(ch);
String userName = request.getParameter("userName");
String userPwd = request.getParameter("userPwd");
if ("admin".equals(userName)&&"123".equals(userPwd)) {
if(flag>0){
Cookie c1 = new Cookie("userName", userName);
Cookie c2 = new Cookie("userPwd", userPwd);
c1.setMaxAge(flag);
c2.setMaxAge(flag);
response.addCookie(c1);
response.addCookie(c2);
}
session.setAttribute("userName", userName);
response.sendRedirect(request.getContextPath()+"/shopping.jsp");
}else{
response.sendRedirect(request.getContextPath()+"/login.jsp");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
LoginCookie.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;
import javax.servlet.http.HttpSession;
public class LoginCookie extends HttpServlet {
String userName;
String userPwd;
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setCharacterEncoding("utf-8");
HttpSession session = request.getSession();
Cookie[] cookies = request.getCookies();
if(cookies!=null){
for(Cookie cookie :cookies){
if("userName".equals(cookie.getName())){
userName = cookie.getValue();
}else if("userPwd".equals(cookie.getName())){
userPwd = cookie.getValue();
}
}
if ("admin".equals(userName)&&"123".equals(userPwd)) {
session.setAttribute("userName", userName);
response.sendRedirect(request.getContextPath()+"/shopping.jsp");
}else{
response.sendRedirect(request.getContextPath()+"/login.jsp");
}
}else{
response.sendRedirect(request.getContextPath()+"/login.jsp");
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
- Servlet使用cookie来自动登录
- Servlet cookie自动登录
- 使用auto_ptr来自动释放内存
- 使用主题来自动包装控件样式
- 使用kickstart来自动安装Linux操作系统
- servlet cookie的使用
- Servlet中用Cookie实现自动登录
- Cookie 的使用(Servlet)
- 在Eclipse中使用Ant来自动编译J2me程序
- 使用winrar来自动备份domino服务器数据
- 使用TFS来自动部署站点和Window Service
- 使用T4 模板来自动生成WCF 代理类 代码
- 使用Ajax来自动帮用户填写信息
- 深度学习第一课:使用autograd来自动求导
- 使用Cookie进行登录验证
- php 使用cookie判断登录
- 使用Cookie记录登录信息
- NCrawler中使用Cookie登录
- Eclipse中如何更改Java编辑界面和Jsp编辑界面的字体大小?(图解步骤)
- Java专家之路(二)---数据访问和数据持久化框架的总结
- html的基本单词可能还有没写到的
- DLL学习笔记
- nrf51822与nrf24l01之间的无线通讯
- Servlet使用cookie来自动登录
- 字符串广义表和多叉树、普通树以及多叉树、普通树和广义表链表表示的相互转换
- 常用的数量统计量的计算及统计意义
- 增1和减1运算符
- 爬取邮箱,出现带有文字的处理
- 使用OpenCV自带的级联分类器进行目标检测
- 欢迎使用CSDN-markdown编辑器
- 链式堆栈
- 窗口界面