當前位置:生活全書館 >

IT科技

> redirect java

redirect java

<link rel="stylesheet" href="https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4f61d3594de/a4ea273683c8.css" type="text/css" /><link rel="stylesheet" href="https://js.how234.com/bdf1083093/a4fb0a2d90d5e7db978c76453942a4aad8/a4f61d3594de/a4ea303194c0eaf695827b59325e.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><style>pre{overflow-x: auto}</style>

   <link rel="stylesheet" href="https://js.how234.com/third-party/SyntaxHighlighter/shCoreDefault.css" type="text/css" /><script type="text/javascript" src="https://js.how234.com/third-party/SyntaxHighlighter/shCore.js"></script><script type="text/javascript"> SyntaxHighlighter.all(); </script>

Java redirect是什麼?讓我們一起來了解一下吧!

Java redirect是第一個頁面通知瀏覽器發送一個新的頁面請求。重定向是一個客戶端行爲,用戶請求到達服務器之後,服務器返回響應,HTTP狀態碼置爲302,並將轉發的頁面儲存在響應頭中的Location屬性中,告訴客戶端應該向這個地址發出請求,然後客戶端再次發出請求。

java redirect

那麼我們如何區分選擇重定向還是轉發呢?通常情況下轉發更快,而且能保持request內的對象,所以他是第一選擇。但是由於在轉發之後,瀏覽器中URL仍然指向開始頁面,此時如果重載當前頁面,開始頁面將會被重新調用。如果你不想看到這樣的情況,則選擇轉發。

實戰操作,具體步驟如下:

package com.sn.servlet;import java.io.IOException;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;/** * Servlet implementation class ServletB */@WebServlet("/ServletB")public class ServletB extends HttpServlet {    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {        System.out.println("Bservlet");        /**         * 重定向:         * 1.設定Location         * 2.發送302狀態碼         * 重定向是兩次請求,瀏覽器地址欄發生變化         */        response.setStatus(302);  //重定向        //這兩個方法實現的過程都是一樣的        //response.setHeader("Location", "http://www.baidu.com");        response.sendRedirect("http://www.baidu.com");  //重定向要百度    }}

標籤: java redirect
  • 文章版權屬於文章作者所有,轉載請註明 https://shqsg.com/zh-hant/dianzi/oqp3qy.html