Ideas: in the jsp settings form, the form Jump to access the servlet, but the middle of the filter, the filter is provided with a sensitive word to intercept
sensitive word then prompts the user to jump to another page, the word is not sensitive to the normal jump to the servlet.
Procedure:
1.jsp of:
<form action="comment.do"> username:<input type="text" name="username"/> comment:<textarea name="comment"></textarea> <button>提交评论</button> </form>
2. doFilter filter's method:
// set the character set // not garbled page from the character input Request.setCharacterEncoding ( "UTF-. 8" ); // character output from the servlet does not garbled response.setContentType ( "text / html; charset = utf- 8 " ); // acquisition request parameter String = request.getParameter keyword (" Comment " ); System.out.println (keyword); IF (keyword.contains (" XXX ")! = & keyword null ) { // Note: It is not the HttpServletRequest HttpServletResponse // and ServletResponse is HttpServletResponse parent HttpServletResponse RESP = (HttpServletResponse) Response; // Redirect jump page resp.sendRedirect ( "the error.jsp" ); } the else { the chain.doFilter (Request, Response); } }
3. The existence of sensitive words jump page error.jsp, the contents of self
4. there is no sensitive words, normal jump to the servlet, self-made content