当前位置:网站首页>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
边栏推荐
- The El table horizontal scroll bar is fixed at the bottom of the visual window
- Mysql8 installation
- AUTOSAR from introduction to mastery 100 lectures (87) - key weapon of advanced EEA - AUTOSAR and DDS
- Record a website for querying compatibility, string Replaceall() compatibility error
- HQL statement tuning
- MySQL supports IP access
- Learning notes of AMBA protocol
- Important knowledge of transport layer (interview, retest, final)
- Proteus 8.10 installation problem (personal test is stable and does not flash back!)
- The use of dcast and melt in R language is simple and easy to understand
猜你喜欢
内核错误: No rule to make target ‘debian/canonical-certs.pem‘, needed by ‘certs/x509_certificate_list‘
The use of dcast and melt in R language is simple and easy to understand
Record the problems encountered in using v-print
Design of STM32 multi-channel temperature measurement wireless transmission alarm system (industrial timing temperature measurement / engine room temperature timing detection, etc.)
Recovering data with MySQL binlog
Record some NPM related problems (messy records)
Customize classloader and implement hot deployment - use loadclass
R语言中dcast 和 melt的使用 简单易懂
51 single chip microcomputer stepping motor control system based on LabVIEW upper computer (upper computer code + lower computer source code + ad schematic + 51 complete development environment)
three.js文字模糊问题
随机推荐
AUTOSAR from introduction to mastery 100 lectures (81) - FIM of AUTOSAR Foundation
About the 'enum' enumeration type and structure.
Remote access to raspberry pie at home (Part 1)
Important knowledge of network layer (interview, reexamination, term end)
世界读书日:我想推荐这几本书
AUTOSAR from introduction to mastery 100 lectures (86) - 2F of UDS service foundation
Metalama简介4.使用Fabric操作项目或命名空间
Softbank vision fund entered the Web3 security industry and led a new round of investment of US $60 million in certik
将opencv 图片转换为字节的方式
mui picker和下拉刷新冲突问题
Use of Presto date function
V-model binding value in El select, data echo only displays value, not label
Temperature and humidity monitoring + timing alarm system based on 51 single chip microcomputer (C51 source code)
The use of dcast and melt in R language is simple and easy to understand
mysql 基本语句查询
Custom nail robot alarm
Timing role in the project
Wonderful review | the sixth issue of "source" - open source economy and industrial investment
Brief introduction of asynchronous encapsulation interface request based on uniapp
5 free audio material websites, recommended collection