为什么请求仅对接收servlet可用的调度程序参数

时间:2014-07-09 09:26:34

标签: jsp servlets servlet-filters servlet-listeners servletcontextlistener

我的index.jsp

 <%
    String token = "abc"
    request.getRequestDispatcher("/LoginServlet?Token="+token).forward(request,response);
    %>

实际上在去LoginServlet控件之前会将ServletFilter url映射到/ *(在web.xml中),所以每个请求都要经过这个ServletFilter

ServletFilter中的

request.getparameter(&#34; Token&#34;)返回null,但在LoginServlet中,令牌返回&#34; abc&#34;

请建议我如何在ServletFilter中获取令牌

1 个答案:

答案 0 :(得分:0)

我认为你对sendRedirect和转发工作感到困惑。

使用forward和sendRedirect的过滤器流程如下所示,

向前

1. Filter (Your request came here first, before JSP called, and 
   you are trying to get "Token" parameter which is no where set yet)

2. JSP (Your JSP setting parameter, as it is forward, control will not 
   go to Filter again)

3. Servlet (you are accessing parameter, which is set in JSP 
   and you got the "Token" parameter)

的sendRedirect

1. Filter(Your request came here first, before JSP called, and you are trying
   to get "Token" parameter which is no where set yet)

2. JSP(Your JSP setting parameter, as it is redirect, control will go to browser 
   back and spwan new request,which intercept by web.xml and calls Filter again) 

3. Filter (So you got data here)

4. Servlet (So you got data here) 

试试这个,看看输出

  <%

    String token = "abc";
    // Below 2 lines, try commenting each and see flow
    //request.getRequestDispatcher("/LoginServlet?Token="+token).forward(request,response);   
    response.sendRedirect("LoginServlet?Token="+token);

   %>