jsp4 Cookie
2016-05-04 17:37
477 查看
(1)
(2)Session----Coolie(3)
(4)
cookie保存index.jsp
<%@pagelanguage="java"import="java.util.*,java.net.*"contentType="text/html;charset=UTF-8"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>MyJSP'index.jsp'startingpage</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
</head>
<body>
<h1>用户登录</h1>
<hr>
<%
request.setCharacterEncoding("utf-8");
Stringusername="";
Stringpassword="";
Cookie[]cookies=request.getCookies();
if(cookies!=null&&cookies.length>0){
for(Cookiec: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");
}
}
}
%>
<formaction="dologin.jsp"method="post"name="loginForm">
<table>
<tr><td>用户名:</td><td><inputtype="text"name="username"value="<%=username%>"/></td></tr>
<tr><td>密码:</td><td><input type="password"name="password"value="<%=password%>"/></td></tr>
<tr><tdcolspan="2"><inputtype="checkbox"name="isUseCookie"checked="checkd"/>十天内记住登陆状态</td></tr>
<tr><tdcolspan="2"aligin="center"><inputtype="submit"value="登录"></td></tr>
</table>
</form>
</body>
</html>
[/code]dologin.jsp
<%@pagelanguage="java"import="java.util.*,java.net.*"contentType="text/html;charset=UTF-8"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>MyJSP'dologin.jsp'startingpage</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
</head>
<body>
<h1>登陆成功</h1><br><br><br><br>
<%
request.setCharacterEncoding("utf-8");//防止中文乱码
//首先判断用户是否选择了记录登陆状态
String[]isUseCookie=request.getParameterValues("isUseCookie");//返回包含参数name的所有值的数组
if(isUseCookie!=null&&isUseCookie.length>0){
//吧用户名和密码保存在Cookie对象里面
Stringusername=URLEncoder.encode(request.getParameter("username"),"utf-8");//返回name指定函数的参数值
//URLEncoder.encode(s,enc)防止中文乱码。。短码
Stringpassword=URLEncoder.encode(request.getParameter("password"),"utf-8");
CookieusernameCookie=newCookie("username",username);//创建cookie对象
CookiepasswordCookie=newCookie("password",password);
usernameCookie.setMaxAge(8640000);//设置最大生存期限为10天(有效期)
passwordCookie.setMaxAge(8640000);
response.addCookie(usernameCookie);//加入cookie对象
response.addCookie(passwordCookie);
}else{
Cookie[]cookies=request.getCookies();//读取cookie对象
if(cookies!=null&&cookies.length>0){
for(Cookiec:cookies){
if(c.getName().equals("username")||c.getName().equals("password")){//(名称)
c.setMaxAge(0);//设置cookie失效
response.addCookie(c);//重新保存
}
}
}
}
%>
<ahref="users.jsp"target="_blank">查看用户信息</a>
</body>
</html>
[/code]users.jsp
<%@pagelanguage="java"import="java.util.*,java.net.*"
contentType="text/html;charset=UTF-8"%>
<%
Stringpath=request.getContextPath();
StringbasePath=request.getScheme()+"://"
+request.getServerName()+":"+request.getServerPort()
+path+"/";
%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html>
<head>
<basehref="<%=basePath%>">
<title>MyJSP'users.jsp'startingpage</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
</head>
<body>
<h1>用户信息</h1>
<br>
<br>
<br>
<br>
<%
request.setCharacterEncoding("utf-8");
Stringusername="";
Stringpassword="";
Cookie[]cookies=request.getCookies();
if(cookies!=null&&cookies.length>0){
for(Cookiec:cookies){
if(c.getName().equals("username")){//(名称)
//解码用URLDecoder.decode(s,enc)
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>
[/code](5)Session-Cookie
相关文章推荐
- 七牛上传图片(javascript)
- JS 中如何判断undefined与null
- js 事件机制
- AjaxFileupload.js实现多文件上传
- javascript 事件委托 event delegation
- 使用Jsoup抓取网页数据
- 手把手教你如何开发chrome插件
- jsp ,servlet 中文乱码问题
- setAttribute 和 getAttribute 的设值和获取方法
- 基于JSR-356实现的Tyrus WebSocket框架的消息传递机制初步了解
- JavaScript中Eval()函数的使用
- 一分钟理解js闭包
- js产生随机数并加入数组
- js 简单的下拉加载更多
- 优化js的执行
- JavaScript -- 面向对象的简单分析
- 学JavaScript七大注意事项【必看】
- jsp ,servlet中session ,Cookie 相关知识
- 优雅的图片翻转实现方式rollover.js
- jsonp方式解决跨域共享问题