zs7456

          haha!
          隨筆 - 4, 文章 - 1, 評(píng)論 - 31, 引用 - 0
          數(shù)據(jù)加載中……

          jsp URL重寫(xiě)

          這段時(shí)間一直都在研究jsp生成靜態(tài)頁(yè)面的方法,在網(wǎng)上找了很多資料,也有不少朋友給我提出解決方案,確實(shí)解決了不少的問(wèn)題,但是這樣做有點(diǎn)麻煩。其實(shí)我并不是想減輕服務(wù)器的壓力,我們要做的不是門戶網(wǎng)站,訪問(wèn)量沒(méi)那么大,所以不用擔(dān)心服務(wù)器的承受能力。只是希望搜索引擎能夠搜索到我們的頁(yè)面,只要能達(dá)到目的,用什么樣的方式都可以。

          在網(wǎng)上看到了一則URL重寫(xiě)的貼子,感覺(jué)比較適合我這樣的情況,應(yīng)用起來(lái)簡(jiǎn)單。然后自己試著寫(xiě)了一個(gè)例子,居然成功了,而在Struts里不知道怎么實(shí)現(xiàn),實(shí)現(xiàn)轉(zhuǎn)發(fā)的時(shí)候好象沒(méi)什么用了。*.do好象不能實(shí)現(xiàn)映射
          以下是相關(guān)代碼,若有不正之處,歡迎大家指正!
          首先要去下載一個(gè)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>

          然后再隨便建立幾個(gè)jsp頁(yè)面,如:
          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>

          啟動(dòng)服務(wù)器,然后輸入http://localhost:8080/test/123 ,那么就可以顯示了,而實(shí)際上讀取的路徑是http://localhost:8080/MyJsp.jsp?id=123

          posted on 2008-05-16 15:14 zs7456 閱讀(3519) 評(píng)論(5)  編輯  收藏

          評(píng)論

          # re: jsp URL重寫(xiě)  回復(fù)  更多評(píng)論   

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

          # re: jsp URL重寫(xiě)  回復(fù)  更多評(píng)論   

          搞的這么麻煩..還不如直接在服務(wù)器上設(shè)置html文件的解析方式為jsp,然后把所有jsp文件擴(kuò)展名替換成html不就行了..
          2008-05-21 13:38 | aisdf

          # re: jsp URL重寫(xiě)[未登錄](méi)  回復(fù)  更多評(píng)論   

          @aisdf

          如果出現(xiàn)了參數(shù)怎么辦呢
          如:news.html?id=1
          2008-05-21 14:33 | zs7456

          # re: jsp URL重寫(xiě)  回復(fù)  更多評(píng)論   

          @aisdf
          你知道什么是Url 重寫(xiě)不??

          都像你說(shuō)的
          還有那重寫(xiě)做什么?
          2008-06-13 07:09 | 牛X

          # re: jsp URL重寫(xiě)[未登錄](méi)  回復(fù)  更多評(píng)論   

          @牛X

          出現(xiàn)了參數(shù)也很簡(jiǎn)單啊

          配置一下就可以了

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

          news.jsp?id=123,那么地址就會(huì)變成/test/123
          2008-06-13 08:03 | zs7456

          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 蓬安县| 旬邑县| 新建县| 岳阳市| 上思县| 娱乐| 长沙县| 五寨县| 姜堰市| 甘谷县| 临澧县| 全州县| 封开县| 马关县| 和林格尔县| 延吉市| 博乐市| 南宫市| 商丘市| 石台县| 普陀区| 阳信县| 冀州市| 堆龙德庆县| 绵阳市| 和龙市| 汉阴县| 广丰县| 游戏| 安塞县| 辽阳市| 隆子县| 姜堰市| 安乡县| 子长县| 天镇县| 海安县| 定陶县| 虹口区| 泉州市| 扎兰屯市|