用户登录中Cookie的简单应用
来源:互联网 发布:java web项目面试题 编辑:程序博客网 时间:2024/05/16 10:06
总共分为三个界面,第一个界面:登录界面
<%@ page language="java" contentType="text/html; charset=utf-8" import="java.net.*" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><h1> 用户登录界面</h1><% request.setCharacterEncoding("utf-8"); String username=""; String password=""; Cookie[] cookie= request.getCookies(); if(cookie!=null&&cookie.length>0){ for(Cookie c:cookie){ if(c.getName().equals("username")){ username=URLDecoder.decode(c.getValue(),"utf-8"); } if(c.getName().equals("password")){ password=URLDecoder.decode(c.getValue(), "utf-8"); } } }%><form action="dologin.jsp" method="post"> <table> <tr> <td>用户名:</td> <td><input type="text" name="username" value="<%=username%>"/></td> </tr> <tr> <td>密码:</td> <td><input type="password" name="password" value="<%=password%>"/></td> </tr> <tr> <td colspan="2"> <input type="checkbox" name="isuseCookie" checked="checked" /> 十天内免密码登录</td> </tr> <tr> <td colspan="2" align="center"><input type="submit" value="登录"/></td> </tr> </table></form></body></html>第二个界面:用户请求处理界面
<%@page import="java.net.URLEncoder"%><%@ page language="java" contentType="text/html; charset=utf-8" import="java.net.*" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><h1>用户登录成功</h1><br> <% request.setCharacterEncoding("utf-8"); String[] isuseCookie=request.getParameterValues("isuseCookie"); if(isuseCookie!=null&&isuseCookie.length>0){ //保存用户信息到Cookie
//特别说明:防止中文乱码,URLEncodeer.encode(String "字符集"); String username=URLEncoder.encode(request.getParameter("username"),"utf-8"); String password=URLEncoder.encode(request.getParameter("password"),"utf-8"); Cookie usernameCookie= new Cookie("username",username); Cookie passwordCookie= new Cookie("password",password); //设置Cookie保存的时间,单位秒 usernameCookie.setMaxAge(864000); passwordCookie.setMaxAge(864000); //添加到Cookie response.addCookie(usernameCookie); response.addCookie(passwordCookie); } else{ //需要清空Cookie Cookie[] cookie =request.getCookies(); if(cookie!=null&&cookie.length>0){ for(Cookie c:cookie){ if(c.getName().equals("username")||c.getName().equals("password")){ c.setMaxAge(0);//设置Cookie失效 response.addCookie(c);//重新保存Cookie } } } } %> <br><br><a href="users.jsp">用户信息</a> </body></html>
第三个界面:用户界面
<%@page import="java.net.URLDecoder"%><%@ page language="java" contentType="text/html; charset=utf-8" pageEncoding="utf-8"%><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>Insert title here</title></head><body><h1>用户信息输出</h1><% String username=""; String password=""; Cookie[] cookie= request.getCookies(); if(cookie!=null&&cookie.length>0){ for(Cookie c:cookie){ if(c.getName().equals("username")){ username= URLDecoder.decode(c.getValue(),"utf-8"); } if(c.getName().equals("password")){ password=URLDecoder.decode(c.getValue(), "utf-8"); } } }%> 用户名为:<%= username %> 密码:<%= password %></body></html>
可以记住用户十天的登录信息,十天内面密码登录
0 0
- 用户登录中Cookie的简单应用
- Cookie保存用户登录账号密码的应用
- 用户登录安全性的简单实例分析(Cookie、加密)
- 用户登录安全性的简单实例分析(Cookie、加密)
- 用户登录安全性的简单实例分析(Cookie、加密)
- 用户登录安全性的简单实例分析(Cookie、加密)
- 用户登录安全性的简单实例分析(Cookie、加密)
- JSP中Cookie在登录功能中的简单应用
- Web应用中防止用户重复登录的简单实现方法
- 简单cookie的应用
- 简单的用户登录
- Perl CGI 一个简单的用户登录验证应用
- Perl CGI 一个简单的用户登录验证应用
- Struts框架的简单应用到用户登录
- java中cookie的操作(通过cookie实现简单的单点登录)
- 使用cookie保存用户的登录名
- jsp Cookie记住用户的登录状态
- 轻松拿到登录用户的cookie
- Java中的Callback 回调函数
- 反射-框架、工具类
- chrom浏览器模拟手机访问
- 用面向对象思维理解.NET委托:函数是对象、委托是函数对象的类型
- 网络编程--TCP-IP--2
- 用户登录中Cookie的简单应用
- 新的开始
- SpringMVC 错误分析:@Autowired Could not autowire field
- 关于Xcode 的一些调试技巧
- 欢迎使用CSDN-markdown编辑器
- java 泛型
- itemSelectedListener与itemClickListener的区别
- LeetCode -- Convert SortedList To BST
- Interface与abstract类的区别