Home > Article > Web Front-end > Simple application of ajax and how to write filter
Simple use of ajax:
function dss(userId,obj){ $.post("test?flag=2",{id:userId},function(data) if("删除成功"==data){ alert("删除成功"); $(obj).parent().parent().remove(); } }); }
filter: Filter in Java is not a standard Servlet. It cannot handle user requests or generate responses to the client. It is mainly used for preprocessing HttpServletRequest and can also be used for postprocessing HttpServletResponse. It is a typical processing chain.
Advantages: The advantage of the filter chain is that it can be interrupted at any time during execution. As long as chain.doFilter() is not executed, subsequent filters and requested content will not be executed. In actual use, special attention must be paid to the execution order of the filter chain.
web.xml configuration filter:
##
<filter> <filter-name>FirstFilter</filter-name> <filter-class>com.jkx.web.filter.FirstFilter</filter-class> <init-param> <param-name>encoding</param-name> <param-value>UTF-8</param-value> </init-param> </filter> <filter-mapping> <filter-name>FirstFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>Write Filter :
package com.jkx.web.filter; import java.io.IOException; 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.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class FirstFilter implements Filter{ public String encoding=null; //服务器正常关闭时执行 public void destroy() { System.out.println("FirstFilter销毁"); } public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { HttpServletRequest req = (HttpServletRequest) request; HttpServletResponse resp = (HttpServletResponse) response; req.setCharacterEncoding(encoding); req.setAttribute("fileParam", "222222"); chain.doFilter(req, resp); } //服务器启动时初始化 public void init(FilterConfig filterConfig) throws ServletException { System.out.println("FirstFilter初始化,被创建"); encoding = filterConfig.getInitParameter("encoding"); } }
The above is the detailed content of Simple application of ajax and how to write filter. For more information, please follow other related articles on the PHP Chinese website!