当前位置:网站首页>@ 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.
边栏推荐
- Mysql version upgrade, copy the Data file directly, the query is very slow
- QT(39)-vs开发qt程序提示无法打开源文件
- Anti-shake and throttling
- ospf 综合实验(重发布,特殊区域)
- Data Lake: Data Integration Tool DataX
- 在麒麟V10操作系统上安装MySQL数据库
- SSM框架简单介绍
- 我的Go+语言初体验——祝福留言小系统,让她也可以感受到你的祝福
- sqlmap use tutorial Daquan command Daquan (graphics)
- Pytorch框架学习记录1——Dataset类代码实战
猜你喜欢
day10--install mysql on linux
Roperties类配置文件&DOS查看主机网络情况
Wechat second-hand transaction small program graduation design finished product (1) Development overview
Pytorch framework learning record 7 - convolutional layer
ospf 综合实验(重发布,特殊区域)
Operational configuration: How to run multiple EasyCVR programs as a service in one server?
海外多家权威媒体热议波场TRON:为互联网去中心化奠定基础
逆向分析实战2
恐造成下一个“千年虫”的闰秒,遭科技巨头们联合抵制
使用EFR32作为Zigbee/Thread的sniffer的用法
随机推荐
【C语言】程序环境和预处理
How to compare struct, slice, map for equality and the difference between several comparison methods in golang
day10--install mysql on linux
(redistribute, special comprehensive experiment ospf area)
Boss Rush (二分答案 + 状压DP)
phpoffice edit excel document
How does the Snapdragon 7 series chip perform?Reno8 Pro proves a new generation of God U
Wechat second-hand transaction small program graduation design finished product (1) Development overview
Based on all volunteers - H and D1 XR806 rare plant monitoring device
[ 云原生之谜 ] 云原生背景 && 定义 && 相关技术详解?
swagger使用教程——快速使用swagger
Pytorch framework learning record 1 - Dataset class code combat
The first day of Flink learning - what is batch and streaming computing?
error: The following untracked working tree files would be overwritten by
Pytorch框架学习记录5——DataLoader的使用
sql语句-如何以一个表中的数据为条件据查询另一个表中的数据
05全局配置文件application.properties详解
权值线段树+线段树分裂/合并+CF1659D
数据库概论 - MySQL的简单介绍
MYSQL 唯一约束