Eclipse中使用自定义模板来弥补Myeclipse没有新建Filter的功能
Myeclipse插件,并没有为我们提供Filter的新建功能,为此我们不得不每次都去新建个类,然后输入那繁琐的重复代码,这完全就是浪费时间,时间应该花在更有用的事情上,而不是这类重复操作,象下面这些内容如果要自己来写,完全时浪费时间,我们完全可以创建一个模板来让它自动帮我们完成以下内容
package test;import java.io.IOException;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.Filter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class FilterTemplate implements Filter{/** * Constructor of the object. */public FilterTemplate(){super();}private FilterConfig filterConfig;// Handle the passed-in FilterConfigpublic void init(FilterConfig filterConfig) throws ServletException{this.filterConfig = filterConfig;}public void doFilter(ServletRequest req, ServletResponse res,FilterChain filterChain){try{HttpServletRequest request = (HttpServletRequest) req;HttpServletResponse response = (HttpServletResponse) res;HttpSession session = request.getSession();// 这里放到达目的地前(进入)处理代码filterChain.doFilter(req, res);// 这里放到达目的地(离开)的处理代码}catch (IOException e){e.printStackTrace();}catch (ServletException e){e.printStackTrace();}}/** * Destruction of the servlet. <br> */public void destroy(){// Just puts "destroy" string in log// Put your code here}}
package ${enclosing_package} ;import java.io.IOException;import javax.servlet.FilterChain;import javax.servlet.FilterConfig;import javax.servlet.ServletException;import javax.servlet.ServletRequest;import javax.servlet.ServletResponse;import javax.servlet.Filter;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.servlet.http.HttpSession;public class ${primary_type_name} implements Filter{/** * Constructor of the object. */public ${primary_type_name}(){super();}private FilterConfig filterConfig;// Handle the passed-in FilterConfigpublic void init(FilterConfig filterConfig) throws ServletException{this.filterConfig = filterConfig;}public void doFilter(ServletRequest req, ServletResponse res, FilterChain filterChain){try{HttpServletRequest request = (HttpServletRequest) req;HttpServletResponse response = (HttpServletResponse) res;HttpSession session = request.getSession();// 这里放到达目的地前(进入)处理代码filterChain.doFilter(req,res);// 这里放到达目的地(离开)的处理代码} catch (IOException e){e.printStackTrace();} catch (ServletException e){e.printStackTrace();}}/** * Destruction of the servlet. <br> */public void destroy(){// Just puts "destroy" string in log// Put your code here}}