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:
范例2:
login.jsp
doLogin.jsp
===========================session===============================
会话:浏览器和服务器之间的通话,包含浏览器和服务器之间多次的请求、响应过程
随着浏览器的关闭,而结束
session(内存)对象用来存储有关用户会话的所有信息【服务器】
session和窗口的关系
只要浏览器不关,session始终是一个,不管存在多少窗口
如果重开另一个浏览器,session就是另外一个
===========================Cookie和session区别===============================
1.session:服务器端保存用户信息 Cookie:客户端保存用户信息
2.session:保存对象 Cookie:保存字符串
3.session:随会话结束而关闭 Cookie:长期保存在客户端
4.session:重要信息 Cookie:不重要
===========================application===============================
”全局变量“,用于实现用户之间的数据共享
==========================include指令==================================
将共性的内容写入一个单独的文件中,通过include指令引用该文件
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指令引用该文件
相关文章推荐
- JSP
- js中去除空格
- JSP 原理
- JSTL
- 深入理解JavaScript的闭包特性 如何给循环中的对象添加事件
- JavaScript错误和异常
- 解析JS中this关键字
- 说说JSON和JSONP,也许你会豁然开朗
- js实现继承的6种方式 寄生继承没弄懂
- 韩顺平细说jsp购物车项目--用户登录及验证
- 使用JavaScript实现ajax
- 全端Web开发 使用JavaScript与Java 阅读笔记
- 三种js数组去重的方法
- bzoj 1452: [JSOI2009]Count
- jsp与FCKeditor配置使用说明
- cojs 简单的数位DP 题解报告
- 【JAVAScript】js为日期框设置默认时间
- JavaScript的计时器的工作原理(setTimeout/setInterval)
- 记录js错误:Uncaught SyntaxError: Unexpected token <
- Realm-json遇到的问题