標籤: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頁面重新導向