JSP頁面重新導向

來源:互聯網
上載者:User

標籤:host   請求   center   跳轉   class   adb   cal   blog   描述   

以下內容引用自http://wiki.jikexueyuan.com/project/jsp/page-redirect.html:

頁面重新導向通常用於當一個檔案移動到一個新的位置,需要向用戶端發送到這個新的位置,或可能是因為Server Load Balancer,或簡單隨機化。

請求重新導向到另一個頁面的最簡單的方法是使用Response對象的sendRedirect()方法。以下是該方法的符號描述:

public void response.sendRedirect(String location) throws IOException 

此方法返回帶有狀態編碼和新的頁面位置的響應給瀏覽器。也可以一起使用setStatus()和setHeader()方法實現相同的重新導向。

....String site = "http://www.newpage.com" ;response.setStatus(response.SC_MOVED_TEMPORARILY);response.setHeader("Location", site); ....

樣本:

這個例子顯示了如何將一個JSP頁面重新導向到另一個位置:

<%@ page import="java.io.*,java.util.*" %><html><head><title>Page Redirection</title></head><body><center><h1>Page Redirection</h1></center><%   // New location to be redirected   String site = new String("http://www.easonjim.com");   response.setStatus(response.SC_MOVED_TEMPORARILY);   response.setHeader("Location", site); %></body></html>

現在將上面的代碼放在PageRedirect.jsp中,並且使用URL:http://localhost:8080/PageRedirect.jsp來調用此JSP。它將跳轉到頁面http://www.easonjim.com。

 

測試工程:https://github.com/easonjim/5_java_example/tree/master/jspbasics/test13

JSP頁面重新導向

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.