0

I'd like to map a servlet for every url ending with "/jsfinspector". For example:

  • http://localhost/myapp/pages/somewhere/jsfinspector
  • http://localhost/myapp/jsfinspector

Is it possible to do that? In a very simple way, without declaring all possible url patterns in web.xml?

2 Answers 2

1

The Servlet API doesn't support that.

Your best bet is creating a @WebFilter("/*") which forwards to @WebServlet("/jsfinspector") when the URL matches, as shown below:

if (request.getRequestURI().endsWith("/jsfinspector")) {
    request.getRequestDispatcher("/jsfinspector").forward(request, response);
} else {
    chain.doFilter(request, response);
}

You can if necessary extract the original request URI in servlet as below:

String originalRequestURI = (String) request.getAttribute(RequestDispachter.FORWARD_REQUEST_URI);
Sign up to request clarification or add additional context in comments.

1 Comment

Thank you BalusC! It helps a lot ;)
0

You could think about creating a filter to intercept every request and eventually redirect the flow. https://docs.oracle.com/javaee/6/tutorial/doc/bnagb.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.