1. Response: The method provided by the response object: void addCookie(Cookie Cookie); The server adds a cookie object to the client void sendRedirect(String Location) throws IOException. Void setContetType(String type): Sets the encoding of the server response (sets the contentType type of the server)
  2. Example: login login.jsp -> check.jsp ->success.jsp 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"> User name :<input type="text" name="uname"><br/> Password :<input type="password" name="upwd"><br/>
		<input type="submit" value="Login"><br/>
		
	</form>
</body>
</html>
Copy the code

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")) {// Suppose zs ABC
				response.sendRedirect("success.jsp");// Page redirection: Data is lost due to redirection
				// Page jump: request forward, can obtain the data, and the address bar has not changed (still retain the page of the forward check.jsp)
				//request.getRequestDispatcher("success.jsp").forward( request,response);
			}else{
				// Login failed
				out.print("Wrong username or password!"); } %> </body> </html>Copy the code

success.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% String name = request.getParameter()"uname"); out.print(name) ; %> </body> </html>Copy the code

Redirection result:



Request forwarding result:

Forward requests redirect
Whether the address bar has changed Constant (check. JSP) The change (success. JSP)
Whether to retain the data from the first request keep Don’t keep
Number of requests 1 2
The location where the jump occurs The service side The second jump issued by the client
  • Forwarding and redirecting forwarding: Zhang SAN (client) -> [Service Window A (server) -> Service Window B]

  • Redirect: John (client) -> Server window A (server) -> Go to B

    Zhang SAN (client) -> Service window B (server) -> End

2,

  • Session (Server)
  • Cookie (client, not built-in object) : Cookies are generated by the server and sent to the client for saving. Zs/ABC)-> Server (Hello.mp4; Zs/ABC) Function: Improves the efficiency of accessing the server, but has poor security.
Cookie: name = value javax.mail. Servlet. HTTP.Cookie
public Cookie(String name,String value)
String getName(a): Gets name StringgetValue(a)Gets the valuevoid setMaxAge(int expiry); Maximum validity period (seconds)Copy the code
  • Server prepares cookies:

    Response.addcookie (Cookie Cookie) Page redirects (forwards, redirects) client obtains Cookie: request.getcookies ();

  • A. Add a cookie: Response object to the server. The client obtains the request object

  • B. A single object cannot be obtained directly, only all cookies can be obtained at one time

The cookie case creates a cookie folder in a WebContext

response_addCookie.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>
	<%
		/ / the server
		Cookie cookie1 = new Cookie("name"."zs");
		Cookie cookie2 = new Cookie("pwd"."abc");
		
		response.addCookie(cookie1);
		response.addCookie(cookie2);
		
		// Page jump to client (forward or redirect skip)
		response.sendRedirect("result.jsp");
	%>
</body>
</html>
Copy the code

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>
	<%
		/ / the client
		Cookie[] cookies = request.getCookies();
	
			for(Cookie cookie:cookies){
				out.print(cookie.getName()+"--"+cookie.getValue()+"<br/>");
			}
	
	%>
</body>
</html>
Copy the code

Visit: http://localhost:8080/01_jsp/cookie/responseaddCookie.jsp to jump to http://localhost:8080/01_jsp/cookie/result.jsp



In F12, you can find a Cookie whose name is JSESSIONID in addition to the Cookie you set

The JSESSIONID is the default cookie







Remote Address: [::1]:8080 equivalent to 127.0.0.1:8080

Status Code: general redirection starting with 3

Redirection, hyperlink, and direct access to the address bar are GET requests