当前位置:网站首页>web三大组件之Servlet
web三大组件之Servlet
2022-04-23 13:06:00 【ArchieSean】
博主名取自《小羊肖恩》中的小羊肖恩,名字为:肖恩,音译为Sean,自己取的姓:阿奇,为符合我们的阅读习惯,连起来组成为ArchieSean。博主目前大三在读,志在将博客打造成为个人线上笔记的技术栈,方便自己也方便他人。如博客中有任何错误,请各位指出,谢谢大家。
servlet概述
简单来说,它是服务器端的小程序。
servlet就是一个接口,它定义了java类被浏览器访问到的规则。
servlet结构体系
servlet方法
如图 实现servlet接口,重写其方法。
import javax.servlet.*;
import java.io.IOException;
public class Csdn implements Servlet {
@Override
//初始化方法,在servlet被创建时执行,并且只执行一次
public void init(ServletConfig servletConfig) throws ServletException {
}
@Override
//获取ServletConfig对象
public ServletConfig getServletConfig() {
return null;
}
@Override
//提供服务的方法,每次访问时执行
public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException {
}
@Override
//返回servlet的信息,例如作者,版本等
public String getServletInfo() {
return null;
}
@Override
//销毁方法,在服务器关闭时调用
public void destroy() {
}
}
servlet生命周期
- 出生: init方法执行,且只执行一次,servlet被创建。
默认情况下,第一次访问时,servlet被创建,也可以是服务器启动时创建。 - 活着:执行 service方法,可执行多次。
- 死亡: destroy方法执行
服务器关闭时,执行destriy方法,servlet被销毁。
servlet的xml配置
位于WEB-INF下的web.xml
<web-app>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.xxx.servlet.HelloWorld</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/HelloWorld</url-pattern>
</servlet-mapping>
</web-app>
servlet原理
如上图所示:
- 当服务器收到服务器请求时,会解析url路径,获取访问servlet的资源路径。
- 服务器会去查找web.xml文件,找到和访问路径一致的url-pattern标签内容
- 再去找对应的servlet-class标签,拿到servlet的全类名
- 将该类的class文件加载进内存(使用反射:Class.forName()来加载)
- 创建其对象(反射:Class.newInstance()创建对象)
- 调用service方法
Servlet的注解配置
@webServlet(value="访问路径")
将该注解添加在servlet类之上即可,此时就不需要xml的配置了。
request和response再下一篇中总结。博文中若有任何问题,请评论区评论留言,谢谢。
版权声明
本文为[ArchieSean]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_41964942/article/details/106028863
边栏推荐
- CVPR 2022&NTIRE 2022|首个用于高光谱图像重建的 Transformer
- Van uploader upload picture implementation process, using native input to upload pictures
- Wonderful review | the sixth issue of "source" - open source economy and industrial investment
- filter()遍历Array异常友好
- AUTOSAR from introduction to mastery 100 lectures (50) - AUTOSAR memory management series - ECU abstraction layer and MCAL layer
- [Technical Specification]: how to write technical documents?
- 100 GIS practical application cases (53) - making three-dimensional image map as the base map of urban spatial pattern analysis
- async void 導致程序崩潰
- mysql支持ip访问
- Go language mapping operation
猜你喜欢
数据库中的日期时间类型
Record a website for querying compatibility, string Replaceall() compatibility error
Nodejs + websocket cycle small case
Record Alibaba cloud server mining program processing
The use of dcast and melt in R language is simple and easy to understand
100 lectures on practical application cases of Excel (VIII) - report connection function of Excel
Introduction to servlet listener & filter
Embrace the new blue ocean of machine vision and hope to open a new "Ji" encounter for the development of digital economy
1130 - host XXX is not allowed to connect to this MySQL server error in Navicat remote connection database
100 GIS practical application cases (53) - making three-dimensional image map as the base map of urban spatial pattern analysis
随机推荐
Mui + hbuilder + h5api simulate pop-up payment style
R语言中dcast 和 melt的使用 简单易懂
Install nngraph
Brief introduction of asynchronous encapsulation interface request based on uniapp
Design of STM32 multi-channel temperature measurement wireless transmission alarm system (industrial timing temperature measurement / engine room temperature timing detection, etc.)
教你快速开发一个 狼人杀微信小程序(附源码)
Remote access to raspberry pie at home (Part 1)
Go iris framework implements multi service Demo: start (listen to port 8084) service 2 through the interface in service 1 (listen to port 8083)
Teach you to quickly develop a werewolf killing wechat applet (with source code)
The use of dcast and melt in R language is simple and easy to understand
Go language: passing slices between functions
HQL statement tuning
Golang realizes regular matching: the password contains at least one digit, letter and special character, and the length is 8-16
Timing role in the project
AUTOSAR from introduction to mastery 100 lectures (52) - diagnosis and communication management function unit
Importerror after tensorflow installation: DLL load failed: the specified module cannot be found, and the domestic installation is slow
100 GIS practical application cases (51) - a method for calculating the hourly spatial average of NC files according to the specified range in ArcGIS
CVPR 2022&NTIRE 2022|首个用于高光谱图像重建的 Transformer
Translation of attention in natural language processing
AUTOSAR from introduction to mastery 100 lectures (86) - 2F of UDS service foundation