标签:system span attr 增加 add web charset 需要 col
注解配置
一.注解配置使用
配置 servlvet 的映射路径, 全都在 web.xml 文件中, 非常不方便维护.
注解就是在每个 servlet 之上进行路径的配置
总结:
使用注解配置 servlet的映射路径: @WebServlet
使用注解配置过滤器: @WebFilter
使用注解配置监听器: @WebListener
注解配置映射路径
Demo: 注解配置 servlet
1 @WebServlet(urlPatterns= {"/emp/*","/dept/*"}) 2 public class EmpServlet extends HttpServlet{ 3 @Override 4 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 5 System.out.println("使用注解配置的 servlet"); 6 } 7 @Override 8 protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { 9 this.doGet(req, resp); 10 } 11 }
Demo: 注解配置过滤器
1 @WebFilter(urlPatterns= {"/*"}) 2 public class MyFilter implements Filter { 3 @Override 4 public void init(FilterConfig filterConfig) throws ServletException { 5 System.out.println("过滤器初始化"); 6 } 7 @Override 8 public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) 9 throws IOException, ServletException { 10 System.out.println("对拦截的请求进行处理,之后放行"); 11 //向下转型 12 HttpServletRequest req = (HttpServletRequest)request; 13 HttpServletResponse resp = (HttpServletResponse)response; 14 //处理字符编码 15 req.setCharacterEncoding("utf-8"); 16 resp.setContentType("text/html;charset=utf-8"); 17 chain.doFilter(req, resp); 18 } 19 @Override 20 public void destroy() { 21 System.out.println("过滤器销毁"); 22 } 23 }
Demo: 注解配置过滤器 (登录验证)
@WebFilter(urlPatterns= {"/emp/delete","/emp/updat","/emp/list","/emp_list.jsp","/dept/list"}) public class LoginFilter implements Filter{ }
如果有几百个资源(比如 jsp)需要登录验证, 可以将要进行登录验证的资源统一保存到一个文件夹 (如:pages)
然后过滤器只要对该文件夹进行验证即可.
Demo: 统一要验证的资源
@WebFilter(urlPatterns= {"/emp/delete","/emp/updat","/pages/*"})
Demo: 注解配置监听器
1 @WebListener 2 public class RequestAttributeListener implements ServletRequestAttributeListener{ 3 @Override 4 public void attributeAdded(ServletRequestAttributeEvent event) { 5 System.out.println("增加属性时候触发: " + event.getName() + ":" + event.getValue()); 6 } 7 @Override 8 public void attributeRemoved(ServletRequestAttributeEvent event) { 9 System.out.println("删除属性时候触发: " + event.getName() + ":" + event.getValue()); 10 } 11 @Override 12 public void attributeReplaced(ServletRequestAttributeEvent event) { 13 System.out.println("替换属性时候触发: " + event.getName() + ":" + event.getValue()); 14 } 15 }
标签:system span attr 增加 add web charset 需要 col
原文地址:https://www.cnblogs.com/yslf/p/10755423.html