这段时间一直都在研究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 >
< 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 >
<%
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 >
<! 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