当前位置:网站首页>On Web server
On Web server
2022-07-02 06:03:00 【ysds20211402】
from : Micro reading https://www.weidianyuedu.com
Web Overview of server working principle
A lot of times we want to know ,web Container or web The server ( such as Tomcat perhaps jboss) How it works ? How they deal with people from all over the world http Requested ? What do they do behind the scenes ?Java Servlet API( for example ServletContext,ServletRequest,ServletResponse and Session These classes ) What role does it play in it ? These are all web Application developers may want to be web Important issues or concepts that application developers must know . In this article , I will try to give answers to some of the above questions . Please concentrate !
Chapter of the article :
What is? web The server 、 Application servers and web Containers ?
What is? Servlet? What's the use of them ?
What is? ServletContext? Who created it ?
ServletRequest and ServletResponse Where to enter the life cycle ?
How to manage Session? know cookie Do you ?
How to ensure thread safety ?
What is? web The server , Application servers and web Containers ?
Let me discuss it first web Servers and application servers . Let me talk about... In one sentence :
“ In the past, they were different , But the two different categories slowly merged , Nowadays, in most cases and in use, they can be regarded as a whole .”
stay Mosaic browser ( It is usually considered the first graphical web browser ) And the early days of hyperlink content , Evolved into “web The server ” New concept of , It passes through HTTP Protocol to provide static page content and image services . at that moment , Most of the content is static , also HTTP 1.0 It's just a way to transfer files . But soon after web The server provides CGI function . This means that we can work for everyone web Request to start a process to generate dynamic content . Now? ,HTTP The agreement is mature and web Servers become more complex , Have something like a cache 、 Safety and session Manage these additional functions . As technology matures , We from Kiva and NetDynamics Learned the company's exclusive based on Java Server side technology . These technologies are eventually integrated into what we still use in most application development today JSP in .
So that's about web Server's . Now let's talk about application servers .
At the same time , Application server has existed and developed for a long time . Some companies are Unix Developed Tuxedo( Transaction oriented middleware )、TopEnd、Encina Products such as , These products are from similar IMS and CICS The host application management and monitoring environment derived from . Most of these products are designated “ Closed ” Product specific communication protocol to interconnect fat clients (“fat” client) And the server . stay 90 years , These traditional application server products began to be embedded HTTP Communications functions , At the beginning, we will use the gateway to realize . Soon the line between them began to blur .
meanwhile ,web Servers are becoming more mature , Can handle higher loads 、 More concurrency and better features ; Application servers are starting to add more and more base on HTTP Communication function . All this led to web The boundary between server and application server becomes narrower .
at present ,“ application server ” and “web The server ” The line between them has become blurred . But people also distinguish the two terms , Use... As an emphasis .
When someone says “web The server ” when , You usually think of it as HTTP At the core 、web UI For the application of the wizard . When someone says “ application server ” when , You may think of “ High load 、 Enterprise features 、 Transactions and queues 、 Multichannel communication (HTTP And more agreements )”. But now it's basically the same product that provides these needs .
That's about web Server and application server . Now let's look at the third term , namely web Containers .
stay Java aspect ,web Containers generally refer to Servlet Containers .Servlet The container is associated with Java Servlet The interaction of web Components of the container .web The container is responsible for the management of Servlet Life cycle of 、 hold URL Map to a specific Servlet、 Make sure URL Request to have the right access rights and more similar services . Taken together ,Servlet Containers are used to run your Servlet And the operating environment that maintains its life cycle .
What is? Servlet? What's the use of them ?
stay Java in ,Servlet Enables you to write server-side components that dynamically generate content according to requests . in fact ,Servlet It's a javax.servlet Interfaces defined in the package . It's for Servlet Three basic methods are stated in the life cycle of ——init()、service() and destroy(). Every Servlet All of these methods should be realized ( stay SDK Defined in or user-defined ) And these methods are called by the server at a specific time in their life cycle .
The class loader loads through laziness (lazy-loading) Or preload (eager loading) Automatically put Servlet Class is loaded into the container . Each request has its own thread , And one Servlet Object can serve multiple threads at the same time . When Servlet When the object is no longer in use , It will be JVM Recycle as garbage .
Lazy loading Servlet
Preloaded Servlet
What is? ServletContext? Who created it ?
When Servlet When container starts , It will deploy and load all web application . When web When the application is loaded ,Servlet The container will create... For each application at once Servlet Context (ServletContext) And save it in memory .Servlet The container will handle web Applied web.xml file , And create it at one time web.xml The definition of Servlet、Filter and Listener, They will also be stored in memory . When Servlet When container is closed , It will uninstall all web The application and ServletContext, be-all Servlet、Filter and Listner All instances will be destroyed .
from Java It can be seen from the documents ,ServletContext Defines a set of methods ,Servlet Use these methods to communicate with its Servlet The container communicates . for example , Used to get files MIME type 、 Forward requests or write log files . stay web Application deployment file (deployment descriptor) mark “ Distributed ” Under the circumstances ,web Each virtual machine of the application has a context instance . under these circumstances , Can't take Servlet Context is treated as a variable that shares global information ( Because its information is no longer global ). External resources can be used instead of , Like databases .
ServletRequest and ServletResponse Where to enter the life cycle ?
Servlet The container is contained in web Server ,web The server listens for messages from a specific port HTTP request , This port is usually 80. When the client ( Use web Browser users ) Send a HTTP When asked ,Servlet The container will create a new HttpServletRequest and HttpServletResponse object , And pass them to the created Filter and URL Mode and request URL Matching Servlet Example method , All of these use the same thread .
request Object provides access HTTP Access to all requested information , Such as request header and request entity .response Object provides control and sending HTTP A convenient way to respond , For example, set the response header and response entity ( Usually JSP Generated HTML Content ). When HTTP After the response is submitted and completed ,request and response Objects will be destroyed .
How to manage Session? know cookie Do you ?
When the client first accesses web Application or first use request.getSession() obtain HttpSession when ,Servlet The container creates Session, Generate a long Unique type ID( You can use session.getId() Get it ) And save it in the memory of the server .Servlet The container will also be in HTTP Set a in the response Cookie,cookie What's your name JSESSIONID also cookie The value of is session The only ID.
according to HTTP cookie standard ( Normal web The browser and web Conventions that the server must abide by ), stay cookie The period of validity of , client (web browser ) For subsequent requests, this cookie Back to the server .Servlet The container will be used with the name JSESSIONID Of cookie Detect every arrival HTTP Request header , And use cookie Get the relevant information from the server content HttpSession.
HttpSession Will live forever , Unless you haven't used it for more than a period of time . You can web.xml Set this time period in , The default time period is 30 minute . therefore , If the client has exceeded 30 Minutes no visit web Applications ,Servlet The container will be destroyed Session. Every request after , Even with a specific cookie, Will never access the same Session 了 .servletcontainer It will create a new one Session.
The existing Session
new Session
in addition , On the client side session cookie Have a default lifetime , This time is the same as the running time of the browser . therefore , When the user closes the browser ( All tabs or windows ), Client's Session Will be destroyed . After reopening the browser , And previous Session The associated cookie It will never be sent again . Again using request.getSession() Will return a new HttpSession And use a new session ID To set up cookie.
How to ensure thread safety ?
You should know by now that all requests are shared Servlet and Filter. This is a Java A great feature of , It's multithreaded and different threads ( namely HTTP request ) You can use the same instance . otherwise , Re creating an entity for each request consumes a lot of resources .
You also need to know , You should not use Servlet perhaps Filter Instance variables to store any request or session wide data . These data will be used by other Session All requests are shared . This is non thread safe ! The following example illustrates this problem :
1
2
3
4
5
6
7
8
9
10
11
12
public class MyServlet extends HttpServlet
{
private Object thisIsNOTThreadSafe; //Don"t to this
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
{
Object thisIsThreadSafe;
thisIsNOTThreadSafe = request.getParameter("foo"); // BAD!! Shared among all requests!
thisIsThreadSafe = request.getParameter("foo"); // OK, this is thread safe.
}
}
Don't do this , This can lead to software problems bug.
All the topics have been covered . Please look forward to more articles
边栏推荐
- token过期自动续费方案和实现
- File contains vulnerability (I)
- Some descriptions of Mipi protocol of LCD
- Servlet web XML configuration details (3.0)
- Software testing - concept
- mysql的约束总结
- 借力 Google Cloud 基础设施和着陆区,构建企业级云原生卓越运营能力
- Deep learning classification network -- alexnet
- 脑与认知神经科学Matlab Psytoolbox认知科学实验设计——实验设计四
- Redis key value database [primary]
猜你喜欢
随机推荐
Stc8h8k series assembly and C51 actual combat - digital display ADC, key serial port reply key number and ADC value
Shenji Bailian 3.52-prim
51 single chip microcomputer - ADC explanation (a/d conversion, d/a conversion)
Summary of MySQL constraints
Brain and cognitive neuroscience matlab psychoolbox cognitive science experimental design - experimental design 4
External interrupts cannot be accessed. Just delete the code and restore it Record this unexpected bug
492.构造矩形
Vite打包后的dist不能直接在浏览器打开吗
Can't the dist packaged by vite be opened directly in the browser
php获取cpu使用率、硬盘使用、内存使用
Mathematical statistics and machine learning
TI毫米波雷达学习(一)
PHP obtains some values in the string according to the specified characters, and reorganizes the remaining strings into a new array
php内类名称与类内方法名相同
Vscode paste image plugin saves image path settings
数据挖掘方向研究生常用网站
JS determines whether the mobile terminal or the PC terminal
Jetpack Compose 与 Material You 常见问题解答
Eco express micro engine system has supported one click deployment to cloud hosting
vite如何兼容低版本浏览器