开发者

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:

  1. an request reach the filter. (It's an XmlHttpRequest, but i think it doesn't matter)
  2. the response will cause an redirect.
  3. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜