java server page 運行在服務器端的頁面。本質就是servlet.css
運行流程:
html
jsp(第一次訪問) ------->.java ------->.class ------->運行java
JSP中的腳本:
web
<% java內容%>
跨域
<%=表達式%>
瀏覽器
<%! 內容 %> 該腳本包裹的內容會出如今類定義中。tomcat
jsp腳本註釋:服務器
<%-- 註釋內容 --%> cookie
//該註釋和html註釋的區別是,該註釋不會出如今.java文件中,而html會出如今.java文件中session
cookie和session都屬於會話技術。
*cookie的默認主機就是發送cookie資源所在的主機
*主機用處: 若是cookie須要發送,那麼主機也必須符合.
*主機的手動控制:
理論上能夠手動控制. 主機的設置不容許設置發送cookie資源所在主機之外的其餘主機.
//cookie.setDomain("www.baidu.com"); //不容許
//cookie.setDomain("localhost"); // localhost => 特殊的主機 =>不能設置
cookie.setDomain(".baidu.com");//若是咱們當前主機是www.baidu.com.那麼這麼設置是沒問題
需求: cookie能夠跨越多個主機 例如: www.baidu.com music.baidu.com 等等
操做:
1. 將cookie的主機設置爲".baidu.com" => *.baidu.com主機都知足
2. 將cookie的路徑設置爲"/"=> 什麼路徑都知足
解釋:決定cookie發送的條件是:訪問的路徑是不是這個cookie 所在路徑的子路徑。
也就是 主機名+項目名(或者經過cookie.setPath("/");設置的)
主機同樣,路徑設爲"/" ,則可以共享cookie.即瀏覽器可以發送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); } }
詳解:
tomcat----------> tomcat/conf/web.xml 第511行,默認設置是30分鐘
一個項目:web.xml 中添加
<!-- 配置session的過時時間(分鐘) --> <session-config> <session-timeout>30</session-timeout> </session-config>
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對象銷燬
解決辦法:將網站中全部能點的超連接,後面都附帶上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的參數: