当前位置:网站首页>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 |
边栏推荐
- Distributed file system fastdfs
- SQLite use
- [untitled]
- The rigorous judgment of ID number is accurate to the last place in the team
- threejs 镜子案例Reflector 创建镜子+房子搭建+小球移动
- 迅為恩智浦iTOP-IMX6開發平臺
- Série de tutoriels cmake - 02 - génération de binaires à l'aide du Code cmake
- Tp6 framework integrates JWT for token authentication
- 【直播笔记0629】 并发编程二:锁
- Detailed explanation of minimum stack
猜你喜欢
约瑟夫环 数学解法
Visual HTA form designer htamaker interface introduction and usage, Download | HTA VBS visual script writing
Raki's notes on reading paper: discontinuous named entity recognition as maximum clique discovery
HTA入门基础教程 | VBS脚本的GUI界面 HTA简明教程 ,附带完整历程及界面美化
Three solutions to forced hibernation of corporate computers
IDEA 远程调试 Remote JVM Debug
How to use vant to realize data paging and drop-down loading
Raki's notes on reading paper: Leveraging type descriptions for zero shot named entity recognition and classification
在php中字符串的概念是什么
【实战技能】如何撰写敏捷开发文档
随机推荐
Global and Chinese market of mobile commerce solutions 2022-2028: Research Report on technology, participants, trends, market size and share
模板参数包和函数参数包
How do I enable assembly binding logging- How can I enable Assembly binding logging?
CMake教程系列-02-使用cmake代码生成二进制
NLP text summary: data set introduction and preprocessing [New York Times annotated corpus]
Jvxetable sub table record loading completion event
The Oracle main program is deleted, but the data is on another hard disk. Can I import the data again?
Unity3D UGUI强制刷新Layout(布局)组件
快速排序、聚簇索引、寻找数据中第k大的值
IDEA 远程调试 Remote JVM Debug
Xunwei NXP itop-imx6 development platform
Quick sort, cluster index, find the k-largest value in the data
threejs 镜子案例Reflector 创建镜子+房子搭建+小球移动
The rigorous judgment of ID number is accurate to the last place in the team
Global and Chinese market for defense network security 2022-2028: Research Report on technology, participants, trends, market size and share
Which is a good foreign exchange trading platform? Is it safe to have regulated funds?
GTK interface programming (I): Environment Construction
Study diary: February 15, 2022
Global and Chinese market of relay lens 2022-2028: Research Report on technology, participants, trends, market size and share
Some technology sharing