Methods to be implemented by Java web servlet interface from scratch (3)

Previously, the mapping was done through the xml file configuration method. Now we will introduce the second mapping method
through the annotation method.
Insert picture description here

The restart
Insert picture description here
update resources in the idea ---- update static resources, such as html, js, css and other running modes and debugging modes are effective immediately;

update classes and resources ---- update java, jsp and static resources (1. After java is modified, it will be compiled into .class, and then overwritten to the target/kao folder. In IDE debugging mode, it will take effect immediately. IDE In running mode, it does not take effect immediately and needs to be redeployed to take effect. After the jsp is modified, when it is accessed again, it will be automatically updated, recompiled into java.class and saved in the work directory of tomcat. Because it is accessed, the modification is checked , Do you need to recompile, so in IDE running mode and IDEA debugging mode, it will take effect immediately. Just refresh the page);

redeployed ----- redeploy, publish to tomcat, do not restart tomcat, but delete the original, and then republish;

restart server ----- Restart tomcat.

Servlet 的⽣命周期
1、当浏览器访问 Servlet 的时候,Tomcat 会查询当前 Servlet 的实例化对象是否存在,如果不存在,
则通过反射机制动态创建对象,如果存在,直接执⾏第 3 步。
2、调⽤ init ⽅法完成初始化操作。
3、调⽤ service ⽅法完成业务逻辑操作。
4、关闭 Tomcat 时,会调⽤ destory ⽅法,释放当前对象所占⽤的资源。
Servlet 的⽣命周期⽅法:⽆参构造函数、init、service、destory
1、⽆参构造函数只调⽤⼀次,创建对象。
2、init 只调⽤⼀次,初始化对象。
3、service 调⽤ N 次,执⾏业务⽅法。
4、destory 只调⽤⼀次,卸载对象。

ServletConfig
该接⼝是⽤来描述 Servlet 的基本信息的。
getServletName() 返回 Servlet 的名称,全类名(带着包名的类名)
getInitParameter(String key) 获取 init 参数的值(web.xml)
getInitParameterNames() 返回所有的 initParamter 的 name 值,⼀般⽤作遍历初始化参数
getServletContext() 返回 ServletContext 对象,它是 Servlet 的上下⽂,整个 Servlet 的管理者。
ServletConfig 和 ServletContext 的区别:
ServletConfig 作⽤于某个 Servlet 实例,每个 Servlet 都有对应的 ServletConfig,ServletContext 作⽤
于整个 Web 应⽤,⼀个 Web 应⽤对应⼀个 ServletContext,多个 Servlet 实例对应⼀个
ServletContext。
⼀个是局部对象,⼀个是全局对象。

Guess you like

Origin blog.csdn.net/qq_43928549/article/details/113104990