繁体   English   中英

无法使用Java在Filter中获取请求参数

[英]Not able to get request parameters in Filter using Java

我正在尝试通过在调用servlet之前传递要验证的参数来调用过滤器。 但是,请求参数传递为null。 在我的servlet中接收相同的参数时。 我不知道可能是什么原因。

我对Servlet的调用将使用Javascript中的window.open。

这是我的代码

window.open ("http://localhost:8080/SEMS/testAdd?yearFormat=yyyy", "hiddenFrame");

我的过滤器

public class CBFilter implements Filter {

public void init(FilterConfig config) throws ServletException {     

}

public CBFilter() {}

public void destroy() {
}

public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
    String yearFormat= request.getParameter("yearFormat");

    System.out.println("[Filter] Year Format : "+yearFormat);

    chain.doFilter(request, response);
}

}

Web.xml

  <filter>
   <display-name>CBFilter</display-name>
   <filter-name>CBFilter</filter-name>
   <filter-class>com.cb.CBFilter</filter-class>     
  </filter>
  <filter-mapping>
    <filter-name>CBFilter</filter-name>
    <url-pattern>/*</url-pattern>
   </filter-mapping>

Servlet中提供了相同的请求参数。

尝试将ServletRequest强制转换为HttpServletRequest如下所示:

HttpServletRequest req = (HttpServletRequest)request;
String yearFormat= req.getParameter("yearFormat");

..看看是否有帮助

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM