How to get Servlet class from URI
I'm in a Filter implementation and have a response which will call a redirect. Now I try to determine which Servlet class the redirect URL will call. (without call Servlet itself - I only want to know which class)
like:
public void doFilter( ServletReq开发者_如何学编程uest request, ServletResponse response, FilterChain filterChain ) throws IOException, ServletException
{
...
filterChain.doFilter( request, response );
//now the response is a redirect
if (redirect) {
Class servletClass = request.getRequestDispatcher( response.getString() ).[getServletClass()]; //or something like
...
}
}
I need to know if it's a redirect AND if a specific servlet is called, cause i must manipulate the response BEFORE the redirect.
Exactly:
- an request reach the filter. (It's an XmlHttpRequest, but i think it doesn't matter)
- the response will cause an redirect.
- the response comes to browser and there I see in JavaScript "hey it's a redirect - but which servlet will accessed?" - I need this information at this time in JavaScript. So I try to put some extra information to the response.
Now I try to determine which Servlet class the redirect URL will call
Since your ultimate goal is just to redirect, I think using response#sendRedirect in the servlet you're redirecting from is an easier way to do this all together. Redirecting this way will allow you to avoid this:
if (redirect) {
Class servletClass = request.getRequestDispatcher( response.getString() ).[getServletClass()]; //or something like
...
}
Filters aren't really meant for directing requests to the proper servlets. I admit though that this often ends up being done because the servlet-mapping xml element in web.xml isn't very robust. Despite this, its best to stick to having your web xmls map which urls go to which servlets - when possible - because it is declarative and easier to maintain.
精彩评论