当前位置:网站首页>Servlet interview questions
Servlet interview questions
2022-06-30 02:59:00 【lanleihhh】
Servlet Interview questions
servlet Life cycle of
servlet initialization —init() Method
① First creation Servlet Called when the , Initialization is performed only once
② The default is to create... The first time a request is processed Servlet, It can also be set to create... The first time the server starts Servletpublic void init() throws ServletException { // Initialization code ... }servlet Processing requests :service() Method
① Processing requests from browsers , Respond the processed results to the browser
②servlet After receiving the request , call service Methods to deal with ,service For different requests , Select call doPost or doGet Other methods
③ Writing servlet when , Need to achieve doPost/doGet Other methodsprotected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { String method = req.getMethod(); long lastModified; if (method.equals("GET")) { // Handle get request } } 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 { // other } }servlet It has not been called for a long time or the server has shut down , Would call destroy() Method The destruction servlet, from JVM Garbage collector to recycle servlet
destroy Method only executes once
After execution , The servlet Marked as garbage , Waiting for recycling
servlet Thread safety of
servlet Not thread safe
servlet Is a singleton , After the initialization , Only when the request is not accepted for a long time or the container is closed , This servlet Will destroy , This one is accessed by multiple request threads servlet
One servlet Instantiate before processing the first request , initialization . Or initialize when the container starts ( Need to set up )
This is called later servlet Request , Are called by independent threads service Method , There are thread safety issues
How to solve ?
be not in servlet Using member variables in , There is no resource competition between threads , Thread safety
Use ThreadLocal Provide an independent variable for each thread
Lock the method that handles the request ; Cause obstruction
protected synchronized void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { }Realization SingleThreadModel Interface ; One will be created for each user servlet example , Poor performance ( Have been abandoned )
public class DormServlet extends HttpServlet implements SingleThreadModel
get And post difference
HTTP Request default use get
| get | post |
|---|---|
| Parameter in url in | Parameter in resquest body Request body |
| Limited length | There is no length limit |
| Exposure to the url in , unsafe | Hidden in the request body , Security |
| Can only url code | Support for multiple encodings |
| Produce a TCP Data packets | Produce two TCP Data packets |
| Get data from the server | Modify the data on the server |
边栏推荐
- 如何在 JupyterLab 中把 ipykernel 切换到不同的 conda 虚拟环境?
- What are the three paradigms of database
- 原生JS怎么生成九宫格
- Cmake tutorial series -02- generating binaries using cmake code
-  与空格的区别
- 【微信小程序】条件渲染 列表渲染 原来这样用?
- 迅为恩智浦iTOP-IMX6开发平台
- Xunwei enzhipu ITop - imx6 Development Platform
- LeetCode 3. 无重复字符的最长子串
- How to use redis to realize the like function
猜你喜欢

Some configuration details about servlet initial development

Intel-Hex , Motorola S-Record 格式详细解析

prompt learning 一个空格引发的血案

Time complexity analysis

Pytoch learning (II)

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

2.8 【 weight of complete binary tree 】

约瑟夫环 数学解法

Federal learning: dividing non IID samples by Dirichlet distribution

在php中字符串的概念是什么
随机推荐
Cmake tutorial series-01-minimum configuration example
[Postgres] Postgres database migration
Comparable和Comparator的区别
2.8 【 weight of complete binary tree 】
身份证号的严谨判断精确到队后一位
c#控制台格式化代码
The rigorous judgment of ID number is accurate to the last place in the team
Uniapp address translation latitude and longitude
Servlet面试题
SQLite使用
模板参数包和函数参数包
Use compose to realize the effect of selecting movie seats by panning tickets
High paid programmers & interview questions series 63: talk about the differences between sleep (), yield (), join (), and wait ()
Mysql提取表字段中的字符串
mysqldump原理
怎样的外汇交易平台是有监管的,是安全的?
Call collections Sort() method, compare two person objects (by age ratio first, and by name ratio for the same age), and pass lambda expression as a parameter.
Wechat applet page Jump and parameter transfer
Raki's notes on reading paper: named entity recognition as dependency parsing
原生JS怎么生成九宫格