您的位置:首页 > Web前端 > JavaScript

JSP(二)

2016-05-10 21:42 477 查看
==========================cookie=================================
Web服务器保存在【客户端】的一些系列【文本】信息

作用:
1.对特定对象的追踪
2.统计网页浏览次数
3.简化登录

安全性:
容易信息泄露

语法:
1.导包
import="javax.servlet.http.Cookie" 【servlet是JSP的真身】
2.创建Cookie
Cookie newCookie=new Cookie(String key, Object value);
3.写入Cookie:创建Cookie,★使用response的addCookie方法保存Cookie★
response.addCookie(newCookie)
范例1:

<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@page import="javax.servlet.http.Cookie"%>
<!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>演示Cookie的基本用法</title>
</head>
<body>
<%
//创建Cookie对象    Cookie 中文 jar包
Cookie cookie = new Cookie("name","xijingp");
//设置Cookie的生命周期为1小时
cookie.setMaxAge(60*60);
//将Cookie对象写入客户端
response.addCookie(cookie);
%>
</body>
</html>


范例2:
login.jsp

<%@ 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>
<%
//获得Cookie,以数组方式保存
Cookie[] cookies = request.getCookies();
String username = "";
String password = "";
//循环遍历数组,得到key为"username"和"password"
for(Cookie cookie:cookies){
if(cookie.getName().equals("userInfo")){//判断cookie的名称
username = cookie.getValue().split("&")[0];
password = cookie.getValue().split("&")[1];
//实现自动登:要记得把用户名和密码保存到request对象中
//request.getRequestDispatcher("doLogin.jsp");
}
}
%>
<a href="xx.jsp?ReturnUrl=http%3A%2F%2><%=request.getRequestURI()%>">当前页连接<%=request.getRequestURI()%></a>
<form action="doLogin.jsp" method="post">
用户名:<input type="text" name="txtName" value="<%=username %>"/><br/>
密  码:<input type="password" name="txtPwd" value="<%=password %>"/><br/>
<input type="checkbox" value="1" name="savePass"/>记住密码<br/>
<button type="submit" value="Login">登录</button>
</form>
</body>
</html>


doLogin.jsp

<%@ 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>
<%
String name = request.getParameter("txtName");
String pass = request.getParameter("txtPwd");

String[] savePass = request.getParameterValues("savePass");
if(null ==savePass || savePass.length == 0){
response.sendRedirect("login.jsp");
return;
}
//省略判断
//构建Cookie
//name + "&" + pass
String value = String.format("%s&%s", name, pass);
Cookie cookie = new Cookie("userInfo",value);
cookie.setMaxAge(60*5);
response.addCookie(cookie);
%>
</body>
</html>


===========================session===============================
会话:浏览器和服务器之间的通话,包含浏览器和服务器之间多次的请求、响应过程
随着浏览器的关闭,而结束
session(内存)对象用来存储有关用户会话的所有信息【服务器】

session和窗口的关系
只要浏览器不关,session始终是一个,不管存在多少窗口
如果重开另一个浏览器,session就是另外一个

===========================Cookie和session区别===============================
1.session:服务器端保存用户信息 Cookie:客户端保存用户信息
2.session:保存对象 Cookie:保存字符串
3.session:随会话结束而关闭 Cookie:长期保存在客户端
4.session:重要信息 Cookie:不重要

===========================application===============================
”全局变量“,用于实现用户之间的数据共享

==========================include指令==================================
将共性的内容写入一个单独的文件中,通过include指令引用该文件
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: