URL重写技术的优点:
URL重写技术
URL重写技术的优点:
1、满足搜索引擎的要求。某些搜索引擎不能支持动态页面的抓取,大量的信息就不能被用户搜索到。应此把网站地址改成静态的绝对地址是很有必要的。
2、隐藏技术实现。
有些网站挂着.asp、.jsp开发语言的标记,让人一眼就看出使用什么语言开发的。使用URL重写技术就可以实现地址的隐藏。
下面是个URL重写技术的例子:
index.jsp
<%@pagelanguage="java"pageEncoding="ISO-8859-1"%>
<%@tagliburi="http://struts.apache.org/tags-bean"prefix="bean"%>
<%@tagliburi="http://struts.apache.org/tags-html"prefix="html"%>
<%@tagliburi="http://struts.apache.org/tags-logic"prefix="logic"%>
<%@tagliburi="http://struts.apache.org/tags-tiles"prefix="tiles"%>
<!DOCTYPEHTMLPUBLIC"-//W3C//DTDHTML4.01Transitional//EN">
<html:htmllang="true">
<head>
<html:base/>
<title>index.jsp</title>
<metahttp-equiv="pragma"content="no-cache">
<metahttp-equiv="cache-control"content="no-cache">
<metahttp-equiv="expires"content="0">
<metahttp-equiv="keywords"content="keyword1,keyword2,keyword3">
<metahttp-equiv="description"content="Thisismypage">
<!--
<linkrel="stylesheet"type="text/css"href="styles.css">
-->
</head>
<body>
<html:linkpage="/personal/jilin/gaohongyan">test</html:link>
</body>
</html:html>
Action的java代码:
UrlReWriteAction.jsp
*GeneratedbyMyEclipseStruts
*Templatepath:templates/java/JavaClass.vtl
*/
packagecom.yourcompany.struts.action;
importjavax.servlet.http.HttpServletRequest;
importjavax.servlet.http.HttpServletResponse;
importorg.apache.struts.action.Action;
importorg.apache.struts.action.ActionForm;
importorg.apache.struts.action.ActionForward;
importorg.apache.struts.action.ActionMapping;
/**
*MyEclipseStruts
*Creationdate:04-10-2008
*
*XDocletdefinition:
*@struts.actionvalidate="true"
*/
publicclassUrlReWriteActionextendsAction{
/*
*GeneratedMethods
*/
/**
*Methodexecute
*@parammapping
*@paramform
*@paramrequest
*@paramresponse
*@returnActionForward
*/
publicActionForwardexecute(ActionMappingmapping,ActionFormform,
HttpServletRequestrequest,HttpServletResponseresponse){
//TODOAuto-generatedmethodstub
System.out.println("city="+request.getParameter("city"));
System.out.println("name="+request.getParameter("name"));
returnnull;
}
}
导入JAR包:urlrewrite-2.6.0.jar在http://tuckey.org/urlrewrite/下载
web.xml中添加以下代码:
<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>
在WEB-INF添加:
urlrewrite.xml
<?xmlversion="1.0"encoding="utf-8"?>
<!DOCTYPEurlrewritePUBLIC"-//tuckey.org//DTDUrlRewrite2.6//EN"
"http://tuckey.org/res/dtds/urlrewrite2.6.dtd">
<urlrewrite>
<rule>
<from>/personal/([a-z]+)/([a-z]+)</from>
<totype="forward">/urlReWrite.do?city=$1&name=$2</to>
</rule>
</urlrewrite>
更多内容请登录:http://tuckey.org/urlrewrite/