JSP website Permanent change domain name processing process _jsp programming

Source: Internet
Author: User
Tags change domain name
1, under Tomcat for the old domain to do a virtual host
Home page for index.jsp, 404 error page for unfind.jsp
Homepage uses 301 to turn, is advantageous to the search engine optimization
<%
Response.setstatus (301);
Response.setheader ("Location", "http://***.com");
Response.setheader ("Connection", "close");
%>
unfind.jsp that the old page should be turned to the new page address, did not jump to the new page, so the previous chain will not be affected

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "iserrorpage=" true "pageencoding=" Utf-8 "%>
<%
Response.setstatus (HTTPSERVLETRESPONSE.SC_OK);
String key = (string) request.getattribute ("Javax.servlet.forward.servlet_path");
String id= request.getparameter ("id");
if (key!= null) {
int index = Key.lastindexof ("/");
if (index!=-1) {
Key = key.substring (index + 1);
if (Key.startswith ("V")) {
Response.sendredirect ("http://***.com/viewtv/" + ID);
}
if (Key.startswith ("T")) {
Response.sendredirect ("http://***.com/tv/" + ID);
}
}
}
Response.sendredirect ("http://***.com");
%>
Reference articles
Reprint from Http://blog.csdn.net/lifaming15/archive/2008/10/16/3084282.aspx
URL rewrite, in fact, is to take a lot of parameters of the URL, into a seemingly very regular URL, the main purpose is to search engines.
Example
/viewthread.jsp?id=1234
/viewthread.jsp?id=1235
/viewthread.jsp?id=1236
After rewriting, you can use the
/viewthread/1234.htm
/viewthread/1235.htm
/viewthread/1236.htm
I'm currently using Tomcat+apache, and I've tried three ways to rewrite it.
One, Tomcat filter
The most typical is the class library with Urlreweite. If you have experience, writing yourself is not very troublesome. You can also use ready-made open source free urlrewrite filter. http://tuckey.org/urlrewrite/Download
Modify the Web.xml add filter, and then configure a filtering rule on it
Web.xml Modification Section
<!--Set URL rewrite-->
<filter>
<filter-name>UrlRewriteFilter</filter-name>
<filter-class>
Org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>UrlRewriteFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
Filtering rules
<?xml version= "1.0" encoding= "Utf-8"?>
<! DOCTYPE urlrewrite Public "-//tuckey.org//dtd urlrewrite 2.6//en"
"Http://tuckey.org/res/dtds/urlrewrite2.6.dtd" >
<urlrewrite>
<rule>
<from>/viewthread/(\d+) .htm$</from>
<to>/viewthread.jsp?id=$1</to>
</rule>
</urlrewrite>
Second, the use of Apache MoD
# get rid of this front #, enable it
LoadModule Rewrite_module modules/mod_rewrite.so
<virtualhost _default_:80>
# Other configuration data
Rewriteengine on
# three lines below to implement dynamic parsing
Rewriterule ^/viewthread/(\d+). htm$/viewthread.jsp?id=$1 [l,pt]
</VirtualHost>
Third, the use of the system 404 error
That is, the page did not find the error
Instance:
Http://www.xxx.net/f29
will be automatically forwarded to
http://www.xxx.net/forumdisplay.jsp?fid=29
Because the system does not exist f29, it uses the 404 error-handling page to implement this function
The page code is as follows
View Copy to clipboard print
<%@ page language= "java" contenttype= "text/html; CHARSET=GBK "iserrorpage=" true "pageencoding=" GBK "%>
<%
Response.setstatus (HTTPSERVLETRESPONSE.SC_OK);
String key = (string) request.getattribute ("Javax.servlet.forward.servlet_path");
if (key!= null) {
int index = Key.lastindexof ("/");
if (index!=-1) {
Key = key.substring (index + 1);
if (Key.startswith ("F") | | key.startswith ("P")) {
try {
Long id = Long.parselong (key.substring (1));
String url = key.startswith ("f")? "Forumdisplay.jsp?fid=": "viewthread.jsp?tid=";
%>
<jsp:forward page= "<%=url+id%>" ></jsp:forward>
<%
Return
catch (Exception ex) {}
}
%>
<jsp:forward page= "<%=response.encodeurl (" search.jsp ")%>" ><jsp:param name= "keyword" value= "<%= Key%> "/></jsp:forward>
<%
Return
}
}
%>
Sorry, the page you requested is not found!
Summarize
The first kind of trouble, but for the smooth running of the system, is a good choice
The second requirement is to configure Apache, but the Apache reboot is fast, suitable for the possibility of often becoming a situation, and the Apache mod is still a good efficiency
The third most flexible, generally simple application can use this.
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.