03 Servlet的两种配置方式(通用配置和注解配置)

通用配置

Servlet 2.5之前需要通过配置web.xml来实现Servlet的配置

  • 这种配置方式依然适用

配置Servlet

  • 添加servlet节点
  • 添加servlet-mapping节点(映射配置)
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

       <!-- servlet配置 -->
    <!-- 1.添加servlet节点 -->
    <servlet>
        <!-- 名称 -->
        <servlet-name>test1</servlet-name>
        <!-- servlet的全类名称 -->
        <servlet-class>com.example.test.test1</servlet-class>
        <!-- 启动的优先级,数字越小越先起作用 -->
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- 2.添加servlet-mapping节点 -->
    <!-- 映射配置 -->
    <servlet-mapping>
        <!-- 名称 -->
        <servlet-name>test1</servlet-name>
        <!-- 资源匹配(精确匹配)-->
        <url-pattern>/web-test1</url-pattern>
    </servlet-mapping>

</web-app>

load-on-startup

  • 是否应该在web程序启动的时候就加载这个Servlet
  • 数值必须为正数,表示Servlet被加载的先后顺序
  • 数值为负数或者没有设置,则容器会当Servlet被请求时才会加载
  • 数值为正数或者0时,则容器在启动时加载并初始化Servlet
  • 数值越小,优先级越高,数值相同时,容器会自动选择顺序加载

url-pattern匹配规则

匹配格式作用
精确匹配/具体名称只有URL的路径名称是具体名称才会触发Servlet
后缀匹配*.xxx只要是以xxx结尾的路径才会触发**Servlet
通配符匹配*匹配所有请求,包含服务器的所有资源
通配符匹配/匹配所有请求,包含服务器的所有资源,不包括.jsp

注解配置

Servlet3.0支持注解配置。可以没有web.xml

@WebServlet注解常用属性

属性作用
nameServlet名字(可选)
value配置URL路径(可配置多个)
urlPatterns和value一样但不能同时使用
load-on-startup配置Servlet创建时间

测试

在这里插入图片描述
在这里插入图片描述在这里插入图片描述

在这里插入图片描述

注解配置分析

源码

package javax.servlet.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Target;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Retention;
import java.lang.annotation.Documented;

/**
 * Annotation used to declare a servlet.
 *
 * <p>This annotation is processed by the container at deployment time,
 * and the corresponding servlet made available at the specified URL
 * patterns.
 * 
 * @see javax.servlet.Servlet
 *
 * @since Servlet 3.0
 */
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface WebServlet {
    
    /**
     * The name of the servlet
     *
     * @return the name of the servlet
     */
    String name() default "";
    
    /**
     * The URL patterns of the servlet
     *
     * @return the URL patterns of the servlet
     */
    String[] value() default {};

    /**
     * The URL patterns of the servlet
     *
     * @return the URL patterns of the servlet
     */
    String[] urlPatterns() default {};
    
    /**
     * The load-on-startup order of the servlet 
     *
     * @return the load-on-startup order of the servlet
     */
    int loadOnStartup() default -1;
    
    /**
     * The init parameters of the servlet
     *
     * @return the init parameters of the servlet
     */
    WebInitParam [] initParams() default {};
    
    /**
     * Declares whether the servlet supports asynchronous operation mode.
     *
     * @return {@code true} if the servlet supports asynchronous operation mode
     * @see javax.servlet.ServletRequest#startAsync
     * @see javax.servlet.ServletRequest#startAsync(ServletRequest,
     * ServletResponse)
     */
    boolean asyncSupported() default false;
    
    /**
     * The small-icon of the servlet
     *
     * @return the small-icon of the servlet
     */
    String smallIcon() default "";

    /**
     * The large-icon of the servlet
     *
     * @return the large-icon of the servlet
     */
    String largeIcon() default "";

    /**
     * The description of the servlet
     *
     * @return the description of the servlet
     */
    String description() default "";

    /**
     * The display name of the servlet
     *
     * @return the display name of the servlet
     */
    String displayName() default "";

}

分析

  • @Target({ElementType.TYPE})目标作用在类上
  • @Retention(RetentionPolicy.RUNTIME)注解保留在运行期
  • @Documented被生成到文档中

只需要直接写资源路径即可

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值