一. response
response:響應對象。
提供的方法:
void addCookie(Cookie cookie):服務端向客戶端增加cookie對象。
void sendRedirect(String location)throws IOException:頁面跳轉的一種方式(即重定向)。
void setContentType(String type):設置服務端響應的編碼(設置服務端的contentType)。
實例:登錄。
login.jsp ->check.jsp->success.jsp
下面分別爲:login.jsp,check.jsp,success.jsp。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="check.jsp" method="post">
用戶名:<input type="text" name="uname"><br/>
密碼:<input type="password" name="upwd"></br>
性別:<input type="radio" sex="usex" >男
<input type="radio" sex="usex">女<br/>
<input type="submit" value="登錄"><br/>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("uname");
String pwd=request.getParameter("upwd");
if(name.equals("zs")&&pwd.equals("abc"))//假設zs,abc
{
response.sendRedirect("success.jsp");
}else{
//登錄失敗
out.print("用戶名或者密碼有誤!");
}
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
登錄成功!<br/>
歡迎您:
<%
String name=request.getParameter("uname");
out.print(name);
%>
</body>
</html>
在瀏覽器搜索欄輸入:http://localhost:8080/MyJspProject/login/login.jsp。
可以看出,success.jsp沒有拿到真正的值。那麼我們需要將check.jsp 裏的重定向換掉,這裏換成了請求轉發。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("uname");
String pwd=request.getParameter("upwd");
if(name.equals("zs")&&pwd.equals("abc"))//假設zs,abc
{
//response.sendRedirect("success.jsp");頁面跳轉。重定向,導致數據丟失。
//那麼我們換一種頁面跳轉:請求轉發。可以獲取數據,而且地址欄沒有改變(仍然此jsp文件)
request.getRequestDispatcher("success.jsp").forward(request, response);
}else{
//登錄失敗
out.print("用戶名或者密碼有誤!");
}
%>
</body>
</html>
測試的結果:
請求轉發 | 重定向 | |
地址欄是否改變 | 不變 | 改變 |
是否保留第一次請求時的數據 | 保留 | 不保留 |
請求的次數 | 1 | 2 |
跳轉的位置 | 服務端 | 客戶端發出的第二次 |
請求轉發有一次請求一次響應,請求轉發的跳轉是在服務器內部跳轉。所以地址欄地址不改變。
重定向有兩次請求,二次響應,重定向的跳轉是返回到客戶端,讓客戶端重新發起一次跳轉。
二. session和cookie
session:會話。存在於服務端,內置對象。
一次會話 的例子:
a. 瀏覽網站:開始——>關閉。
b. 購物。瀏覽,付款,退出。
c.電子郵件。瀏覽——>寫郵件——>退出。
session機制:
服務端會在第一次請求時創建session(通過session保存客戶信息),每個session會自帶一個唯一的sessionID,並且把sessionID的值賦值給JSESSIONID,JSESSIONID會保存到Cookie裏(cookie的name=JSEESIONID,value=服務端的sessionID),響應的時候Cookie發給客戶端,客戶端就有JSEESIONID了。因此,客戶端的cookie和服務端的session一一對應(客戶端的JSEESIONID——服務端的sessionID)。即客戶端的cookie(JSEESIONID)和服務端的SessionID像對應。
客戶端第二/n次請求服務端時:服務端會先使用客戶端cookie中的JSEESIONID 去服務端的session中匹配sessionID,如果匹配成功(cookie JSESSIONID和session的sessionID),聲明此用戶 不是第一次訪問了。
例子:
客戶端: 顧客(顧客端)
服務端:存包處——>商場(服務端)
顧客第一次存包:商場判斷此人是否 之前已經存過包(通過手裏是否有鑰匙)。
如果是新顧客(沒鑰匙),分配一個鑰匙(鑰匙和櫃子一一對應)。
第二/n次存包:商場判斷此人是否 之前已經存過包(通過手裏是否有鑰匙)。
如果是有鑰匙的,則不需要分配鑰匙。
session的總結:
a. session存儲在服務端。
b. session是在 同一個(客戶)請求時 共享(同一次會話時,所以的jsp均可共享)。
c. 實現機制:見上面。
session的方法:
String getID():獲取sessionID
Boolean isNew():判斷是否是 新用戶
void invalidate():使session失效(如:退出登錄,註銷)
void setAttribute()
Object getAttribute()
void setMaxInactiveInterval(秒):設置最大有效 非活動時間
int getMaxInactiveInterval():獲取最大有效 非活動
int getMaxInactiveInterval():獲取最大有效非活動時間
例:
login.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<form action="check.jsp" method="post">
用戶名:<input type="text" name="uname"><br/>
密碼:<input type="password" name="upwd"><br/>
<input type="submit" value="登錄"><br/>
</form>
</body>
</html>
check.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("uname");
String pwd=request.getParameter("upwd");
if(name.equals("wangcai")&&pwd.equals("abc"))
{
//只有登錄成功,session中才會存在uname和upwd
session.setAttribute("uname", name);
session.setAttribute("upwd", pwd);
session.setMaxInactiveInterval(10);
request.getRequestDispatcher("welcome.jsp").forward(request, response);
}else{
response.sendRedirect("login.jsp");
}
%>
</body>
</html>
welcome.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
歡迎您,
<%
String name=(String)session.getAttribute("uname");
if(name==null)
{
response.sendRedirect("login.jsp");
}else
{
out.print(name);
}
%>
</body>
</html>
a.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
String name= (String)session.getAttribute("uname");
out.print(name);
%>
</body>
</html>
輸入...login.jsp 後,再輸入..a.jsp會出現wangcai。過10秒,再輸入...wangcai,出現null。說明10秒後的session失效了。
再加入註銷的功能:
添加了invalidate.jsp,修改了welcome.jsp。
invalidate.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
session.invalidate();
%>
</body>
</html>
welcome.jsp:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
歡迎您,
<%
String name=(String)session.getAttribute("uname");
if(name==null)
{
response.sendRedirect("login.jsp");
}else
{
%>
<a href="invalidate.jsp">註銷</a>
<%
out.print(name);
}
%>
</body>
</html>
cookie:客戶端,不是內置對象,用cookie的時候需要new。cookie是有 服務端產生的, 再發送給客戶端保存。相當於 本地緩存的作用 。
好處:提高訪問服務端的效率。
弊端:安全性較差。
Cookie:name=value
通過javax.servlet.http.Cookie生成Cookie。
該對象的方法:
public Cookie(String name,String value)
String getName():獲取name
String getValue():獲取value
void setMaxAge(int expiry):最大有效期(秒)。即客戶端拿到文件的有效時間。
a. 服務端增加cookie:response對象;客戶端獲取對象:request對象。
b. 不能直接獲取某一個單獨對象,只能一次性將 全部的cookie 拿到。
示例:
下面分別爲:response_AddCookie.jsp——>result.jsp。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
//服務端
Cookie cookie1=new Cookie("name","張三");
Cookie cookie2=new Cookie("pwd","abc");
//服務端準備Cookie
response.addCookie(cookie1);
response.addCookie(cookie2);
//頁面跳轉到客戶端(重定向,轉發)
response.sendRedirect("result.jsp");
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
Cookie[] cookies=request.getCookies();
for(Cookie cookie:cookies)
{
out.print(cookie.getName()+"---"+cookie.getValue()+"<br/>");
}
%>
</body>
</html>
結果:
通過F12可以發現,除了自己設置的Cookie對象(name,pwd)外,還有一個name爲 JSESSIONID的cookie。
Cookie應用案例:
需求:使用Cookie實現 記住用戶名 功能。即下次登錄的時候不用我們輸入用戶名。
下面分別爲:login.jsp,check.jsp,A.jsp。
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%!
String uname;
%>
<%
Cookie[] cookies=request.getCookies();
for(Cookie cookie:cookies)
{
if(cookie.getName().equals("uname"))
{
uname=cookie.getValue();
}
}
%>
<form action="check.jsp" method="post">
用戶名:<input type="text" name="uname" value="<%=(uname==null?"":uname)%>"><br/>
密碼:<input type="password" name="upwd"></br>
性別:<input type="radio" sex="usex" >男
<input type="radio" sex="usex">女<br/>
<input type="submit" value="登錄"><br/>
</form>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("uname");
String pwd=request.getParameter("upwd");
//將用戶名加入到Cookie中,建議cookie只保存英文數字,否則需要進行編碼,解碼。
Cookie cookie=new Cookie("uname",name);
response.addCookie(cookie);
response.sendRedirect("A.jsp");
%>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
</body>
</html>
結果:
我們輸入各種信息後:
我們點擊登錄後跳轉到A.jsp:
我們再在地址欄裏打開login.jsp這個界面,會發現用戶名自動是我們上次的名字:
這說明其Cookie實現了記住用戶名的功能。
我們也可以爲cookie設置有效時間:
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Insert title here</title>
</head>
<body>
<%
request.setCharacterEncoding("utf-8");
String name=request.getParameter("uname");
String pwd=request.getParameter("upwd");
//將用戶名加入到Cookie中,建議cookie只保存英文數字,否則需要進行編碼,解碼。
Cookie cookie=new Cookie("uname",name);
cookie.setMaxAge(10);//設置有效時間
response.addCookie(cookie);
response.sendRedirect("A.jsp");
%>
</body>
</html>
cookie和session的區別:
session | cookie | |
保存的位置 | 服務端 | 客戶端 |
安全性 | 較安全 | 較不安全 |
保存的內容 | Object | String |