zs7456

haha!
随笔 - 4, 文章 - 1, 评论 - 31, 引用 - 0
数据加载中……

jsp URL重写

这段时间一直都在研究jsp生成静态页面的方法,在网上找了很多资料,也有不少朋友给我提出解决方案,确实解决了不少的问题,但是这样做有点麻烦。其实我并不是想减轻服务器的压力,我们要做的不是门户网站,访问量没那么大,所以不用担心服务器的承受能力。只是希望搜索引擎能够搜索到我们的页面,只要能达到目的,用什么样的方式都可以。

在网上看到了一则URL重写的贴子,感觉比较适合我这样的情况,应用起来简单。然后自己试着写了一个例子,居然成功了,而在Struts里不知道怎么实现,实现转发的时候好象没什么用了。*.do好象不能实现映射
以下是相关代码,若有不正之处,欢迎大家指正!
首先要去下载一个urlrewritefilter-2.6.zip,然后把它解压到WEB-INF下,然后配置一下web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee   http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  
<filter>
    
<filter-name>UrlRewriteFilter</filter-name>
    
<filter-class>org.tuckey.web.filters.urlrewrite.UrlRewriteFilter</filter-class>
    
<init-param>
      
<param-name>logLevel</param-name>
      
<param-value>WARN</param-value>
    
</init-param>
  
</filter>
  
<filter-mapping>
    
<filter-name>UrlRewriteFilter</filter-name>
    
<url-pattern>/*</url-pattern>
  
</filter-mapping>
  
<servlet>
    
<servlet-name>action</servlet-name>
    
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
    
<init-param>
      
<param-name>config</param-name>
      
<param-value>/WEB-INF/struts-config.xml</param-value>
    
</init-param>
    
<init-param>
      
<param-name>debug</param-name>
      
<param-value>3</param-value>
    
</init-param>
    
<init-param>
      
<param-name>detail</param-name>
      
<param-value>3</param-value>
    
</init-param>
    
<load-on-startup>0</load-on-startup>
  
</servlet>
  
<servlet-mapping>
    
<servlet-name>action</servlet-name>
    
<url-pattern>*.do</url-pattern>
  
</servlet-mapping>
</web-app>

然后再随便建立几个jsp页面,如:
MyJsp.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  
<head>
    
<base href="<%=basePath%>">
    
    
<title>My JSP 'MyJsp.jsp' starting page</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>
  
<% 
      
String a = request.getParameter("id");
  
%>
    
<%=basePath %> <br>
    
<%
        
if(a.equals("123"))
        {
            out.println(
"哈哈");
        }
        
else
        {
            out.println(
"再试一次!");
        }
     
%>
  
</body>
</html>

接着在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"
>

<!--

    Configuration file for UrlRewriteFilter
    http://tuckey.org/urlrewrite/

-->
<urlrewrite>

    
<rule>
        
<note>
            The rule means that requests to /test/status/ will be redirected to /rewrite-status
            the url will be rewritten.
        
</note>
        
<from>/test/status/</from>
        
<to type="redirect">%{context-path}/rewrite-status</to>
    
</rule>


    
<outbound-rule>
        
<note>
            The outbound-rule specifies that when response.encodeURL is called (if you are using JSTL c:url)
            the url /rewrite-status will be rewritten to /test/status/.

            The above rule and this outbound-rule means that end users should never see the
            url /rewrite-status only /test/status/ both in thier location bar and in hyperlinks
            in your pages.
        
</note>
        
<from>/rewrite-status</from>
        
<to>/test/status/</to>
    
</outbound-rule>
        
<rule>
            
<from>/test/([0-9]+)</from>
            
<to>/MyJsp.jsp?id=$1</to>
        
</rule>
</urlrewrite>

启动服务器,然后输入http://localhost:8080/test/123 ,那么就可以显示了,而实际上读取的路径是http://localhost:8080/MyJsp.jsp?id=123

posted on 2008-05-16 15:14 zs7456 阅读(3493) 评论(5)  编辑  收藏

评论

# re: jsp URL重写  回复  更多评论   

good....
2008-05-18 15:32 | si

# re: jsp URL重写  回复  更多评论   

搞的这么麻烦..还不如直接在服务器上设置html文件的解析方式为jsp,然后把所有jsp文件扩展名替换成html不就行了..
2008-05-21 13:38 | aisdf

# re: jsp URL重写[未登录]  回复  更多评论   

@aisdf

如果出现了参数怎么办呢
如:news.html?id=1
2008-05-21 14:33 | zs7456

# re: jsp URL重写  回复  更多评论   

@aisdf
你知道什么是Url 重写不??

都像你说的
还有那重写做什么?
2008-06-13 07:09 | 牛X

# re: jsp URL重写[未登录]  回复  更多评论   

@牛X

出现了参数也很简单啊

配置一下就可以了

如:
<rule>
<from>/test/([0-9]+)</from>
<to>/news.jsp?id=$1</to>
</rule>

news.jsp?id=123,那么地址就会变成/test/123
2008-06-13 08:03 | zs7456

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


网站导航: