Java Servlet:将请求传递回默认处理
我希望 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
不放心,但是一旦捕获所有 *.xml 请求,您可以通过 HttpServletRequest.getRequestURI() 在代码中再次检查请求
(代码未经测试,仅是一个想法......)
Not shure, but once you catch all *.xml requests you can inspect the request again in your code via HttpServletRequest.getRequestURI()
(code not tested, only an idea ...)
另一个解决方案(也许适合您)是,如果您正在/计划在该 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.
我强烈建议为此使用适当的 MVC 框架。 正如您所发现的,标准 servlet API 在请求分派方面的灵活性非常有限。
理想情况下,您将能够将现有的 servlet 代码与 MVC 框架结合使用,框架基于路径模式执行 diapcthing,而 servlet 执行业务逻辑。 幸运的是,Spring MVC 允许您使用 ServletForwardingController 来做到这一点。 这将是一个非常轻量级的弹簧配置。
所以你的 web.xml 中会有这样的内容:
然后你会有一个像这样的 WEB-INF/spring-servlet.xml 文件:
就这样了。 对 /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:
You would then have a WEB-INF/spring-servlet.xml file like this:
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.