一.response:響應對象
提供的方法:
void addCookie(Cookie cookie);服務端向客戶端增加一個cookie對象
void sendRedirect(String location) throws IOExcetion:頁面跳轉的一種方法
void setContentType(String type):設置服務端響應的編碼
示例重定向:
login.jsp—->check.jsp->success.jsp 判斷登錄是否合法
1.login.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<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>
2.check.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<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")) {
response.sendRedirect("success.jsp");
} else {
out.print("用戶名或者密碼錯誤!");
}
%>
</body>
</html>
3.success.jsp
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
登陸成功!
<br /> 歡迎您:
<%
String name = request.getParameter("uname");
out.print(name);
%>
</body>
</html>
經發現重定向方式會導致數據丟失:
示例請求轉發:
checks.jsp修改:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<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")) {
//response.sendRedirect("success.jsp");
request.getRequestDispatcher("success.jsp").forward(request,response);
} else {
out.print("用戶名或者密碼錯誤!");
}
%>
</body>
</html>
經運行發現,地址欄沒有變。
請求轉發和重定向的區別:
a.地址欄是否轉變,請求不變(內部轉發看不見),重定改變
b.是否保留第一次請求時的數據,請求保留,重定不保留
c.請求次數 請求轉發1次,重定2次
請求轉發:
重定向:
就是找錯人了,再次請求到success.jsp:
轉發:
張三 --> 【服務窗口a --> 服務窗口b】
重定向:
張三 --> 【 服務窗口a】 --> 去張三找【服務窗口b】
| ^
|-----------------------------------|
二. session(服務端,內置對象) cookie(客戶端,不是內置對象)
cookie由服務端生成,再給客戶端保存,相當於本地緩存作用
客戶端->服務端(hello.MP4,zs/abc)將(MP4,zc/abc)返回到客戶端,提高效率,但安全性低
Cookie :
1.包含key:value鍵值對
2.javax.servlet.http.Cookie
3.方法:
服務端向客戶端發送:
response.addCookie(Cookie cookie)
頁面跳轉(轉發或者重定向)
客戶端2獲取cookie:request.getCookie();
注意:
a.服務端增加cookie:response對象 客戶端獲取對象:request
b,必須一次將全部的cookie拿到
示例:
reponse_addCookie.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
//服務端
Cookie cookie1 = new Cookie("name", "zs");
Cookie cookie2 = new Cookie("pwd", "abc");
response.addCookie(cookie1);
response.addCookie(cookie2);
//頁面跳轉到客戶端
response.sendRedirect("result.jsp");
%>
</body>
</html>
result.jsp:
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
//客戶端
Cookie[] cookies = request.getCookies();
for (Cookie cookie : cookies) {
out.print(cookie.getName() + "->" + cookie.getValue() + "<br/>");
}
%>
</body>
</html>