简体   繁体   English

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

[英]Java Servlet: pass a request back to default processing

I want a Servlet to handle requests to files depending on prefix and extension, eg 我希望Servlet根据前缀和扩展名来处理对文件的请求,例如

prefix_*.xml 前缀_ *。xml的

Since mapping on beginning AND end of request path is not possible, I have mapped all *.xml requests to my Servlet. 由于无法在请求路径的开头和结尾进行映射,因此我已将所有* .xml请求映射到我的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? 现在的问题是:如何针对非以“ prefix_”开头的XML文件退出servlet,以便像处理XML文件的“普通”请求一样处理请求?

This is probably quite simple but I do not seem to be able to find this out... :-/ 这可能很简单,但我似乎无法找到...:-/

Thanks a lot in advance 提前谢谢

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. 另一个解决方案(也许适合您)是,如果您正在/计划在该Web容器实例之前使用Apache,则可以使用apache的重写模块。 Rewriting the url to something more easy to handle for the Webapp container. 将URL重写为更易于处理的Webapp容器。

Hope this helps. 希望这可以帮助。 David. 大卫。

Not shure, but once you catch all *.xml requests you can inspect the request again in your code via HttpServletRequest.getRequestURI() 不是很清楚,但是一旦捕获了所有* .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
        }
    }

(code not tested, only an idea ...) (代码未经测试,只是一个主意...)

I would strongly suggest using a proper MVC framework for this. 我强烈建议为此使用适当的MVC框架。 As you've discovered, the flexibility of the standard servlet API is very limited when it comes to request dispatching. 正如您所发现的,在请求分派时,标准Servlet API的灵活性非常有限。

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. 理想情况下,您将能够将现有的Servlet代码与MVC框架结合使用,该框架基于路径模式进行透彻,而Servlet则进行业务逻辑。 Luckily, Spring MVC allows you to do just that, using the ServletForwardingController. 幸运的是,Spring MVC允许您使用ServletForwardingController做到这一点。 It'd be a very lightweight spring config. 这将是一个非常轻量级的spring配置。

So you'd have something like this in your web.xml: 因此,您的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: 然后,您将获得一个WEB-INF / spring-servlet.xml文件,如下所示:

<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. 对/prefix*.xml的所有请求都将发送到myServlet,而所有其他请求将落入容器。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM