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都是非法的,启动时候会报错
  • 相关阅读:
    CentOS7使用firewalld打开关闭防火墙与端口
    ssh连接卡在【To escape to local shell, press 'Ctrl+Alt+]'.】的解决方法
    本地链路地址
    RIFF和WAVE音频文件格式
    声音分贝的概念,dBSPL.dBm,dBu,dBV,dBFS
    VS中C++ 项目重命名
    FFmpeg学习6:视音频同步
    FFmpeg学习5:多线程播放视音频
    FFmpeg学习4:音频格式转换
    FFmpeg数据结构:AVPacket解析
  • 原文地址:https://www.cnblogs.com/whluan/p/12145756.html
Copyright © 2011-2022 走看看