当前位置:网站首页>@ WebServlet annotations (Servlet annotations)
@ WebServlet annotations (Servlet annotations)
2022-07-30 04:20:00 【m0_54861649】
@WebServlet annotation
Article Directory
- @WebServlet annotation
- Foreword
- First, the attributes of the @WebServlet annotation
- Second, the use of @WebServlet annotation
- The difference between @WebServlet annotation and web.xml
Foreword
In Servlet, the role of web.xml is very important. It can centrally manage the configuration of all Servlets. However, if the number of Servelts in the project is large, the configuration of web.xml will become very verbose.In this case, Annotation is a better choice.
Tips: The following is the text of this article, and the following cases are for reference
First, the attributes of the @WebServlet annotation

Second, the use of @WebServlet annotation
1. Enable annotation support
There is an attribute in the top-level tag ofweb.xml: metadata-complete, which is used to specify whether the current web.xml is complete.If this property is set to true, the container will only rely on web.xml when deploying, ignoring all annotations.If this property is not configured, or set to false, annotation support is enabled.
Since the default value of the metadata-complete attribute is false, that is, Servlet annotation support is enabled by default, so by default, when using this annotation, there is no need to create a web.xml file.
2. Use @WebServlet annotation
@WebServlet is a class-level annotation, which is annotated on the class that inherits HttpServlet.The common way of writing is to write the relative request path (ie value) of the servlet directly in the annotation,
@WebServlet(urlPatterns = “/MyServlet”).
@WebServlet(“/MyServlet”) omits the urlPatterns attribute name
If multiple attributes need to be set in @WebServlet, they must be separated by commas.
Created by implementing Serlvet interface or inheriting GenericServletServlet classes cannot be annotated with @WebServlet.
Use the servlet class configured with the @WebServlet annotation, do not configure the servlet related properties again in the web.xml file.If both web.xml and @WebServlet are used to configure the same servlet class, the value in web.xml cannot be the same as the value of name in the annotation, otherwise the container will ignore the configuration in the annotation.
The difference between the @WebServlet annotation and web.xml
Use web.xml or @WebServlet annotation to configure Servlet
@WebServlet annotation to configure Servlet
Advantages: @WebServlet can be used directly in the Servlet class, with less code and simple configuration.Each class only focuses on its own business logic and does not interfere with other servlet classes, which is suitable for multiple people to develop at the same time.
Disadvantage: When there are many servlets, the configuration of each servlet is distributed in its own class, which is not easy to find and modify.
Web.xml configuration file configuration Servlet
Advantages: Centralized management of Servlet configuration, easy to find and modify.
Disadvantages: The code is cumbersome, not readable, and not easy to understand.
边栏推荐
- Drools (7): WorkBench
- PyG builds R-GCN to realize node classification
- MySQL 安装报错的解决方法
- 2022.7.29-----leetcode.593
- 恐造成下一个“千年虫”的闰秒,遭科技巨头们联合抵制
- 弘玑再度入围Gartner 2022 RPA魔力象限并实现位置大幅跃升
- day10--install mysql on linux
- Hongji was once again shortlisted in the Gartner 2022 RPA Magic Quadrant and achieved a significant jump in position
- Azure 开发者新闻快讯丨开发者7月大事记一览
- [ 云原生之谜 ] 云原生背景 && 定义 && 相关技术详解?
猜你喜欢

Charles replaces the interface response information
![[ 云原生之谜 ] 云原生背景 && 定义 && 相关技术详解?](/img/eb/0cd6891fcc00d2c01ba8bd7f8d0822.png)
[ 云原生之谜 ] 云原生背景 && 定义 && 相关技术详解?

数组和结构体

How to Effectively Conduct Retrospective Meetings (Part 1)?

Shell script basic editing specifications and variables

MySQL 字符串拼接 - 多种字符串拼接实战案例

Eureka Registry

Mini Program Graduation Works WeChat Second-hand Trading Mini Program Graduation Design Finished Works (4) Opening Report

PyG builds R-GCN to realize node classification

Eureka注册中心
随机推荐
Redis【超详解!!!】
state space representation
Pytorch框架学习记录5——DataLoader的使用
How does the AI intelligent security video platform EasyCVR configure the simultaneous transmission of audio and video?
After 5 years of Ali internship interview~
Taobao/Tmall get the list of sold product orders API
MySQL data query (subtotal and sorting)
When the EasyNVR platform is cascaded to the EasyCVR, why can't the video be played after a while?
Boutique: Taobao/Tmall Get Order Details API for Purchased Products
Mysql版本升级,直接复制Data文件,查询特别慢
(6) "Digital Electricity" - Diodes and CMOS Gate Circuits (Introduction)
SQL introduction of the first lecture -- MySQL 8.0.29 installation tutorial (Windows 64 - bit)
cnpm安装步骤
【翻译】Envoy Fundamentals,这是一个培训课程,使人们能够更快地采用Envoy Proxy。...
机器学习:知道通过低方差过滤实现降维过程
Unity3D Application模拟进入前后台及暂停
golang中如何比较struct,slice,map是否相等以及几种对比方法的区别
[Driver] udev aliases each serial port of USB to 4 serial ports
Pytorch framework learning record 7 - convolutional layer
【C进阶】数组传参与函数指针