当前位置:网站首页>注解简化配置与启动时加载
注解简化配置与启动时加载
2022-07-03 08:35:00 【緈福的街口】
一、注解简化配置
注解用于简化Web应用程序的配置过程
在遇到多个Web应用程序,如果使用web.xml工作量较大,因此我们可以使用注解
Servlet核心注解:@WebServlet
java页面
package com.imooc.servlet;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@WebServlet("/anno")
public class AnnotationServlet extends HttpServlet{
@Override
protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().println("I'm annotation servlet!");
}
}
其中使用@WebServlet("/anno")与在web.xml中使用以下代码具有相同的效果
<servlet>
<servlet-name>anno</servlet-name>
<servlet-class>com.imooc.servlet.AnnotationServlet</servlet-class>
</servlet>
<!-- 将Servlet与URL绑定 -->
<servlet-mapping>
<servlet-name>anno</servlet-name>
<url-pattern>/anno</url-pattern>
</servlet-mapping>

二、启动时加载
- web.xml使用<load-on-startup>设置启动加载
- 启动时加载在工作中常用于系统的预处理
1.java页面
此项目进行数据库的创建,导入和分析
ImportServlet.java
package com.ssyt.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class CreateServlet extends HttpServlet{
@Override
public void init() throws ServletException {
System.out.println("正在创建数据库");
}
}
CreateServlet.java
package com.ssyt.servlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
public class ImportServlet extends HttpServlet{
@Override
public void init() throws ServletException {
System.out.println("正在导入建数据");
}
}
AnalysisServlet.java
package com.ssyt.servlet;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
@WebServlet(urlPatterns="/unused",loadOnStartup=2)
public class AnalysisServlet extends HttpServlet{
@Override
public void init() throws ServletException {
System.out.println("正在分析结果");
}
}
2.web.xml配置文件
<servlet>
<servlet-name>create</servlet-name>
<servlet-class>com.ssyt.servlet.CreateServlet</servlet-class>
<load-on-startup>0</load-on-startup>
</servlet>
<servlet>
<servlet-name>import</servlet-name>
<servlet-class>com.ssyt.servlet.ImportServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
- 其中在web.xml中:0~9999表示启动时加载的先后顺序
<load-on-startup>1</load-on-startup>- 在java页面使用注解简化配置:其中url必须进行编写
@WebServlet(urlPatterns="/unused",loadOnStartup=2)
3.启动时加载
未进行请求,启动时加载,按照顺序进行启动
边栏推荐
- Mall management system of database application technology course design
- GIS实战应用案例100篇(七十八)-多规合一数据库设计及数据入库
- Animation_ IK overview
- Development material set
- 使用base64编码传图片
- Swagger document configuration
- Huawei interview summary during the epidemic
- Osgearth topographic shading map drawing
- Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network
- 图像处理8-CNN图像分类
猜你喜欢

matlab神经网络所有传递函数(激活函数)公式详解

详解sizeof、strlen、指针和数组等组合题

【更新中】微信小程序学习笔记_3

【Rust笔记】02-所有权

Mall management system of database application technology course design

Collection interface

了解小程序的笔记 2022/7/3

Detailed explanation of all transfer function (activation function) formulas of MATLAB neural network

Unity4.3.1 engine source code compilation process

图像处理8-CNN图像分类
随机推荐
Un système de gestion de centre commercial pour la conception de cours de technologie d'application de base de données
matlab神经网络所有传递函数(激活函数)公式详解
Why can void * be a general pointer
Osgearth target selection
Talking about: is the HashSet set ordered or disordered /hashset set unique, why can we store elements with the same content
UE4 plug in development
Base64 and base64url
How to deal with the core task delay caused by insufficient data warehouse resources
Find the intersection of line segments
UE4 source code reading_ Bone model and animation system_ Animation compression
Conversion between string and int types in golang
Image processing 8-cnn image classification
Notes on understanding applets 2022/7/3
Huawei interview summary during the epidemic
Cloudcompare learning (1) - cloudcompare compilation and common plug-in implementation
Markdown learning
[rust notes] 05 error handling
Golang 时间格式整理
十六进制编码简介
Development experience and experience