当前位置:网站首页>Servlet面试题
Servlet面试题
2022-06-30 02:51:00 【lanleihhh】
servlet的生命周期
servlet初始化—init()方法
①第一次创建Servlet时调用,只会执行一次初始化
②默认是第一次处理请求时创建Servlet,也可以设置为服务器第一次启动时就创建Servletpublic void init() throws ServletException { // 初始化代码... }servlet处理请求:service()方法
①处理来自浏览器的请求,将处理后的结果响应给浏览器
②servlet接收到请求后,调用service方法来进行处理,service中针对不同的请求进行匹配,选择调用doPost或doGet等方法
③在编写servlet时,需要实现doPost/doGet等方法protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getMethod(); long lastModified; if (method.equals("GET")) { //处理get请求 } } else if (method.equals("HEAD")) { } else if (method.equals("POST")) { this.doPost(req, resp); } else if (method.equals("PUT")) { this.doPut(req, resp); } else if (method.equals("DELETE")) { this.doDelete(req, resp); } else if (method.equals("OPTIONS")) { this.doOptions(req, resp); } else if (method.equals("TRACE")) { this.doTrace(req, resp); } else { //其他 } }servlet长时间没有被调用或服务器关闭,会调用destroy()方法销毁servlet,由JVM垃圾回收器来回收servlet
destroy方法只会执行一次
执行后,该servlet被标记为垃圾,等待回收
servlet的线程安全问题
servlet不是线程安全的
servlet是单例的,初始化后,只有长时间不再接受请求或容器关闭时,这个servlet才会销毁,多个请求线程都访问的是这一个servlet
一个servlet在处理第一次请求前进行实例化,初始化。或在容器启动时初始化(需要设置)
后续调用这个servlet的请求,都是独立的线程来调用service方法,存在线程安全问题
如何解决?
不在servlet中使用成员变量,线程之间没有资源竞争,线程安全
使用ThreadLocal为每个线程提供一份独立的变量
给处理请求的方法加锁;造成阻塞
protected synchronized void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { }实现SingleThreadModel接口;会为每个用户创建一个servlet实例,性能差(已弃用)
public class DormServlet extends HttpServlet implements SingleThreadModel
get 与 post 区别
HTTP请求默认使用get
| get | post |
|---|---|
| 参数在url中 | 参数在 resquest body 请求体中 |
| 有长度限制 | 没有长度限制 |
| 暴露在url中,不安全 | 隐藏在请求体中,安全 |
| 只能url编码 | 支持多种编码 |
| 产生一个TCP数据包 | 产生两个TCP数据包 |
| 获取服务器中的数据 | 修改服务器上的数据 |
边栏推荐
- CMake教程系列-02-使用cmake代碼生成二進制
- Global and Chinese market of ERP software for garment and textile industries 2022-2028: Research Report on technology, participants, trends, market size and share
- How does native JS generate Jiugong lattice
- Linear algebra Chapter 3 summary of vector and vector space knowledge points (Jeff's self perception)
- High paid programmers & interview questions series 63: talk about the differences between sleep (), yield (), join (), and wait ()
- Five cheapest wildcard SSL certificate brands
- SSL证书七大常见错误及解决方法
- 福利抽奖 | 开源企业级监控Zabbix6.0都有哪些亮点
- 什么是X.509证书?X.509证书工作原理及应用?
- What files does a CA digital certificate contain? How to view SSL certificate information?
猜你喜欢

Jupyter notebook显示k线图集合

Créer des compétences exquises dans l'éducation des créateurs

Several key points recorded after reviewing redis design and Implementation

Ffmpeg source code

如何在 JupyterLab 中把 ipykernel 切换到不同的 conda 虚拟环境?

三层交换机和二层交换机区别是什么
![[dry goods sharing] the latest WHQL logo certification application process](/img/c3/37277572c70b0af944e594f0965a6c.png)
[dry goods sharing] the latest WHQL logo certification application process

2. 成功解决 BUG:Exception when publishing, ...[Failed to connect and initialize SSH connection...

重看《Redis设计与实现》后记录几个要点

How to prevent duplicate submission under concurrent requests
随机推荐
How to modify and add fields when MySQL table data is large
What are the requirements for NPDP product manager international certification examination?
怎么利用Redis实现点赞功能
Raki's notes on reading paper: neighborhood matching network for entity alignment
Sitelock nine FAQs
Raki's notes on reading paper: Leveraging type descriptions for zero shot named entity recognition and classification
隐藏在科技教育中的steam元素
The rigorous judgment of ID number is accurate to the last place in the team
重看《Redis设计与实现》后记录几个要点
LeetCode 3. 无重复字符的最长子串
What is an X.509 certificate? 10. 509 certificate working principle and application?
Idea remote debugging remote JVM debug
Summary of knowledge points about eigenvalues and eigenvectors of matrices in Chapter 5 of Linear Algebra (Jeff's self perception)
FDA ESG规定:必须使用数字证书保证通信安全
JMeter obtains cookies across thread groups or JMeter thread groups share cookies
Global and Chinese market of wind energy equipment logistics 2022-2028: Research Report on technology, participants, trends, market size and share
外汇交易平台哪个好?有监管的资金就安全吗?
[论]【DSTG】Dynamic SpatiotemporalGraph Convolutional Neural Networks for Traffic Data Imputation
CMake教程系列-04-编译相关函数
重磅来袭--UE5的开源数字孪生解决方案