ajax的简单应用及filter的编写

时间:2021-03-11 18:28:03
ajax的简单运用:function dss(userId,obj){
  $.post("test?flag=2",{id:userId},function(data){
  if("删除成功"==data){
  alert("删除成功");
  $(obj).parent().parent().remove();
  }
  });
  }

filter:Java中的Filter 并不是一个标准的Servlet ,它不能处理用户请求,也不能对客户端生成响应。 主要用于对HttpServletRequest 进行预处理,也可以对HttpServletResponse 进行后处理,是个典型的处理链。
优点:过滤链的好处是,执行过程中任何时候都可以打断,只要不执行chain.doFilter()就不会再执行后面的过滤器和请求的内容。而在实际使用时,就要特别注意过滤链的执行顺序问题.


web.xml配置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>
编写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"); 
}


} ajax的简单应用及filter的编写ajax的简单应用及filter的编写ajax的简单应用及filter的编写ajax的简单应用及filter的编写ajax的简单应用及filter的编写