zoukankan      html  css  js  c++  java
  • 关于servlet的@WebServlet注解的使用

    一、Servlet的传统配置方式

    web.xml中的servlet配置

     发一个Servlet,都要在web.xml中配置Servlet才能够使用,就比较麻烦。所以Servlet3.0之后提供了注解(annotation)

    二、使用注解方式配置Servlet

    @WebServlet注解用于标注在一个继承了HttpServlet类之上,属于类级别的注解。

    用法形如:

    @WebServlet("/RegistServlet")

    public class RegistServlet extends HttpServlet{

      //处理 GET 方法请求的方法

      public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}

      //处理POST方法请求的方法

      protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {}

    }

    该注解的作用等价于 在web.xml中配置的该servlet的<servlet-mapping>元素中<url-pattern>的配置

    三、@WebServlet注解属性

    (1).loadOnStartup属性:标记容器是否在启动应用时就加载Servlet,默认不配置或数值为负数时表示客户端第一次请求Servlet时再加载;0或正数表示启动应用就加载,正数情况下,数值越小,加载该Servlet的优先级越高;
    (2).name属性:可以指定也可以不指定,通过getServletName()可以获取到,若不指定,则为Servlet的完整类名
    (3).urlPatterns/value属性: String[]类型,可以配置多个映射,如:urlPatterns={"/user/test", "/user/example"}
    (4).urlPatterns的常用规则:
    • /*或者/:拦截所
    • *.do:拦截指定后缀
    • /user/test:拦截路径
    • /user/*.do、/*.do、test*.do都是非法的,启动时候会报错
  • 相关阅读:
    肩部肌肉劳损zt
    大屏幕手机上网页字体显示很小的问题
    SWT的Display
    The connection to adb is down, and a severe error has occured.
    [ZT]使用tmpfs缓存文件提高性能
    Mutex
    javascript阻塞加载问题【转】
    IE参考
    2台电脑网线对接注意的事项
    重建索引
  • 原文地址:https://www.cnblogs.com/whluan/p/12145756.html
Copyright © 2011-2022 走看看