學習——JavaWeb06:response,cookie


學習——JavaWeb06:response,請求轉發重定向

Ø  Response響應對象

1、        提供的方法:

void addCookie(Cookie cookie);服務器向客戶端增加cookie對象

void sendRedirect(String location) throws IOException:重定向

void setConterType(String type):設置服務端響應的編碼(設置服務端的content類型)

2、        重定向和請求轉發的區別

1、 請求轉發:getRequestDispatcher(“b.jsp”).forward(request,response);請求轉發的方式跳轉頁面 A->B

2、 重定向:void sendRedirect(String location) throws IOException:

  • 地址欄是否改變:轉發不變定向變;
  • 是否保留第一次請求時的數據:轉發保留定向丟;
  • 請求的次數:轉發一次定向倆;
  • 效率:轉發很高定向慢;

     

2.Cookie

1、Cookie(客戶端,不是內置對象):是由服務端產生的,再發送給客戶端保存。

相當於 本地緩存的作用: 客戶端——>服務端

 

作用:可以提高訪問服務端的效率,但是安全性較差。

Cookie:key-value

Javax.servlet.http.Cookie

Public Cookie (String name,String value);

String getName();

String getValue();

Void setMaxAge(int expiry);最大有效期(秒)

 

服務端發送給客戶端:

Response.addCookie(Cookie cookie)

頁面跳轉(轉發,重定向)

客戶端獲取cookie:response對象,getCookies();客戶端必須一次全部拿到,不能單獨獲取一個cookie;

通過F12可以發現除了自己設置的cookie對象外,還有一個 key 為 JSESSIONID的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>

   <form action="check.jsp" method="post">

      用戶名:<input type="text" name="name"/><br>

      密碼:<input type="password" name="password"/><br>

      <input type="submit" name="登錄" 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("name");

      String pwd = request.getParameter("password");

     

      if(name.equals("zs")&&pwd.equals("123")){

         //response.sendRedirect("success.jsp");//導致數據丟失;

        

         //可以獲取數據,而且地址欄沒有改變(仍然保持轉發時的頁面:check.jsp)

         request.getRequestDispatcher("success.jsp").forward(request, response);

      }

      else{

         out.println("登錄失敗!");

      }

   %>

</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("name");

      out.println(name);

   %>

</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 cookie1 = new Cookie("name","zs");

      Cookie cookie2 = new Cookie("password","123");

     

      response.addCookie(cookie1);

      response.addCookie(cookie2);

     

      //通過重定向或者請求轉將 發頁面跳轉到客戶端

      response.sendRedirect("request.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

      Cookie[] cookies = request.getCookies();

  

      for(Cookie cookie:cookies){

         out.print(cookie.getName()+"-----"+cookie.getValue()+"<br/>");

         out.print("--------------------------<br/>");

      }

   %>

</body>

</html>


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM