Summary: Requirements Description When doing a project, in order to save trouble, at first, the initialized configuration is placed in each class for static loading. Once the initial configuration is too much, I want to sort it out. Here, the init method in servlet is used to initialize it. web.xml description First understand the web.
Statement of needs
When working on a project, in order to save trouble, at first, the initialized configuration is placed in each class for static loading. Once the initial configuration is too much, I want to sort it out. Here, the init method in the servlet is used to initialize it.
web.xml description
First understand the loading order of elements in web.xml:
-
After starting the web project, the web container first goes back to the web.xml file and reads this file
-
The container creates a ServletContext (servlet context) that will be shared by all parts of the entire web project
-
The container will be converted into key-value pairs and handed over to the servletContext
-
Class instance in container creation, create listener
-
The container loads the filter and creates the filter. Note that the corresponding filter-mapping must be placed after the filter
-
The container loads the servlet, and the loading order is executed according to Load-on-startup
Full load order: ServletContext -> context-param -> listener-> filter -> servlet
Configuration implementation
InitServlet.java:
/**
* 初始化系统参数
* 创建者 科帮网
* 创建时间 2017年5月10日
*
*/
public class InitServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
@Override
public void init(){
try {
if(Constants.PAY_URL.size()==0){
List<CommonEntity> listPayUrl = PropertiesListUtil.listPayUrl();
for(CommonEntity entity:listPayUrl){
Constants.PAY_URL.put(entity.getEntityCode(), entity.getEntityName());
}
}
LogUtil.info("佛祖保佑 永不宕机 永无BUG :初始化系统数据数量:"+Constants.PAY_URL.size());
Configs.init("zfbinfo.properties");
LogUtil.info("初始化支付宝配置信息");
SDKConfig.getConfig().loadPropertiesFromSrc();
LogUtil.info("初始化银联支付配置信息");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 重新加载配置文件
* @Author 科帮网
* @param request
* @param response
* @throws ServletException
* @throws IOException
* @Date 2017年5月10日
* 更新日志
* 2017年5月10日 张志朋 首次创建
*
*/
@Override
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
Constants.PAY_URL = new ConcurrentHashMap<String, String>();
List<CommonEntity> listPayUrl = PropertiesListUtil.listPayUrl();
for(CommonEntity entity:listPayUrl){
Constants.PAY_URL.put(entity.getEntityCode(), entity.getEntityName());
}
LogUtil.info("初始化系统数据数量:"+Constants.PAY_URL.size());
}
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doGet(request, response);
}
}
web.xml: (partial configuration)
<!-- 初始基础化数据-->
<servlet>
<servlet-name>InitServlet</servlet-name>
<servlet-class>com.acts.web.common.servlet.InitServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>InitServlet</servlet-name>
<url-pattern>/InitServlet</url-pattern>
</servlet-mapping>
Introduction to servlets
what is a servlet
servlet is a technology provided by sun company to develop dynamic web. If a user wants to send a dynamic web resource (that is, develop a Java program to output data to the browser), he needs to complete the following two steps:
- Write a Java class that implements the servlet interface.
- Deploy the developed Java class to the web server.
According to a conventional naming habit, we usually call the java program that implements the servlet interface Servlet.
servlet running process
- The browser sends a request, which is obtained by the web container
-
Web服务器首先检查是否已经装载并创建了该Servlet的实例对象。如果是,则直接执行第④步,否则,执行第②步。
-
装载并创建该Servlet的一个实例对象,调用Servlet实例对象的init()方法。
-
创建一个用于封装HTTP请求消息的HttpServletRequest对象和一个代表HTTP响应消息的HttpServletResponse对象,然后调用Servlet的service()方法并将请求和响应对象作为参数传递进去。
-
WEB应用程序被停止或重新启动之前,Servlet引擎将卸载Servlet,并在卸载之前调用Servlet的destroy()方法
servlet初始化
-
load-on-startup >=0 时,表示在web应用启动后立即加载,其中load-on-startup的值越小,表示加载的优先级越高,如果两个servlet的load-on-startup值相同,则其加载优先级有容器决定;
-
load-on-startup 未配置时,则该servlet的加载由容器决定;
配置load-on-startup后,servlet在startup后立即加载,但只是调用servlet的init()方法,用以初始化该servlet相关的资源。初始化成功后,该servlet可响应web请求;如未配置load-on-startup,容器一般在第一次响应web请求时,会先检测该servlet是否初始化,如未初始化,则调用servlet的init()先初始化,初始化成功后,再响应请求。
PS:一般我们在开发web应用时,都会配置这个参数,有两个好处:
1. 如果初始化过程失败,则容器会提示启动失败,此时我们能够提前知道相关错误;
2. 配置该参数相当于将初始化servlet的工作转移到容器启动过程,使得容器只要启动成功后,就可立即响应web请求。
关于load-on-startup一些官网说明:
If the value is a negative integer, or the element is not present, the container is free to load the servlet
whenever it chooses. If the value is a positive
integer or 0, the container must load and initialize the servlet as the application is deployed.
注意
使用servlet时,一般都是继承httpServlet,然后分别实现doGet或者doPost方法,但是在这里面要注意的是,这servlet并不是线程安全的,多线程单实例执行的,当并发访问同一个资源的话(成员变量等等),就有可能引发线程安全问题。