当前位置:网站首页>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 |
边栏推荐
- Série de tutoriels cmake - 02 - génération de binaires à l'aide du Code cmake
- Lua Basics
- Azure 开发者新闻快讯丨开发者6月大事记一览
- Some configuration details about servlet initial development
- Cmake tutorial series-03-dependency management
- High paid programmers & interview questions series 63: talk about the differences between sleep (), yield (), join (), and wait ()
- 2022 underground coal mine electrical test and underground coal mine electrical simulation test
- GTK interface programming (I): Environment Construction
- Raki's notes on reading paper: discontinuous named entity recognition as maximum clique discovery
-  与空格的区别
猜你喜欢

LeetCode 3. Longest substring without duplicate characters

在php中字符串的概念是什么

Heavy attack -- ue5's open source digital twin solution

福利抽奖 | 开源企业级监控Zabbix6.0都有哪些亮点

Mysql提取表字段中的字符串

约瑟夫环 数学解法

可视化HTA窗体设计器-HtaMaker 界面介绍及使用方法,下载 | HTA VBS可视化脚本编写

三层交换机和二层交换机区别是什么

Three solutions to forced hibernation of corporate computers

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.
随机推荐
What kind of foreign exchange trading platform is regulated and safe?
Hands on in-depth learning notes (XV) 4.1 Multilayer perceptron
What is the concept of string in PHP
HTA introductory basic tutorial | GUI interface of vbs script HTA concise tutorial, with complete course and interface beautification
How to switch ipykernel to a different CONDA virtual environment in jupyterlab?
迅為恩智浦iTOP-IMX6開發平臺
O & M (21) make winpe startup USB flash disk
GTK interface programming (II): key components
2. 成功解决 BUG:Exception when publishing, ...[Failed to connect and initialize SSH connection...
Xunwei NXP itop-imx6 development platform
PHP two-dimensional array randomly fetches a random or fixed number of one-dimensional arrays
Global and Chinese market for defense network security 2022-2028: Research Report on technology, participants, trends, market size and share
Unity TimeLine 数据绑定
Global and Chinese market of wind energy equipment logistics 2022-2028: Research Report on technology, participants, trends, market size and share
Mysql表数据比较大情况下怎么修改添加字段
How to prevent duplicate submission under concurrent requests
备忘一下es6的export/import和类继承的用法
Mysql提取表字段中的字符串
NLP text summary: data set introduction and preprocessing [New York Times annotated corpus]
SQLite use