Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29955221
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: 系统运维

2009-04-16 20:12:02

不是常规理解:response.sendRedirect哦。来看看吧!
首先要去下载一个,然后把它解压到WEB-INF下,然后配置一下web.xml


 
    UrlRewriteFilter
    org.tuckey.web.filters.urlrewrite.UrlRewriteFilter
   
      logLevel
      WARN
   

 

 
    UrlRewriteFilter
    /*
 


配置完成。相当于配置了一个过滤器了。
2.随便写一个JSP页面当作测试之用的。
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>


 
   
 
 
  <%
      String a = request.getParameter("id");
  %>
 

    <%
        if(a.equals("123"))
        {
            out.println("哈哈");
        }
        else
        {
            out.println("再试一次!");
        }
     %>
 

3.配置urlrewrite.xml里配置一下路径

        "">



   
       
            The rule means that requests to /test/status/ will be redirected to /rewrite-status
            the url will be rewritten.
       

        /test/([0-9]+)
        /MyJsp.jsp?id=$1       
   

   
       
            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.
       

        /rewrite-status
        /test/status/
   


OK!运行Tomcat 服务器。之后在浏览器中输入:

显示 : 哈哈
其实真实的访问地址应该是:
所以这才是真正意义上的URL重写!

扩散学习一下这个JAR文件方便今后的应用!
1.安装
1.1 下载这个文件包
1.2 配置web.xml文件内容如下:

UrlRewriteFilter
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter


UrlRewriteFilter
/*

1.3
Add your own configuration to the WEB-INF/urlrewrite.xml that was created.
手动去修改这个自带的配置文件。
1.4 Restart the context.

2.关于这个过滤配置的扩展说明包含的内容如下:

UrlRewriteFilter
org.tuckey.web.filters.urlrewrite.UrlRewriteFilter


confReloadCheckInterval
60



logLevel
DEBUG



statusEnabled
true



statusPath
/status



UrlRewriteFilter
/*

说明一点:
Note, setting logLevel to log4j or commons will cause the built in loging to
call either or
as if they were the logging framework,
obviously you will need to have the jar for log4j or commons-logging in your classpath.
可能需要借助于这两个现成的记录日志框架的方法进行相关处理!

3. 简单地解释一下 urlrewrite.xml 文件内容及相关的配置说明

PUBLIC "-//tuckey.org//DTD UrlRewrite 2.6//EN"
"">


^/some/olddir/(.*)$
/very/newdir/$1


其实隐藏在背后的是通过了JAVA中的正则运算实现的。

The urlrewrite.xml file must have a root element called "urlrewrite" and must contain
at least one "rule" element.

1. element
此标签包含的属性
Attribute Possible Value Explanation
enabled
(optional)
true (default) Enable this rule.
false Disable this rule.
表示这个规则是将被应用的。否则就不会被应用进来的!
例如:

In the following example requests for /world/usa/nyc will be transparently forwarded to /world.jsp?country=usa&city=nyc


^/world/([a-z]+)/([a-z]+)$
/world.jsp?country=$1&city=$2

如果这样进行配置的话那所有传过来的URL
/world/usa/nyc 都会被重定向为
/world.jsp?country=usa&city=nyc

哦!明白了$1、$2表示的是 引用 了标签中的第一个参数与第二个参数。相当于AWK中的接收参数方法
{所以要学习的东西越多那理解起来就越快,这样反而又可以推进自己学习的速度!}

2.
element
but it is used for rewriting urls that go through response.encodeURL()
它所包含的相关属性如下:
Attribute Possible Value Explanation
enabled
(optional)
true (default) Enable this rule.
false Disable this rule.
encodefirst
(optional)
true Run encodeURL() before running this outbound rule.
false (default) Run encodeURL() after running this outbound rule.
例子:

^/world.jsp?country=([a-z]+)&city=([a-z]+)$ [源]
/world/$1/$2 [替换的URL地址]

Using the example above JSP's with the code
">nyc
will output
nyc
3. element
This can be used with rule and outbound-rule。

World Rule

^/world/([a-z]+)/([a-z]+)$
/world.jsp?country=$1&city=$2


4.
element
A simple optional element used for documentation of the rule。一个注释之用的。

World Rule

Cleanly redirect world requests to JSP,
a country and city must be specified.

^/world/([a-z]+)/([a-z]+)$
/world.jsp?country=$1&city=$2


5.
element
An element that lets you choose condtions for the rule. Note, all conditions must be met for the rule to be run (unless "next" is set to "or" obvoiusly).

此属性的可能值:
Attribute Possible Value Explanation
type
(optional)
header (default)If used, the header name must be specified in the "name" attribute.
methodThe method of the request. GET, POST, HEAD etc.
portThe port that the web application server is running on.
time Current time at the server (this will be the number of seconds since 00:00:00 1970-01-01 UTC otherwise known as unix time).
i.e. (new Date()).getTime()
This can be used for making sure content goes live only at a time you set.
year Current year at the server.
i.e. (Calendar.getInstance()).get(Calendar.)
month Month at the server. January is 0
i.e. (Calendar.getInstance()).get(Calendar.)
dayofmonth Day of the month at the server. March first is 1
i.e. (Calendar.getInstance()).get(Calendar.)
dayofweek Day of the week at the server. Saturday is 1, Sunday is 7
i.e. (Calendar.getInstance()).get(Calendar.)
ampm AM or PM time at the server.
i.e. (Calendar.getInstance()).get(Calendar.)
hourofday The hour of the day (24 hour clock) at the server. 10pm is 22
i.e. (Calendar.getInstance()).get(Calendar.)
minute The minute field of the current time at the server.
i.e. (Calendar.getInstance()).get(Calendar.)
second The second field of the current time at the server.
i.e. (Calendar.getInstance()).get(Calendar.)
millisecond The millisecond field of the current time at the server.
i.e. (Calendar.getInstance()).get(Calendar.)
attribute Will check the value of a request attribute (don't confuse this with parameter!), name must be set when using this type.
i.e. request.([name])
auth-type Will check the value of a request attribute (don't confuse this with parameter!)
i.e. request.()
character-encoding The character encoding of the imcoming request.
i.e. request.()
content-length The length of the imcoming request (can be useful if you want to deny large requests).
i.e. request.()
content-type The type of the imcoming request. (this is probably not that useful)
i.e. request.()
context-path The context path of the imcoming request.
i.e. request.()
cookie The value of a cookie, note, name must be specified to use this
i.e. request.() the find we the one with [name] specified and check the value.
parameter A tidier way of checking request parameters than looking for them in the query string. This will check for the parameter in GET or POST, note, name must be specified.
i.e. request.([name])
path-info i.e. request.()
path-translated i.e. request.()
protocolThe protocol used to make the request, e.g. HTTP/1.1
i.e. request.()
query-stringThe query string used to make the request (if any), e.g. id=2345&name=bob
i.e. request.()
remote-addrThe IP address of the host making the request, e.g. 123.123.123.12
i.e. request.()
remote-hostThe host name of the host making the request, e.g. 123qw-dsl.att.com (note, this will only work if your app server is configured to lookup host names, most aren't).
i.e. request.()
remote-userThe login of the user making this request, if the user has been authenticated, e.g. bobt
i.e. request.()
requested-session-idReturns the session ID specified by the client, e.g. 2344asd234sada4
i.e. request.()
request-uriReturns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request
i.e. request.
request-urlReconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
i.e. request.
session-attribute (note, name must be set)
i.e. session.
session-isnew Weather the session is new or not.
i.e. session.
server-name The host name of the server to which the request was sent (from the host header not the machine name).
i.e. request.
scheme The scheme used for the request, e.g. http or https
i.e. request.
user-in-role (Note, the value for this cannot be a regular expression)
i.e. request.
name
(optional)
(can be anything) If type is header, this specifies the name of the HTTP header used to run the value against.
next
(optional)
and (default)The next "rule" and this "rule" must match.
orThe next "rule" or this "condition" may match.
operator
(optional)
equal (default) Equals. The operator to be used when the condition is run.
notequalNot equal to. (i.e. request value != condition value). Note, this operator only work with numeric rule types.
greaterGreater than. (i.e. request value > condition value). Note, this operator only work with numeric rule types.
lessLess than. (i.e. request value < condition value). Note, this operator only work with numeric rule types.
greaterorequalGreater to or equal to. (i.e. request value >= condition value). Note, this operator only work with numeric rule types.
lessorequalLess than or equal to. (i.e. request value <= condition value). Note, this operator only work with numeric rule types.

示例:
Mozilla/[1-4]
bigboss

PROPFIND
PUT

6.
element
Value can be a regular expression in the Perl5 style  [如果懂Perl的话那比较容易理解!]
Attribute Possible Value Explanation
casesensitive
(optional)
false (default) This value will be matched using case insentitive match. ie, "/WellingtoN" will match "/wellington".
true This value will be matched using case sentitive match. ie, "/aAa" will NOT match "/aaa".

^/world/([a-z]+)$ 示例!

7.
element
Value can be a regular replacement expression in the Perl5 style.
Attribute Possible Value Explanation
type
(optional)
forward (default) Requests matching the "conditions" for this "rule", and the URL in the "from" element will be internally forwarded to the URL specified in the "to" element. Note: In this case the "to" URL must be in the same context as UrlRewriteFilter. This is the same as doing:
RequestDispatcher rq = request.getRequestDispatcher([to value]);
rq.forward(request, response);
passthrough Identical to "forward".
redirect Requests matching the "conditions" and the "from" for this rule will be HTTP redirected. This is the same a doing:
HttpServletResponse.sendRedirect([to value]))
permanent-redirect The same as doing:
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.setHeader("Location", [to value]);

(note, SC_MOVED_TEMPORARILY is HTTP status code 301)
temporary-redirect The same as doing:
response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
response.setHeader("Location", [to value]);

(note, SC_MOVED_TEMPORARILY is HTTP status code 302)
last
(optional)
false (default) The rest of the "rules" will be processed if this one succeeds.
true No more "rules" will be processed if this one is a match.
encode
(optional)
false (default if under rule) response.([to]) will be run on the to url before performing the rewrite.
true (default if under outbound-rule) response.([to]) will NOT be called.
/world.jsp?country=$1  示例

8.
element
Allows you to set varous(各种) things if the rule is matched.
Attribute Possible Value Explanation
type
(optional)
request (default) The same as request.([name], [value]) (note, name must be set).
session The same as request.(true).([name], [value]) (note, name must be set).
response-header The same as response.([name], [value]) (note, name must be set).
cookie Value can be in the format "[value][:domain[:lifetime[:path]]]". This sets a cookie on the client's browser. The cookie's name is specified by the name attribute. The domain field is the domain of the cookie, such as '.apache.org',the optional lifetime is the lifetime of the cookie in minutes, and the optional path is the path of the cookie (note, name must be set).
status The same as response.([value])
content-type The same as response.([value])
charset The same as response.([value])
locale The same as response.([value]) specify the in the format (valid locales are, zh, zh-CN, zh-CN-southern i.e. "-" separating the language, country and variant (if any)).
name
(optional)
(can be anything) If type is request, session, response-header, cookie this specifies the name item.
示例

Mozilla/3\.0 (compatible; AvantGo .*)
.*
AvantGo


UP\.Browser/3.*SC03 .*
.*
Samsung SCH-6100


9.
element
Allows you to run a method on an object when a rule and it's conditions are matched.
Attribute Explanation
class (default) The class you want to run a method on. Must be a fully qualified name.
method (optional, default run) The method you want to run, the method must have the parameters (HttpServletRequest, HttpServletResponse) e.g. run(HttpServletRequest request, HttpServletResponse response)
Note, if init(ServletConfig) or destroy() is found they will be run at when creating or destroying an instance.
neweachtime (optional, default false) If you want new instance of the class to be created before running each time set to true.

^/world/[a-z]+/[a-z]+$

/world-presentation.jsp

示例:













阅读(2358) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~