2014-03-19 49 views
1

後,現在假設我火了一些URL說從ServletRequest中刪除查詢字符串達到了Servlet /過濾

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

現在,當它達到過濾

public void doFilter(request,response,filterChain){ 

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

} 

的要求,這將是doFilter方法傳遞給filterChain's doFilter()不應該有queryString。我如何實現這一目標?

+0

我不知道這件事。但這會工作嗎? Map params = request.getParameterMap(); params.remove(「parameter-to-remove」); ' –

+0

我認爲它不會像HTTP狀態500那樣工作 - 不允許修改鎖定參數映射 –

回答

0

我在想重寫篩選方法:

@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); 
    } 
} 
+1

但是請求是否仍然有查詢字符串? –

+0

所以現在下次如果你調用request.getQueryString()它應該是null。我認爲這不是一個答案 –

+0

正在編輯...對不起。 –

0

我嘗試了很多,但最後我用window.location的對象

上一頁路徑更改URL在客戶端:

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

客戶端代碼

window.location.pathname="userAuthentication"; 

這樣取出之前的查詢字符串

當前URL

http://shoaib:8080/userAuthentication