当前位置:网站首页>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 |
边栏推荐
- Note the use of export/import and class inheritance in ES6
- Comparable和Comparator的区别
- Unity3D UGUI强制刷新Layout(布局)组件
- Time complexity analysis
- 怎么使用Vant实现数据分页和下拉加载
- Global and Chinese market of mobile commerce solutions 2022-2028: Research Report on technology, participants, trends, market size and share
- 2022护网行动在即,关于护网的那些事儿
- Cmake tutorial series-01-minimum configuration example
- Functions in C language
- Summary of PHP test sites encountered in CTF questions (I)
猜你喜欢

【实战技能】如何撰写敏捷开发文档

Pytoch learning (II)

Three solutions to forced hibernation of corporate computers

Intel hex, Motorola S-Record format detailed analysis

Unity3D UGUI强制刷新Layout(布局)组件
![[Postgres] Postgres database migration](/img/45/7074aa766640160a3b6f00b109cb2f.png)
[Postgres] Postgres database migration

Federal learning: dividing non IID samples by Dirichlet distribution

CMake教程系列-02-使用cmake代码生成二进制

【直播笔记0629】 并发编程二:锁

可视化HTA窗体设计器-HtaMaker 界面介绍及使用方法,下载 | HTA VBS可视化脚本编写
随机推荐
Use of Arthas
High paid programmers & interview questions series 63: talk about the differences between sleep (), yield (), join (), and wait ()
RAII内存管理
Global and Chinese market of wind energy equipment logistics 2022-2028: Research Report on technology, participants, trends, market size and share
Raii memory management
CMake教程系列-02-使用cmake代碼生成二進制
Unity TimeLine 数据绑定
可视化HTA窗体设计器-HtaMaker 界面介绍及使用方法,下载 | HTA VBS可视化脚本编写
Raki's notes on reading paper: neighborhood matching network for entity alignment
What is the metauniverse: where are we, where are we going
Azure 开发者新闻快讯丨开发者6月大事记一览
(graph theory) connected component (template) + strongly connected component (template)
HTA入门基础教程 | VBS脚本的GUI界面 HTA简明教程 ,附带完整历程及界面美化
Global and Chinese markets for light cargo conveyors 2022-2028: Research Report on technology, participants, trends, market size and share
迅为恩智浦iTOP-IMX6开发平台
迅為恩智浦iTOP-IMX6開發平臺
Which is a good foreign exchange trading platform? Is it safe to have regulated funds?
2. < tag dynamic programming and 0-1 knapsack problem > lt.416 Split equal sum subset + lt.1049 Weight of the last stone II
Use compose to realize the effect of selecting movie seats by panning tickets
原生JS怎么生成九宫格