会话:
会话可简单理解为:用户开一个浏览器,点击多个超链接,访问服务器多个web资源,然后关闭浏览器,整个过程称之为一个会话。
会话主要解决服务端如何保存每个客户端对应的私有信息。
会话主要有二种:Cookie技术[客户端技术] Session技术
创建cookie Cookie cookie=new Cookie(String name,String value);
向客户端发送cookie response.addCookie(Cookie cookie);
注意:Cookie中不能存储中文
设置持久化时间:cookie.setMaxAge(int seconds); 时间是秒
设置cookie的携带路径
cookie.setPath(“/WEB16/sendCookie”) 访问sendCookie资源时才携带这个cookie
cookie.setPath(“/WEB16”) 访问WEB16下的任何资源时都携带这个cookie
cookie.setPath(“/”) 访问服务器下的所有资源时都携带这个cookie
如果不设置携带路径
如:访问localhost:8080/WEB16/demo/index.jsp 红字的路径下都会携带
删除客户端的cookie 使用同名同路径的持久化时间为0的cookie覆盖
获得客户端携带的cookie的数据 Cookie [ ] cookies=request.getCookies();
单位是分钟
cookie客户端技术,只能存字符串,HttpSession服务器端技术,它可以存对象
setMaxInactiveInterval(int interval) 设置session的存活时间
invalidate() 使此会话无效
JSESSIONID的持久化
Servlet 获取表单数据 调用业务逻辑 分发转向
JSP
为什么说jsp就是servlet:
如果是JSP资源,JSP引擎会将其翻译成Servlet资源,传入相关的对象,如果是静态资源,以out.write()形式输出,如果是动态资源,以out.print()形式输出
Html注释:<!--注释内容-->
Java注释://单行注释 /* 多行注释 */
Jsp注释:<%--注释内容--%>
Jsp语法:
JSP模版元素
JSP脚本表达式
JSP脚本片断
JSP声明
JSP注释
JSP指令
JSP标签
JSP内置对象
JSP页面中的HTML内容称之为JSP模版元素。
JSP脚本表达式(expression)用于将程序数据输出到客户端
<%= 变量或表达式 %>
JSP脚本片断用于在JSP页面中编写多行Java代码
<%
多行java代码
%>
JSP声明 <%! java代码 %>
JSP声明的变量或方法,会成为Servlet的实例变量或实例方法或普通方法
JSP注释 <%-- 注释的内容 --%>
JSP指令有三类:
a)page指令 b)include 指令 c)tablib指令
<%@ 指令 属性名="值" %>
如:<%@ page errorPage=”error.jsp”%> "当前jsp页面出错后,转发到的目标页面"
如果想在目标页面显示出错原因:<%@ page isErrorPage=”true”%>
Jsp中在web.xml中配置页面错误的提示信息
<error-page>
<error-code>500</error-code>
<location>/500.jsp</location>
</error-page>
include 指令:静态包含:<%@include file=”head.jsp”%>
taglib指令用于在JSP页面中导入标签库
<%@ taglib uri=”标签库地址” prefix=”前缀” %>
Jsp的9个内置/隐式对象
request response session application pageContext page out config exception:
jsp四大域对象:
pageContext request session application
范围从小到大
Servlet JSP
pageContext(称之为page域)
request (称之为request域)
session (称之为session域)
servletContext (称之为application域)
pageContext域可以向其他域存取数据
pageContext域可以创建其它8个隐式对象
如:pageContext.getRequest() pageContext.getSession()
Jsp常用的6个标签/动作
<jsp:include>:动态包含
<jsp:forward>:请求转发
<jsp:param>:设置请求参数
<jsp:useBean>:创建一个对象
<jsp:setProperty>:给对象赋值
<jsp:getProperty>:取出对象的值
Jsp中动态包含和静态包含的区别
EL表达式:${}
EL从域中取出数据
获得普通字符串
获得User对象的值
获得List<User>的值
<!-- 使用el表达式 全域查找 -->
${company }
${user.name }
${list[1].name}
获得表单的参数 ${param.username} 一个jsp表单传数据到另一个jsp页面
获得当前目录的虚拟目录:${pageContext.request.contextPath}== request.getContextPath()
<!-- el可以执行表达式运算 -->
${1+1 }
${1==1?true:false }
<!-- empty 判定某个对象是否是null 是null返回true -->
${empty list}
Jstl标签库(引入的前提要记得导包)
使用jstl实现jsp页面中的逻辑处理,如判断.循环等.
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
常用标签 <c:if test=”${}”> <c:forEach>
普通循环(模拟for循环)
迭带器(模拟增强for循环)
items表示一个集合或数组 var代表数组或集合中的每一个元素
varStatus=”vs” 循环的状态
${vs.count} 循环的次数
<!-- 用户没有登录 -->
<c:if test="${empty user }">
<!-- 用户已经登录 -->
<c:if test="${!empty user }">
问题:
1.cookie的创建和发送
2.删除cookie(如:删除问题1的cookie)
3.获得cookie
4.记录用户的上次访问时间
5.获取session和它的JSESSIONID
6.JSESSIONID的持久化 Answer6Session Answer7Session 访问Answer6Session 然后访问Answer7Session 关闭浏览器 然后在访问Answer7Session 不会出现null
6.1新增验证码的校验(登录)
7.pageContext可以分别向request session application域中存数据
8.使用el表达式 分别获得 普通字符串 User对象的值 List<User>的值 el表达式
9.使用jstl标签库来完成 <c:if test="">
10.使用jstl标签库来完成 <c:forEach> 主要有两种方式遍历 普通循环
11.使用jstl标签库来完成 <c:forEach> 增强for循环
遍历List<String>的值 遍历List<User>的值 遍历Map<String,String>的值 遍历Map<String,User>的值
1.cookie的创建和发送
public class Answer1 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// 创建Cookie对象
Cookie cookie=new Cookie("name","zhangsan");
//1.1 为cookie设置持久化时间 ---- cookie信息在硬盘上保存的时间
cookie.setMaxAge(10*60);//10分钟 ---- 时间设置为0代表删除该cookie
//1.2 为cookie设置携带的路径
//cookie.setPath("/WEB16/sendCookie");//访问sendCookie资源时才携带这个cookie
cookie.setPath("/JavaWeb_day3");//访问JavaWeb_day3下的任何资源时都携带这个cookie
//cookie.setPath("/");//访问服务器下的所有的资源都携带这个cookie
// 将Cookie中存储的信息发送到客户端
response.addCookie(cookie);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
2.删除cookie(如:删除问题1的cookie)
public class Answer2 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//删除cookie
Cookie cookie=new Cookie("name","zhangsan");
cookie.setMaxAge(0);
cookie.setPath("/JavaWeb_day3");//访问JavaWeb_day3下的任何资源时都携带这个cookie
//将Cookie中存储的信息发送到客户端
response.addCookie(cookie);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
3.获得cookie
public class Answer3 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
Cookie[] cookies=request.getCookies();
if(cookies!=null){
for(Cookie cookie:cookies){
String cookieName=cookie.getName();
//这里主要是通过cookie的键来判断是否是自己想要的
if(cookieName.equals("name")){
String cookieValue=cookie.getValue();
System.out.println(cookieValue);
}
}
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
4.记录用户的上次访问时间 cookie
public class Answer4 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获得当前时间
Date date1=new Date();
SimpleDateFormat adf=new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
String currentTime=adf.format(date1);
//1、创建Cookie 记录当前的最新的访问时间
Cookie cookie=new Cookie("lastAccessTime",currentTime);
cookie.setMaxAge(60*60*24*365);
response.addCookie(cookie);
//2、获得客户端携带cookie ---- lastAccessTime
String lastAccessTime=null;
Cookie[] cookies=request.getCookies();
if(cookies!=null){
for(Cookie co:cookies){
if("lastAccessTime".equals(co.getName())){
lastAccessTime=co.getValue();
}
}
}
response.setContentType("text/html;charset=utf-8");
if(lastAccessTime==null){
response.getWriter().write("您是第一次访问本网站");
}else{
response.getWriter().write("您上次访问本网站的时间是:"+lastAccessTime);
}
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
5.获取session和它的JSESSIONID
public class Answer5 extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//获取session
HttpSession session=request.getSession();
// 获取JSESSIONID
String value=session.getId();
response.getWriter().write("JSESSIONID:"+value);
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
6.JSESSIONID的持久化
public class Answer6Session extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession();
String jsessionId=session.getId();
//手动创建一个存储JSESSIONID的Cookie 为该cookie设置持久化时间
Cookie coo=new Cookie("JSESSIONID",jsessionId);
coo.setMaxAge(60*10);
coo.setPath("JavaWeb_day3");
response.addCookie(coo);
session.setAttribute("name", "zhangsan");
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
public class Answer7Session extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
HttpSession session=request.getSession();
Object obj=session.getAttribute("name");
response.getWriter().write(String.valueOf(obj));
}
public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doGet(request, response);
}
}
6.1登录校验验证码
前提准备,在web-inf下面放入new_words.txt,并且使用生成验证码的servlet
这里可以删除session中存储的验证码 目的是让验证码只有一次是有效的
session.removeAttribute("checkCode_session"); 记得判断不为空的情况
//验证码校验
//获得页面输入的验证
String checkCode_client=request.getParameter("checkCode");
//获得生成图片的文字的验证码 生成的验证码的Servlet里面直接获取
String checkCode_session=(String) request.getSession().getAttribute("checkcode_session");
//比对页面和生成图片的文字验证码是否一致
if(!checkCode_session.equals(checkCode_client)){
request.setAttribute("loginInfo","您的验证码不正确");
request.getRequestDispatcher("/login.jsp").forward(request,response);
return;
}else{
//获得页面的用户名和密码进行数据库的校验
}
7.pageContext可以分别向request session application域中存数据
<%@ 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>
<!-- 分别向request域 session域 application域中存数据 -->
<%
pageContext.setAttribute("name","zhangsan",PageContext.REQUEST_SCOPE);
pageContext.setAttribute("name","lisi",PageContext.SESSION_SCOPE);
pageContext.setAttribute("name","wangwu",PageContext.APPLICATION_SCOPE);
%>
<!-- 分别取出request域 session域 application域中存的数据 -->
<%=pageContext.getAttribute("name",PageContext.REQUEST_SCOPE)%>
<%=pageContext.getAttribute("name",PageContext.SESSION_SCOPE)%>
<%=pageContext.getAttribute("name",PageContext.APPLICATION_SCOPE)%>
<!-- 自动从 page request session application域中找值 -->
<%=pageContext.findAttribute("name")%>
</body>
</html>
8.使用el表达式 分别获得 普通字符串 User对象的值 List<User>的值 el表达式
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ page import="java.util.*"%>
<%@ page import="cn.itcast.domain.*"%>
<!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>
<!-- 模拟域中的数据 -->
<%
//存储字符串
request.setAttribute("company","菜鸟起飞");
//存储一个对象
User user=new User();
user.setId(1);
user.setName("郭叶兵");
user.setPassword("123");
session.setAttribute("user",user);
//存储一个集合
List<User> list=new ArrayList<User>();
User user1=new User();
user1.setId(11);
user1.setName("郭叶兵1");
user1.setPassword("1231");
User user2=new User();
user2.setId(12);
user2.setName("郭叶兵2");
user2.setPassword("1232");
list.add(user1); list.add(user2);
application.setAttribute("list",list);
%>
<!-- 使用EL表达式获得域中的值 -->
${company};
${user.name};
${list[1].name}; //1表示取第二个user的信息
<!-- el可以执行表达式运算 -->
${1+1 }
${1==1?true:false }
<!-- empty 判定某个对象是否是null 是null返回true 否则返回false -->
${empty user }
</body>
</html>
9.使用jstl标签库来完成 <c:if test="">
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<% request.setAttribute("count",10);
%>
<c:if test="${count==10}">
xxxxxxxxxx
</c:if>
<c:if test="${count!=10}">
yyyyyyyyyyy
</c:if>
<!-- 下面都是假的代码,主要是为了加深登录注册的记忆 -->
<!-- 用户没有登录 -->
<c:if test="${empty user}">
<li><a href="login.jsp">登录</a></li>
<li><a href="register.jsp">注册</a></li>
</c:if>
<!-- 用户已经登录 -->
<c:if test="${!empty user}">
<li><a href="login.jsp">${user.username}</a></li>
<li><a href="#">退出</a></li>
</c:if>
</body>
</html>
10.使用jstl标签库来完成 <c:forEach> 主要有两种方式遍历 普通循环
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<!-- 普通循环 默认步长是1 -->
<c:forEach begin="0" end="5" var="i" >
${i }
</c:forEach>
<br/>
<c:forEach begin="0" end="5" var="i" step="2">
${i }
</c:forEach>
</body>
</html>
11.使用jstl标签库来完成 <c:forEach> 增强for循环
遍历List<String>的值 遍历List<User>的值 遍历Map<String,String>的值 遍历Map<String,User>的值
<%@page import="java.util.HashMap"%>
<%@page import="java.util.Map"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page import="cn.itcast.domain.*" %>
<!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=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<!-- 遍历List<String>的值 遍历List<User>的值 遍历Map<String,String>的值 遍历Map<String,User>的值 -->
<%
//模拟List<String> strList
List<String> strList = new ArrayList<String>();
strList.add("itcast");
strList.add("itheima");
strList.add("boxuegu");
strList.add("shandingyu");
request.setAttribute("strList", strList);
//遍历List<User>的值
List<User> userList = new ArrayList<User>();
User user1 = new User();
user1.setId(2);
user1.setName("lisi");
user1.setPassword("123");
userList.add(user1);
User user2 = new User();
user2.setId(3);
user2.setName("wangwu");
user2.setPassword("123");
userList.add(user2);
application.setAttribute("userList", userList);
//遍历Map<String,String>的值
Map<String,String> strMap = new HashMap<String,String>();
strMap.put("name", "lucy");
strMap.put("age", "18");
strMap.put("addr", "西三旗");
strMap.put("email", "licy@itcast.cn");
session.setAttribute("strMap", strMap);
//遍历Map<String,User>的值
Map<String,User> userMap = new HashMap<String,User>();
userMap.put("user1", user1);
userMap.put("user2", user2);
request.setAttribute("userMap", userMap);
%>
<h1>取出strList的数据</h1>
<c:forEach items="${strList }" var="str">
${str }<br/>
</c:forEach>
<h1>取出userList的数据</h1>
<c:forEach items="${userList}" var="user">
user的name:${user.name }------user的password:${user.password }<br/>
</c:forEach>
<h1>取出strMap的数据</h1>
<c:forEach items="${strMap }" var="entry">
${entry.key }====${entry.value }<br/>
</c:forEach>
<h1>取出userMap的数据</h1>
<c:forEach items="${userMap }" var="entry">
${entry.key }:${entry.value.name }--${entry.value.password }<br/>
</c:forEach>
</body>
</html>