通用配置
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注解常用属性
属性 | 作用 |
---|---|
name | Servlet名字(可选) |
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被生成到文档中
只需要直接写资源路径即可