English 中文(简体)
Java Filter URL Reping Loop Problem
原标题:Java Filter URL Rewriting Loop Problem

我希望有人能够指出,我在这里会错。 I m试图制造一个简单的过滤器,它做的是Paul Tuckey s URLRewriteFilter,但只做我需要做的事情。

我无法阻止它回到原来的《世界投资倡议》。 我在《URLRewriteFilter法典》上宣布了出来,似乎正在做基本上相同的事情。 我的过滤代码如下:

public static final String dispatcherURI = "/Dispatcher?q=";
public void doFilter(final ServletRequest request, final ServletResponse response,
                     final FilterChain chain)
        throws IOException, ServletException {
    final HttpServletRequest rq = (HttpServletRequest)request;
    final HttpServletResponseWrapper rsw =
        new HttpServletResponseWrapper((HttpServletResponse)response);
    final String uri = rq.getRequestURI();
    if (uriIsDispatchable(uri)) {
        final String forwardPath = dispatcherURI+uri;
        rq.getRequestDispatcher(forwardPath).forward(rq, rsw);
        LOG.info("Forward From: "+uri+", To: "+forwardPath);
    }
    else {
        try { chain.doFilter(request, response); }
        catch (Exception e) {
            LOG.error(e);
            throw new RuntimeException(e);
        }
    }
}

// checks URI against an ArrayList of URIs that should be ignored.
private static boolean uriIsDispatchable (final String uri) {
    boolean result = true;
    for (String i : ApplicationValues.uriIgnore) {
        if (uri.indexOf(i) == 0) {
            result = false;
            break;
        }
    }
    return result;
}

Here s the filter entry in web.xml

<filter>
    <filter-name>UrlRewriteFilter</filter-name>
    <filter-class>myapp.filter.URLRewrite</filter-class>
</filter>
<filter-mapping>
    <filter-name>UrlRewriteFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

Some notes on the code: A URI like /home should be forwarded to a dispatcher servlet like so: /Dispatcher?q=/home the dispatcher servlet then determines what code to execute based on what s in the query string.

过滤器正确地忽视了“uriIgnore”ArrayList(包括分机/分机)中的URIs,但前方在家留宿。 为了完整起见,本文摘录了在上述各种可分的方法中检索的uriIgnore ArrayList的内容:

public static final List<String> uriIgnore = new ArrayList<String>(4);
static {
  uriIgnore.add("/Dispatcher");
  uriIgnore.add("/netbeans-tomcat-status-test");
  uriIgnore.add("/resources");
  uriIgnore.add("/robots.txt");
}

另外,我跑了最新的Tomcat 6号车.在阿帕奇。

感谢任何帮助。 感谢。

最佳回答

I ll get around to figuring this out when I have time to peruse the URLRewrite Filter code further, but in the meantime it seems that if I really want the highest performance URL rewriting, I ll just let mod_rewrite and Apache do it.

我的解决办法(我确信,还有更好的办法这样做):

# .htaccess file contents
# Apache has the same docroot as the Java web app.
# Java webapp is running on port 8084
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-d
# directory where images, static html files and JS scripts are located.
# Apache will serve these.
RewriteRule ^resources - [L,NC]
# obviously, we want Apache to serve robots.txt
RewriteRule ^robots.txt$ - [L,NC]
RewriteRule ^(.*)$ http://localhost:8084/Dispatcher?q=$1 [L,P,QSA]
</IfModule>
问题回答

I think when the program execute the statement: rq.getRequestDispatcher(forwardPath).forward(rq, rsw); it will call the doFilter() method again, and then the program will be an endless loop. I think you can add a parameter to tell the url is from another doFilter() or not, so it can recognize and then forward the url or not.

Same infinite loop problem here, configure urlrewriter to filter only REQUEST, not FORWARD, try something like this in your web.xml:

 <filter-mapping>
     <filter-name>UrlRewriteFilter</filter-name>
     <url-pattern>/*</url-pattern>
     <dispatcher>REQUEST</dispatcher>
 </filter-mapping>

在本案中,在移交后,规则处理不再执行。





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签