文件名称:java servlet 监听器
文件大小:243KB
文件格式:PPT
更新时间:2014-11-28 16:29:40
servlet 监听器 ppt
servelt 监听器 class MyRequest extends HttpServletRequestWrapper { public MyRequest(HttpServletRequest request) { super(request); } public String MyEncoding(String value) { String value1 = null; try { value1 = new String(value.getBytes("iso-8859-1"), encoding); } catch (UnsupportedEncodingException e) { // TODO Auto-generated catch block e.printStackTrace(); } return value1; } @Override public String getParameter(String name) { return MyEncoding(getHttpServletRequest().getParameter(name)); } @Override public String[] getParameterValues(String name) { String[] values = getHttpServletRequest().getParameterValues(name); if (values != null) { for (int i = 0; i < values.length; i++) { values[i] = MyEncoding(values[i]); } } return values; } /** * 从HttpServletRequestWrapper包装类中取出我们要进行转码的request; * * @return */ public HttpServletRequest getHttpServletRequest() { return (HttpServletRequest) super.getRequest(); } } /** * Default constructor */ public CharacterEncodingFilter() { } @Override public void init(FilterConfig filterConfig) throws ServletException { encoding = filterConfig.getInitParameter("encoding"); } @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { MyRequest myRequest = null; HttpServletRequest request2 = (HttpServletRequest) request; if (request2.getMethod() == "post") { request2.setCharacterEncoding(encoding); } else { myRequest = new MyRequest(request2); } chain.doFilter(myRequest, response); } @Override public void destroy() { System.out.println("destroy被执行啦!"); } }