URL重写技术

Posted on 2008-04-10 23:00 wind_miao 阅读(1618) 评论(0)  编辑  收藏 所属分类: StrutsJ2EE
URL重写技术的优点:
1、满足搜索引擎的要求。某些搜索引擎不能支持动态页面的抓取,大量的信息就不能被用户搜索到。应此把网站地址改成静态的绝对地址是很有必要的。
2、隐藏技术实现。
有些网站挂着.asp、.jsp开发语言的标记,让人一眼就看出使用什么语言开发的。使用URL重写技术就可以实现地址的隐藏。

下面是个URL重写技术的例子:

index.jsp

<%@ page language="java" pageEncoding="ISO-8859-1"%>

<%@ taglib uri="http://struts.apache.org/tags-bean" prefix="bean" %>
<%@ taglib uri="http://struts.apache.org/tags-html" prefix="html" %>
<%@ taglib uri="http://struts.apache.org/tags-logic" prefix="logic" %>
<%@ taglib uri="http://struts.apache.org/tags-tiles" prefix="tiles" %>


<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html:html lang="true">
  <head>
    <html:base />
   
    <title>index.jsp</title>

 <meta http-equiv="pragma" content="no-cache">
 <meta http-equiv="cache-control" content="no-cache">
 <meta http-equiv="expires" content="0">   
 <meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
 <meta http-equiv="description" content="This is my page">
 <!--
 <link rel="stylesheet" type="text/css" href="styles.css">
 -->

  </head>
 
  <body>
   <html:link page="/personal/jilin/gaohongyan">test</html:link>
  </body>
</html:html>



Action的java代码:
UrlReWriteAction.jsp

* Generated by MyEclipse Struts
 * Template path: templates/java/JavaClass.vtl
 */
package com.yourcompany.struts.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

/**
 * MyEclipse Struts
 * Creation date: 04-10-2008
 *
 * XDoclet definition:
 * @struts.action validate="true"
 */
public class UrlReWriteAction extends Action {
 /*
  * Generated Methods
  */

 /**
  * Method execute
  * @param mapping
  * @param form
  * @param request
  * @param response
  * @return ActionForward
  */
 public ActionForward execute(ActionMapping mapping, ActionForm form,
   HttpServletRequest request, HttpServletResponse response) {
  // TODO Auto-generated method stub
  System.out.println("city = " + request.getParameter("city"));
  System.out.println("name = " + request.getParameter("name"));
  return null;
 }
}


导入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

<?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>/personal/([a-z]+)/([a-z]+)</from>
  <to type="forward">/urlReWrite.do?city=$1&amp;name=$2</to>
 </rule>

</urlrewrite>

更多内容请登录:http://tuckey.org/urlrewrite/


只有注册用户登录后才能发表评论。


网站导航:
 

posts - 1, comments - 3, trackbacks - 0, articles - 7

Copyright © wind_miao