【问题标题】:Removing Query String from ServletRequest after it reaches the Servlet/Filter在 ServletRequest 到达 Servlet/Filter 后从 ServletRequest 中删除查询字符串
【发布时间】:2014-04-25 07:45:39
【问题描述】:

现在假设我触发一些 URL 说

http://shoaib:8080/userAuthentication?authenticate=false

现在当它到达Filter的doFilter方法时

public void doFilter(request,response,filterChain){

   boolean authenticate=request.getParameter("authenticate");
   .
   .   //some logic
   .
   .
   filterChain.doFilter(request,response); //this request should not have query String.

} 

将传递给filterChain's doFilter() 的请求不应该有queryString。我怎样才能做到这一点?

【问题讨论】:

  • 对此我不确定。但这行得通吗?地图参数 = request.getParameterMap(); params.remove("要删除的参数"); `
  • 我认为它不能作为 HTTP 状态 500 - 不允许对锁定的 ParameterMap 进行修改

标签: java servlets filter query-string


【解决方案1】:

我正在考虑重写 Filter 方法:

@Override
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) 
    throws ServletException, IOException {
    HttpServletRequest request = (HttpServletRequest) req;
    String requestURI = request.getRequestURI();

    if (requestURI.startsWith(YOUR_CHECK_PATH)) {
       String url = ((HttpServletRequest)request).getRequestURL().toString();
       String queryString = ((HttpServletRequest)request).getQueryString();
        req.getRequestDispatcher(url).forward(req, res);
    } else {
        chain.doFilter(req, res);
    }
}

【讨论】:

  • 但是请求不会还有查询字符串吗?
  • 所以现在下次如果你调用 request.getQueryString() 它应该是空的。我认为这不是答案
【解决方案2】:

我尝试了很多,但最后我使用 window.location 对象在客户端更改了 url

上一条路径:

http://shoaib:8080/userAuthentication?authenticate=false

客户端代码

window.location.pathname="userAuthentication";

这删除了之前的查询字符串

当前网址

http://shoaib:8080/userAuthentication

【讨论】:

    猜你喜欢
    • 2012-05-24
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-07-25
    • 1970-01-01
    • 2015-11-28
    • 1970-01-01
    • 2011-02-02
    相关资源
    最近更新 更多