jsp:
java server page 运行在服务器端的页面。本质就是servlet.
运行流程:
jsp(第一次访问) ------->.java ------->.class ------->运行
JSP中的脚本:
<% java内容%>
<%=表达式%>
<%! 内容 %> 该脚本包裹的内容会出现在类定义中。
jsp脚本注释:
<%-- 注释内容 --%>
//该注释和html注释的区别是,该注释不会出现在.java文件中,而html会出现在.java文件中
cookie:
cookie和session都属于会话技术。
cookie的创建,发送,获得,设置过期时间:
cookie的路径问题:
cookie中主机问题(了解):
*cookie的默认主机就是发送cookie资源所在的主机
*主机用处: 如果cookie需要发送,那么主机也必须符合.
*主机的手动控制:
理论上可以手动控制. 主机的设置不允许设置发送cookie资源所在主机以外的其他主机.
//cookie.setDomain("www.baidu.com"); //不允许
//cookie.setDomain("localhost"); // localhost => 特殊的主机 =>不能设置
cookie.setDomain(".baidu.com");//如果我们当前主机是www.baidu.com.那么这么设置是没问题
跨域(主机)共享cookie:
需求: cookie可以跨越多个主机 例如: www.baidu.com music.baidu.com 等等
操作:
1. 将cookie的主机设置为".baidu.com" => *.baidu.com主机都满足
2. 将cookie的路径设置为"/"=> 什么路径都满足
解释:决定cookie发送的条件是:访问的路径是否是这个cookie 所在路径的子路径。
也就是 主机名+项目名(或者通过cookie.setPath("/");设置的)
主机一样,路径设为"/" ,则能够共享cookie.即浏览器能够发送cookie。
cookie使用中文:
例1:使用cookie记录浏览历史
<%@ page language="java" import="java.util.*" pageEncoding="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 'history.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>
<a href="/day11/history?name=apple">apple</a><br>
<a href="/day11/history?name=banana">banana</a><br>
<a href="/day11/history?name=orange">orange</a><br>
<hr>
<%
Cookie[] cookies = request.getCookies();
Cookie cookie = null;
if(cookies!=null&&cookies.length>0){
for(Cookie c:cookies){
if(c.getName().equals("history")){
cookie = c;
}
}
}
%>
brower history:<%=cookie!=null?cookie.getValue():"" %>
</body>
</html>
package cookie.rember;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class History extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//获得传递过来的值
String name = request.getParameter("name");
//获得传递过来的cookie
Cookie[] cookies = request.getCookies();
Cookie cookie = null;
if(cookies!=null&&cookies.length>0){
for(Cookie c:cookies){
if(c.getName().equals("history")){
cookie = c;
}
}
}
//如果原来有cookie,放入新值
if(cookie!=null){
if(!cookie.getValue().contains(name)){
cookie.setValue(cookie.getValue()+","+name);
}
}else{
//如果原来没有,创建cookie,放入值
cookie = new Cookie("history",name);
}
//在response中添加cookie
response.addCookie(cookie);
//
response.sendRedirect("/day11/history.jsp");
}
}
例二:记住用户名
<%@page import="java.net.URLDecoder"%>
<%@ page language="java" import="java.util.*" pageEncoding="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 'login.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>
<%
Cookie[] cookies = request.getCookies();
Cookie cookie = null;
if(cookies!=null&&cookies.length>0){
for(Cookie c:cookies){
if(c.getName().equals("re")){
cookie = c;
}
}
}
%>
<form action="/day11/remember" method="post">
用户名<input type="text" name="name" value="<%=cookie!=null?URLDecoder.decode(cookie.getValue(), "UTF-8"):"" %>"><br>
<input type="checkbox" name="remember" value="yes" <%=cookie!=null?"checked='checked'":"" %>>记住用户名<br>
<input type="submit" value="submit">
</form>
</body>
</html>
package cookie.rember;
import java.io.IOException;
import java.net.URLEncoder;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class Remember extends HttpServlet {
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request, response);
}
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("UTF-8");
String name = request.getParameter("name");
String remember = request.getParameter("remember");
Cookie cookie = new Cookie("re",URLEncoder.encode(name, "UTF-8"));
if("yes".equals(remember)){
cookie.setMaxAge(60*60*24*14);
}else{
cookie.setMaxAge(0);
}
response.addCookie(cookie);
}
}
详解:
Session:
基本操作:
生命周期(过期时间):
tomcat----------> tomcat/conf/web.xml 第511行,默认设置是30分钟
一个项目:web.xml 中添加
<!-- 配置session的过期时间(分钟) -->
<session-config>
<session-timeout>30</session-timeout>
</session-config>
Session其他方法:
long ctime = session.getCreationTime();//获得创建时间
System.out.println("session的创建时间:"+new Date(ctime));
String id = session.getId();//获得Session的Id
System.out.println("sessionID:"+id);
long atime = session.getLastAccessedTime();// 获得最后一次操作Session的时间
System.out.println("session的最后访问时间:"+new Date(atime));
int sec = session.getMaxInactiveInterval();//获取Session的有效时间的
System.out.println("session过期时间:"+sec);
//session.setMaxInactiveInterval(1800);//设置Session的有效时间的
ServletContext sc = session.getServletContext();// 获得servletContext对象
System.out.println("ServletContext对象:"+sc);
boolean is = session.isNew();// 判断该Session是不是新的
System.out.println("session是否是新的:"+is);
session.invalidate();//(记住)让Session失效. 让Session对象销毁
cookie的禁用与session使用(了解的了解)
解决办法:将网站中所有能点的超链接,后面都附带上sessionid.
<a href="/day11-session/AServlet;JSessionID=xxxxx">
<a href="/day11-session/AServlet;JSessionID=xxxxx">
<a href="/day11-session/AServlet;JSessionID=xxxxx">
<a href="/day11-session/AServlet;JSessionID=xxxxx">
javaEE提供了一个方法,可以帮我们在路径后面加上sessionID的参数: