過濾器過濾敏感字元
阿新 • • 發佈:2019-02-14
1.專案結構
2.1EncodingFilter
2.2LoginServletpackage com; import java.io.IOException; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; import java.util.ArrayList; import java.util.List; import javax.servlet.Filter; import javax.servlet.FilterChain; import javax.servlet.FilterConfig; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.annotation.WebFilter; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet Filter implementation class EncodingFilter */ @WebFilter(urlPatterns = "/*") public class EncodingFilter implements Filter { private List<String> list; /** * Default constructor. */ public EncodingFilter() { } /** * @see Filter#destroy() */ public void destroy() { } /** * @see Filter#doFilter(ServletRequest, ServletResponse, FilterChain) */ public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { // TODO Auto-generated method stub // place your code here // System.out.println("EncodingFilter生效"); HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; // 解決post中文亂碼問題 req.setCharacterEncoding("utf-8"); resp.setContentType("text/html;charset=utf-8"); // 解決get中文亂碼問題 HttpServletRequest proxy = (HttpServletRequest) Proxy.newProxyInstance(req.getClass().getClassLoader(), new Class[] { HttpServletRequest.class }, new InvocationHandler() { @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { String methodName = method.getName(); if ("getParameter".equals(methodName)) { // 解決get中文亂碼問題 // 接受此方法的引數 String value=req.getParameter(args[0].toString()); // 判斷請求方式-獲取當前請求的方式 GET POST String methodSubmit=req.getMethod(); if("GET".equals(methodSubmit)) { // 非空判斷 if(value!=null && value.length()>0) { // 對接受到的引數進行編碼 value=new String(value.getBytes("ISO-8859-1"),"utf-8"); } } for(String str:list) { if(value.contains(str)) { value=value.replace(str, "*"); } } return value; } else { Object returnValue = method.invoke(req, args); return returnValue; } } }); // pass the request along the filter chain chain.doFilter(proxy, response); } /** * @see Filter#init(FilterConfig) */ public void init(FilterConfig fConfig) throws ServletException { list=new ArrayList<String>(); list.add("妹"); list.add("媽"); list.add("蛋"); } }
2.3 login.jsppackage com; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; /** * Servlet implementation class LoginServlet */ @WebServlet("/LoginServlet") public class LoginServlet extends HttpServlet { private static final long serialVersionUID = 1L; /** * @see HttpServlet#HttpServlet() */ public LoginServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String msg=request.getParameter("msg"); request.setAttribute("result", msg); request.getRequestDispatcher("/msg.jsp").forward(request, response);; } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub doGet(request, response); } }
2.4 msg.jsp<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <form action="${pageContext.request.contextPath}/LoginServlet" method="post"> <input type="text" name="msg"/> <input type="submit" value="測試"/> </form> </body> </html>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
顯示頁面的資訊:${result }
</body>
</html>