5

I have two static wars filled with help files served by a Jetty server and a root context war.

  • help.war - English help files
  • help_CS.war - Czech help files
  • ROOT.war

    Based on the locale of the request, I want to divert a user to the language relevant to them. i.e. user requests /help/index.htm and as they are requesting from a Czech locale, they get /help_CS/index.htm. The idea is that language packs can be added as required without too much fuss.

    I tried adding a custom RewriteHandler, referred to in Jetty.xml which grabs the locale from the Request and either forwards or redirects on handle(). Both complain as response codes have been sent by this point...somehow?!

    I tried a custom Filter in the web.xml of the ROOT.war which I couldn't get to match */help/** no matter what variation of the url-pattern I tried.

    I then added a reference to the same Filter as the last attempt into WEB-INF/web.xml to my help.war which would match and URLS could be generated but I can't rewrite the URL at this point because it is always prepended by /help/ so the URL with help replaced with help_CS ends up as domain/help/help_CS/index.htm.

    So my question. How should/could this be done?

4

1 回答 1

1

所以!在搞砸了几天之后,我发现了一些感觉有点hacky但它​​有效的东西。

我使用我的自定义过滤器,然后将它放在每个help_XX.war的WEB_INF/web.xml中,每个 war 文件都有一个单独的servlet 映射(但其他方面相同)

<servlet-mapping>
    <servlet-name>default</servlet-name>
    <url-pattern>/help_CS</url-pattern>
</servlet-mapping>

然后在过滤器中,我得到所需战争的ServletContext并使用它转发,像这样从请求地址中手动删除/help

  @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
                                throws IOException, ServletException
  {
    HttpServletRequest request = (HttpServletRequest) req;
    String requestAddress = request.getRequestURI();
    String country_code = req.getLocale().getCountry();

    if (requestAddress.contains("/help/")) 
    {
        ServletContext forwardContext = config.getServletContext().getContext("/help_" + country_code);

        forwardContext.getRequestDispatcher(requestAddress.replace("/help", "")).forward(req, res);
    } 
    else 
    {
        chain.doFilter(req, res);
    }
  }
于 2013-10-17T11:14:03.263 回答