JAVA遇见HTML——JSP篇(JSP状态管理)








案例:Cookie在登录中的应用

URL编码与解码的工具类解决中文乱码的问题,这个工具类在java.net.*包里
编码:URLEncoder.encode(String s,String enc)//s:对哪个字符串进行编码,enc:用的字符集(例:utf-8)
解码:URLDecoder.decode(String s,String enc)//s:对哪个字符串进行解码,enc:用哪个字符集解码(例:utf-8)
login.jsp
<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head> <body>
<h1>用户登录</h1>
<hr>
<%
request.setCharacterEncoding("utf-8");
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 name="loginForm" 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="登录"/><input type="reset" value="取消"/></td>
</tr>
</table>
</form>
</body>
</html>
dologin.jsp
<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'dologin.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>登录成功</h1>
<hr>
<br>
<br>
<br>
<%
request.setCharacterEncoding("utf-8");
//首先判断用户是否选择了记住登录状态
String[] isUseCookies = request.getParameterValues("isUseCookie");
if(isUseCookies!=null&&isUseCookies.length>0)
{
//把用户名和密码保存在Cookie对象里面。
String username = URLEncoder.encode(request.getParameter("username"),"utf-8");
//使用URLEncoder解决无法在Cookie当中保存中文字符串问题
String password = URLEncoder.encode(request.getParameter("password"),"utf-8"); Cookie usernameCookie = new Cookie("username",username);
Cookie passwordCookie = new Cookie("password",password);
usernameCookie.setMaxAge(864000);
passwordCookie.setMaxAge(864000);//设置最大生存期限为10天
response.addCookie(usernameCookie);
response.addCookie(passwordCookie);
}
else
{//如果用户没有勾选记住用户名、密码的复选框,则把已经保存的原来的cookie设置失效
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>
user.jsp
<%@ page language="java" import="java.util.*,java.net.*" contentType="text/html; charset=utf-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>"> <title>My JSP 'users.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
--> </head> <body>
<h1>用户信息</h1>
<hr>
<%
request.setCharacterEncoding("utf-8");
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");
}
}
}
%>
<BR>
<BR>
<BR>
用户名:<%=username %><br>
密码:<%=password %><br>
</body>
</html>

JAVA遇见HTML——JSP篇(JSP状态管理)的更多相关文章
- JAVA遇见HTML——Servlet篇:应用MVC架构实现项目
java关键字“this”只能用在方法方法体内.当一个对象创建之后,java虚拟机就会给这个对象分配一个引用自身的指针,这个指针的名字就是this.只能在非静态方法中使用 package servle ...
- JAVA遇见HTML——Servlet篇:Servlet基础
代码实现: HelloServlet package servlet; import java.io.IOException; import java.io.PrintWriter; import j ...
- JAVA遇见HTML——JSP篇:JSP内置对象(上)
JSP九大内置对象 JSP内置对象是Web容器创建的一组对象,不使用new关键就可以使用的内置对象. <% int[] value={60,70,80}; for(int i:value){ o ...
- 当Java遇见了Html--Servlet篇
###一.什么是servlet servlet是在服务器上运行的小程序.一个servlet就是一个 java类,并且通过"请求-响应"编程模型来访问的这个驻留在服务器内存里的程序. ...
- Java学习之Servlet篇
<JAVA遇见HTML——Servlet篇> Servlet 生命周期:Servlet 加载--->实例化--->服务--->销毁. init():在Servlet的生命 ...
- Java遇见HTML——JSP篇之JSP状态管理
一.http协议的无状态 无状态性是指,当浏览器发送请求给服务器的时候,服务器响应客户端请求.但是当同一个浏览器再次发送请求给服务器的时候,服务器并不知道他就是刚才的那个浏览器.简单的说,就是服务器不 ...
- JAVA遇见HTML——JSP篇(JavaBeans)
1.像使用普通java类一样,创建javabean实例,利用构造方法创建实例 跟表单关联,“*”表示根据名称来进行匹配,就是根据表单所提交过来的参数的名字和Javabean当中的属性名字来进行一一匹配 ...
- Java遇见HTML——JSP篇之商品浏览记录的实现
一.项目总体介绍 使用Cookie实现商品浏览记录. 要实现这个程序采取的是Model1(Jsp+JavaBean)架构实现,具体步骤: 首先要有个数据库,商品表,操作数据库的一个类DBHelper类 ...
- Java遇见HTML——JSP篇之JSP指令与动作元素
一.include指令(如:<%@include file="..."%> ) 示例: Date.jsp <%@page import="java.te ...
随机推荐
- [学习笔记] Blender 常用工具栏,选择及游标
Shift + A 创建物体 选择工具: 默认是框选 shift 鼠标左键 加选, 再次可减选 游标 默认情况下游标在世界中心.创建新物体时,会自动被创建在游标的位置.可以随意改变游标的位置,便于建模 ...
- v-bind 绑定属性
与mustache相区别,他是对内容(content内部)进行修改的.v-bind的语法糖写法是 : v-bind 动态绑定class属性:v-bind:class="对象名" ...
- Jmeter CSV操作
统计行号列号 import java.io.BufferedReader; import java.io.FileReader; import java.io.File; print("== ...
- Scala调用Kafka的生产者和消费者Demo,以及一些配置参数整理
kafka简介 Kafka是apache开源的一款用Scala编写的消息队列中间件,具有高吞吐量,低延时等特性. Kafka对消息保存时根据Topic进行归类,发送消息者称为Producer,消息接受 ...
- XDebug调试
安装 访问Xdebug 点击download 找到RELEASES,点击 custom installation instructions. 在白色框框内填入phpinfo()出来的源码 点击Anal ...
- python学习-26 函数作用域
举例说明: 1. name = 'john' def foo(): name = 'xiaomming' def bar(): print(name) return bar a=foo() print ...
- shell习题第23题:检测网卡流量
[题目要求] 写一个脚本,检测网卡流量并记录到日志,需要按照如下格式并一分钟统计一次(只需统计外网网卡,网卡名称eth0) 2019-06-07 1:11 eth0 input: 1000bps et ...
- MogileFS与spring结合
一.通过Maven添加MogileFS的Java客户端驱动包 <dependency> <groupId>fm.last</groupId> <artifac ...
- (三十)JSP标签之自定义标签
创建一个类,引入外部jsp-api.jar包(在tomcat 下lib包里有),这个类继承SimpleTagSupport 重写doTag()方法. jspprojec包下的helloTag类: 1 ...
- SOLID Principles
Intention: more understandable, easier to maintain and easier to extend.(通过良好的设计使得代码easy and simple, ...