Ross Drew
Ross Drew

Reputation: 8246

How does a webapp forward to another webapp?

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

Upvotes: 5

Views: 1599

Answers (1)

Ross Drew
Ross Drew

Reputation: 8246

So! After a few days of messing around with this I've found something that feels a little hacky but it works.

I use my custom Filter then put it in WEB_INF/web.xml for each help_XX.war with an individual servlet-mapping (but otherwise identical) for each war file as so

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

Then inside the Filter I get the ServletContext of the required war and forward using that, manually removing /help from the request address like so

  @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);
    }
  }

Upvotes: 1

Related Questions