服务器之家:专注于服务器技术及软件下载分享
分类导航

PHP教程|ASP.NET教程|Java教程|ASP教程|编程技术|正则表达式|C/C++|IOS|C#|Swift|Android|VB|R语言|JavaScript|易语言|vb.net|

服务器之家 - 编程语言 - Java教程 - JAVA实现301永久重定向方法

JAVA实现301永久重定向方法

2021-04-23 11:27tiger925 Java教程

本篇文章给大家总结了JAVA中实现永久重定向的方法以及详细代码,对此有需要的朋友可以参考学习下。

代码实例:

?
1
2
3
4
5
6
7
8
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%>
<%
// 转到iq.jsp
out.clearbuffer();
response.setstatus(httpservletresponse.sc_moved_permanently);
string param = request.getquerystring();
response.setheader("location","iq.jsp"+(param==null?"":("?"+param)));
%>
?
1
2
3
4
5
6
7
8
<%@ page language="java" contenttype="text/html; charset=utf-8" pageencoding="utf-8"%> 
<% 
 // 转到iq.jsp  
out.clearbuffer();  
response.setstatus(httpservletresponse.sc_moved_permanently);   
string param = request.getquerystring(); 
response.setheader("location","iq.jsp"+(param==null?"":("?"+param))); 
%>

如上这段代码,将访问download.jsp的所有访问,永久重定向到 iq.jsp
参数也同时携带过去。

如果你的重定向是跨域的,或者使用完整路径,可以使用

?
1
response.setheader("location","http://www.laozizhu.com/iq.jsp"+(param==null?"":("?"+param)));
?
1
response.setheader("location","http://www.laozizhu.com/iq.jsp"+(param==null?"":("?"+param)));

这样你就可以指定完整的转向域名了。

用常用方式实现的是302跳转:

?
1
response.sendredirect(http://www.baidu.com);
?
1
response.sendredirect("http://www.baidu.com");

原文链接:https://blog.csdn.net/tiger925/article/details/8596065

延伸 · 阅读

精彩推荐