Java Servlet:将请求传递回默认处理

发布于 2024-07-25 20:40:24 字数 238 浏览 3 评论 0原文

我希望 Servlet 根据前缀和扩展名处理对文件的请求,例如

prefix_*.xml

由于无法映射请求路径的开头和结尾,因此我已将所有 *.xml 请求映射到我的 Servlet。 现在的问题是:如何从 servlet 中删除不以“prefix_”开头的 XML 文件,以便像对 xml 文件的“正常”请求一样处理该请求?

这可能很简单,但我似乎无法找到答案...:-/

提前非常感谢

I want a Servlet to handle requests to files depending on prefix and extension, e.g.

prefix_*.xml

Since mapping on beginning AND end of request path is not possible, I have mapped all *.xml requests to my Servlet.
The question now is: how can I drop out of my servlet for XML files not starting with "prefix_", so that the request is handled like a "normal" request to an xml file?

This is probably quite simple but I do not seem to be able to find this out... :-/

Thanks a lot in advance

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

余生一个溪 2024-08-01 20:40:25

不放心,但是一旦捕获所有 *.xml 请求,您可以通过 HttpServletRequest.getRequestURI() 在代码中再次检查请求

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri =req.getRequestURI();
        int i = uri.lastIndexOf('/');
        int j = uri.lastIndexOf('.', i);
        if (uri.substring(i+1, j).startsWith("prefix_")) {
            // your code
        }
    }

(代码未经测试,仅是一个想法......)

Not shure, but once you catch all *.xml requests you can inspect the request again in your code via HttpServletRequest.getRequestURI()

protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String uri =req.getRequestURI();
        int i = uri.lastIndexOf('/');
        int j = uri.lastIndexOf('.', i);
        if (uri.substring(i+1, j).startsWith("prefix_")) {
            // your code
        }
    }

(code not tested, only an idea ...)

℡寂寞咖啡 2024-08-01 20:40:24

另一个解决方案(也许适合您)是,如果您正在/计划在该 Web 容器实例前面使用 Apache,您可以使用 apache 的重写模块。 将 url 重写为更易于 Web 应用程序容器处理的内容。

希望这可以帮助。
大卫。

another solution (maybe fits for you) is if you are using/plan to use an Apache in front of that web container instance you could use the rewrite module of apache. Rewriting the url to something more easy to handle for the Webapp container.

Hope this helps.
David.

独自唱情﹋歌 2024-08-01 20:40:24

我强烈建议为此使用适当的 MVC 框架。 正如您所发现的,标准 servlet API 在请求分派方面的灵活性非常有限。

理想情况下,您将能够将现有的 servlet 代码与 MVC 框架结合使用,框架基于路径模式执行 diapcthing,而 servlet 执行业务逻辑。 幸运的是,Spring MVC 允许您使用 ServletForwardingController 来做到这一点。 这将是一个非常轻量级的弹簧配置。

所以你的 web.xml 中会有这样的内容:

<servlet>
   <servlet-name>myServlet</servlet-name>
   <servlet-class>foo.MyServlet</servlet-class>
</servlet>

<servlet>
   <servlet-name>spring</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<url-mapping>
   <servlet-name>spring</servlet-name>
   <url-pattern>*</url-pattern>
</url-mapping>

然后你会有一个像这样的 WEB-INF/spring-servlet.xml 文件:

<beans>
    <bean name="/prefix*.xml" class="org.springframework.web.servlet.mvc.ServletForwardingController">
       <property name="servletName" value="myServlet"/>
    </bean>
</beans>

就这样了。 对 /prefix*.xml 的所有请求都将发送至 myServlet,所有其他请求将发送至容器。

I would strongly suggest using a proper MVC framework for this. As you've discovered, the flexibility of the standard servlet API is very limited when it comes to request dispatching.

Ideally, you would be able to use your existing servlet code in combination with an MVC framework, with the framework doing the diapcthing based on path pattern, and your servlets doing the business logic. Luckily, Spring MVC allows you to do just that, using the ServletForwardingController. It'd be a very lightweight spring config.

So you'd have something like this in your web.xml:

<servlet>
   <servlet-name>myServlet</servlet-name>
   <servlet-class>foo.MyServlet</servlet-class>
</servlet>

<servlet>
   <servlet-name>spring</servlet-name>
   <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
</servlet>

<url-mapping>
   <servlet-name>spring</servlet-name>
   <url-pattern>*</url-pattern>
</url-mapping>

You would then have a WEB-INF/spring-servlet.xml file like this:

<beans>
    <bean name="/prefix*.xml" class="org.springframework.web.servlet.mvc.ServletForwardingController">
       <property name="servletName" value="myServlet"/>
    </bean>
</beans>

And that would be pretty much it. All requests for /prefix*.xml would go to myServlet, and all others would fall through to the container.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文