WEB3.0版本之后出现了注解,为了替代web.xml配置

@WebServlet

1.使用注解的方式访问Servlet

  1. 自己写一个类Servlet,类继承HttpServlet extends HttpServlet)<br /> 类重写方法<br /> 方法参数 抛出异常<br /> 配置 在类上 添加一个注解<br /> @WebServlet()<br /> urlPatterns={"/test","/test2"}<br /> 如果请求url只有一个的话 大括号是可以省略的<br /> value信息 等价于urlPatterns <br /> 建议用value如果只有value是可以生路的 <br /> loadOnStartUp信息 控制servlet对象的加载的(立即)<br /> initParams 为了携带信息<br /> 里面需要{@WebInitParam(name="",value="")}
  1. package controller;
  2. import javax.servlet.ServletConfig;
  3. import javax.servlet.ServletException;
  4. import javax.servlet.annotation.WebInitParam;
  5. import javax.servlet.annotation.WebServlet;
  6. import javax.servlet.http.HttpServlet;
  7. import javax.servlet.http.HttpServletRequest;
  8. import javax.servlet.http.HttpServletResponse;
  9. import java.io.IOException;
  10. import java.util.Enumeration;
  11. @WebServlet(
  12. // name = "test",
  13. // description = "类似注释",
  14. // displayName = "显示请求名",
  15. // asyncSupported = true,
  16. // value = "",
  17. urlPatterns = {"/test",""},
  18. loadOnStartup = 0,
  19. initParams = {
  20. @WebInitParam(name = "zhangsan",value = "333"),
  21. @WebInitParam(name = "lisi",value = "444"),
  22. @WebInitParam(name = "wangwu",value = "555"),
  23. }
  24. )
  25. public class TestController extends HttpServlet {
  26. public void init(ServletConfig config) throws ServletException {
  27. System.out.println("testController对象加载ing...");
  28. Enumeration en = config.getInitParameterNames();
  29. while (en.hasMoreElements()){
  30. String key = (String) en.nextElement();
  31. String value = config.getInitParameter(key);
  32. System.out.println(key+"--"+value);
  33. }
  34. }
  35. protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  36. System.out.println("This is a TestController,执行ing...");
  37. String name = request.getParameter("name");
  38. String pass = request.getParameter("pass");
  39. System.out.println(name+"--"+pass);
  40. }
  41. }

@WebFilter

2.使用注解的方式访问Filter

  1. 自己写一个类,类继承HttpFilter extends HttpFilter )<br /> 重写方法doFilter<br /> 方法参数 request respons chain 抛异常<br /> 配置 类上添加注解<br /> @WebFilter<br /> urlPatterns<br /> value<br /> initParams 为了携带信息<br /> 里面需要{@WebInitParam(name="",value="")}
  1. package filter;
  2. import javax.servlet.FilterChain;
  3. import javax.servlet.FilterConfig;
  4. import javax.servlet.ServletException;
  5. import javax.servlet.annotation.WebFilter;
  6. import javax.servlet.annotation.WebInitParam;
  7. import javax.servlet.http.HttpFilter;
  8. import javax.servlet.http.HttpServletRequest;
  9. import javax.servlet.http.HttpServletResponse;
  10. import java.io.IOException;
  11. import java.util.Enumeration;
  12. @WebFilter(
  13. // description = "",
  14. // displayName = "",
  15. // value = {"","",""};
  16. urlPatterns = {"/*",""},
  17. initParams = {
  18. @WebInitParam(name = "key1",value = "value1"),
  19. @WebInitParam(name = "key2",value = "value2")
  20. }
  21. )
  22. //@WebFilter("/*")
  23. public class TestFilter extends HttpFilter {
  24. public void init(FilterConfig filterConfig) throws ServletException {
  25. System.out.println("This is a Filter,真正init方法加载ing```");
  26. Enumeration en = filterConfig.getInitParameterNames();
  27. while (en.hasMoreElements()){
  28. String key = (String) en.nextElement();
  29. String value = filterConfig.getInitParameter(key);
  30. System.out.println(key+"--"+value);
  31. }
  32. }
  33. protected void doFilter(HttpServletRequest request, HttpServletResponse response, FilterChain chain) throws IOException, ServletException {
  34. System.out.println("This is a Filter,真正doFilter方法执行ing...");
  35. chain.doFilter(request,response);
  36. }
  37. }

@WebListener

3.使用注解的方式让Listener执行

  1. 自己写一个类,实现接口 impliments ServletRequestListener)<br /> 方法必须重写<br /> 配置 类上添加注解<br /> @WebListener
  1. package listener;
  2. import javax.servlet.ServletRequestEvent;
  3. import javax.servlet.ServletRequestListener;
  4. import javax.servlet.annotation.WebListener;
  5. @WebListener()
  6. public class TestRequestListener implements ServletRequestListener {
  7. public void requestDestroyed(ServletRequestEvent sre) {
  8. System.out.println("request对象销毁了");
  9. }
  10. public void requestInitialized(ServletRequestEvent sre) {
  11. System.out.println("request对象创建了");
  12. }
  13. }

image.png