“JSP 页面重定向”的版本间差异

本页内容
上一节: JSP_日期处理 下一节: JSP_点击量统计
(创建页面,内容为“{{DISPLAYTITLE:JSP 页面重定向}}20 = JSP 页面重定向 = 当需要将文档移动到一个新的位置时,就需要使用JSP重定向了。 最简单的重定向方式就是使用response对象的sendRedirect()方法。这个方法的签名如下: <pre> public void response.sendRedirect(String location) throws IOException </pre> 这个方法将状态码和新的页面位置作为响应发回给浏览器。您也可以使用…”)
 
Neo讨论 | 贡献
无编辑摘要
 
(未显示同一用户的1个中间版本)
第6行: 第6行:
最简单的重定向方式就是使用response对象的sendRedirect()方法。这个方法的签名如下:
最简单的重定向方式就是使用response对象的sendRedirect()方法。这个方法的签名如下:


<pre>
<sample title="" desc="" lang="jsp" hererun="1">
public void response.sendRedirect(String location)
public void response.sendRedirect(String location)
throws IOException  
throws IOException
</pre>
</sample>
这个方法将状态码和新的页面位置作为响应发回给浏览器。您也可以使用setStatus()和setHeader()方法来得到同样的效果:
这个方法将状态码和新的页面位置作为响应发回给浏览器。您也可以使用setStatus()和setHeader()方法来得到同样的效果:


<pre>
<sample title="" desc="" lang="jsp" hererun="1">
....
....
String site = &quot;http://www.xiaobai.wang&quot; ;
String site = "http://www.xiaobai.wang" ;
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setStatus(response.SC_MOVED_TEMPORARILY);
response.setHeader(&quot;Location&quot;, site);
response.setHeader("Location", site);
....
....
</pre>
</sample>
== 实例演示 ==
== 实例演示 ==


这个例子表明了JSP如何进行页面重定向:
这个例子表明了JSP如何进行页面重定向:


<pre>
<sample title="" desc="" lang="jsp" hererun="1">
&lt;%@ page language=&quot;java&quot; contentType=&quot;text/html; charset=UTF-8&quot;
<%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding=&quot;UTF-8&quot;%&gt;
     pageEncoding="UTF-8"%>
&lt;%@ page import=&quot;java.io.*,java.util.*&quot; %&gt;
<%@ page import="java.io.*,java.util.*" %>
&lt;html&gt;
<html>
&lt;html&gt;
<html>
&lt;head&gt;
<head>
&lt;title&gt;页面重定向&lt;/title&gt;
<title>页面重定向</title>
&lt;/head&gt;
</head>
&lt;body&gt;
<body>


&lt;h1&gt;页面重定向&lt;/h1&gt;
<h1>页面重定向</h1>


&lt;%
<%
   // 重定向到新地址
   // 重定向到新地址
   String site = new String(&quot;http://www.xiaobai.wang&quot;);
   String site = new String("http://www.xiaobai.wang");
   response.setStatus(response.SC_MOVED_TEMPORARILY);
   response.setStatus(response.SC_MOVED_TEMPORARILY);
   response.setHeader(&quot;Location&quot;, site);
   response.setHeader("Location", site);
%&gt;
%>


&lt;/body&gt;
</body>
&lt;/html&gt;
</html>
</pre>
</sample>
将以上代码保存在PageRedirecting.jsp文件中,然后访问http://localhost:8080/PageRedirect.jsp,它将会把您带至[[http://www.xiaobai.wang/]]。
将以上代码保存在PageRedirecting.jsp文件中,然后访问http://localhost:8080/PageRedirect.jsp,它将会把您带至[[http://www.xiaobai.wang/]]。

2022年8月17日 (三) 20:24的最新版本

JSP 页面重定向

当需要将文档移动到一个新的位置时,就需要使用JSP重定向了。

最简单的重定向方式就是使用response对象的sendRedirect()方法。这个方法的签名如下:


示例

public void response.sendRedirect(String location)
throws IOException

这个方法将状态码和新的页面位置作为响应发回给浏览器。您也可以使用setStatus()和setHeader()方法来得到同样的效果:


示例

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

实例演示

这个例子表明了JSP如何进行页面重定向:


示例

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<html>
<html>
<head>
<title>页面重定向</title>
</head>
<body>

<h1>页面重定向</h1>

<%
   // 重定向到新地址
   String site = new String("http://www.xiaobai.wang");
   response.setStatus(response.SC_MOVED_TEMPORARILY);
   response.setHeader("Location", site);
%>

</body>
</html>

将以上代码保存在PageRedirecting.jsp文件中,然后访问http://localhost:8080/PageRedirect.jsp,它将会把您带至[[1]]。

上一节: JSP_日期处理 下一节: JSP_点击量统计
此页面最后编辑于2022年8月17日 (星期三) 20:24。