JSP中Cookie在登录功能中的简单应用
来源:互联网 发布:赤焰狂魔莫小贝 知乎 编辑:程序博客网 时间:2024/05/29 02:43
代码如下
login.jsp
- <%@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[] cookies = request.getCookies();
- if(cookies!=null &&cookies.length>0) {
- for(Cookie c:cookies) {
- 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"/>10天记住登录
- </td>
- </tr>
- <tr>
- <td clospan="2">
- <input type="submit" value="登录"/>
- </td>
- </tr>
- </table>
- </form>
- </body>
- </html>
doLogin.jsp
- <%@page import="java.net.URLEncoder"%>
- <%@ 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>
- <br />
- <%
- request.setCharacterEncoding("utf-8");
- //首先判断用户是否选择了记住登录选项
- String[] isUseCookies = request.getParameterValues("isUseCookie");
- if(isUseCookies!=null &&isUseCookies.length>0) {
- //把用户名和密码保存在Cookie里面
- //使用URLEncoder解决无法在Cookie中保存中文字符串问题
- 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);
- response.addCookie(usernameCookie);
- response.addCookie(passwordCookie);
- usernameCookie.setMaxAge(864000);//设置最大生存期限为10天
- passwordCookie.setMaxAge(864000);
- } else {
- Cookie[] cookies = request.getCookies();
- if(cookies!=null && cookies.length>0) {
- for(Cookie c:cookies) {
- if(c.getName().equals("username")||c.getName().equals("password")) {
- c.setMaxAge(0);//设置Cookie失效
- response.addCookie(c);//重新保存
- }
- }
- }
- }
- %>
- <a href="users.jsp" target="blank">查看用户信息</a>
- </body>
- </html>
users.jsp
- <%@page import="java.net.URLDecoder"%>
- <%@page import="org.apache.tomcat.util.http.Cookies"%>
- <%@ 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>
- <hr />
- <%
- String username = "";
- String password = "";
- Cookie[] cookies = request.getCookies();
- if(cookies!=null &&cookies.length>0) {
- for(Cookie c:cookies) {
- 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 %> <br />
- 密码: <%=password %> <br />
- </body>
- </html>
本文链接:https://liuyanzhao.com/4833.html
阅读全文
0 0
- JSP中Cookie在登录功能中的简单应用
- JSP的Cookie在登录中的应用
- 用户登录中Cookie的简单应用
- JSP中cookie 的应用
- 在JSP中使用Session制作简单的登录模块
- 在jsp中设置cookie
- 在web中实现简单的注册,登录功能
- 简单的jsp登录页面跳转功能
- Cookie在登录中的小例子
- jsp cookie 用户登录
- JSP(4)—Cookie创建及简单案例(自动登录)
- cookie在web应用中实现上次登录时间以及上次访问时间 小案例理解cookie
- Java使用cookie实现简单的单点登录功能
- cookie登录功能实现
- Java网站中的自动登录功能如何实现 Session Cookie
- 关于登录中的记住我的功能(cookie实例)
- 在JSP中如何设置/删除COOKIE?
- JavaBean在JSP中的应用
- PowerDesigner导出表到word
- ftpclient一些用法
- 51Nod 1349 最大值
- OpenTSDB部署
- 【转】托管代码和非托管代码的区别
- JSP中Cookie在登录功能中的简单应用
- 集群之 -- zookeeper 集群安装及配置
- mysql union,union all的优化
- 简单的Javaweb生成验证码实例
- lucene源码-创建IndexReader--2
- Java
- 巩固C/C++
- Spring Boot教程(四)Spring Boot 连接MySql数据库
- 分析函数-STDDEV,STDDEV_SAMP ,STDDEV_POP,VAR_POP,VAR_SAMP,VARIANCE