English 中文(简体)
Java: 意外情况下的 Regex 匹配
原标题:Java: Regex matching in an unexpected situation

我正在研究一个服务器,该服务器有效连接到其他服务器,沿途运行一些nifty 助手功能。 我的 Web.xml 配置可以引导所有交通通过此类, 而班级则根据路由Info 和 Regex 逻辑来跟踪请求。 我的代码是这样的:

public class API1 extends HttpServlet {

    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {

        String pathInfo = req.getPathInfo();

        if (pathInfo.matches("/.*/.*/.*/?")) {
            _fooBarBazHandler.get(req, resp);
        } else if (pathInfo.matches("/.*/.*/?")) {
            _fooBarHandler.get(req, resp);
        } else if (pathInfo.matches("/.*/?")) {
            _fooHandler.get(req, resp);
        } else {
            resp.sendError(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
    }
...
}

现在,我的想法是,如果我打到 http://host/FooServicice/foo/bar/baz ,该代码将转向 , http://host/FooServicice/foo/bar/code >,那么,如果我打到 ,而 http://host/FooServicice/foo 将打到

然而,出于某种原因,http://host/FooService/foo 显然在 上匹配。要绝对清楚,我用调试器确定,pathInfo 的价值是“/foo”。

我的Regex不太强,有什么想法吗?

最佳回答

regexp /.*/*/? not 匹配 /foo> 。 您的错误在别处( 请原谅! ) 。

“http://fidle.re/0g28' rel=“nofollow'>http://fidle.re/0g28(点击蓝色“Java”)

问题回答

我在我的机器里做了测试, 第三次测试是真实的, 所以我同意安德鲁·库克的看法, 虫子在别处。





相关问题
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 ...