当前位置:网站首页>Servlet specification Part II
Servlet specification Part II
2022-07-03 17:29:00 【Xiaogao who loves fitness】
First of all . Multiple Servlet Call rules between :
1. Prerequisite : Some requests come from the browser , Multiple servers are often required Servlet Collaborative processing , But browsers can only access one at a time Servlet, As a result, users need to manually initiate multiple requests through the browser to get the service .
2. Rules to improve user experience :
No matter how many brothers are involved in this request Servlet, Users only need to manually notify the browser to initiate a request .
3. Multiple Servlet Call rules between
1) Redirection solution
2) Request forwarding solution
second . Redirection solution :
1. working principle : For the first time, the user manually notifies the browser to access OneServlet.OneServlet After work , take TwoServlet The address is written to the response header Location Properties of the , Lead to Tomcat take 302 The status code is written to the status line .
After the browser receives the corresponding package , It will read 302 state . At this time, the browser will automatically respond according to location Attribute address initiates a second request , visit TwoServlet To complete the remaining tasks .
2. Implement the command .respone.sendRedirect(“ Request address ”)// Redirection should not be slashed , Because it represents the root directory of the site , If you don't add it, it means the project root directory , If you have to add / Project name /TwoServlet( The websites , Deployed in tomact in )
3. features
1) Request address : You can send the resource file address inside the current website to the browser (/ The websites / Resource file name )
You can also send the file address of other website resources to the browser (http://ip Address : Port number / The websites / Resource file name )
2) Number of requests : Send at least twice , But only the first time is when the user sends it manually , Other things automatically .
3) Request mode : Redirection solution , Notify the browser through the address bar to initiate the next request , therefore The request received through the resource file called by the redirection solution must be 【GET】
4) shortcoming : The redirection solution requires multiple round trips between the browser and the server , lot of time Consumed in round trips , Increase user waiting time
Third . Request forwarding solution :
1. principle : For the first time, the user manually requires the browser to access OneServlet. The Servlet After work , The current request object replaces the browser to Tomcat Send a request , Apply to call TwoServlet.Tomcat After receiving this request , Automatically call TwoServlet To complete the remaining tasks .
2. Implement the command :RequestDispatcher report = request.getRequestDispatcher("/ Resource file name ").forward( Current request object , The current response object ); Be sure to "/" Start with
3. advantage :
1) No matter how many... Are involved in this request Servlet, Users only need to manually send a request through the browser once
2)Servlet The call between takes place on the server computer , It saves the number of round trips between the server and the browser , Increase processing service speed
4. features :
1) Number of requests : In the course of the request , The browser sends the request only once
2) Request address : Only to Tomcat The server requests to call the resource file address under the current website
request.getRequestDispathcer("/ Resource file name ") // Don't write the website name
3) Request mode : During request forwarding , The browser sends only one Http Request agreement package , All involved in this request Servlet Share the same request protocol package , therefore , these Servlet The received request mode is consistent with the request mode sent by the browser .
Fourth . Multiple Servlet Implementation scheme of data sharing between :
1 Data sharing :OneServlet After work , Give the generated data to TwoServlet To use
2.Servlet Four data sharing schemes are provided in the specification
1.ServletContext Interface :
1) come from Servlet An interface in the specification . stay Tomcat The server is responsible for providing this interface implementation class
2) If more than one Servlet From the same website , Between each other through the website ServletContext Instance object ( Global scope object ) Implement data sharing
working principle : Every web site has a global scope object . This global scope object 【 amount to 】 One Map.
In this website OneServlet You can put a data Save to global scope object , Other in the current web site
Servlet At this time, this data can be obtained from the global scope object for use .
Global scope object lifecycle :
1) stay Http During server startup , Automatically create a global scope object in memory for the current web site .
2) While the server is running , A web site has only one global scope object
3) stay Http While the server is running , The global scope object is always alive
4) When the server shuts down , Responsible for the current website ServletContext Destroy
ServletContext The object life cycle runs through the entire operation of the website
Command implementation :
Loading data OneServlet:ServletContext application = request.getServletContesst();
application.setAttribute("key1", data ) // similar map aggregate .
Take the data TwoServlet:ServletContext application = request.getServletContesst();
Object data = application.getAttribute("key1");
2.Cookie class
1. Introduce :Cookic From Servlet A tool class in the specification ,
If two Servlet From the same website , And for the same browser / Service provided by users , At this time, with the help of Cookic Object for data sharing
cookie Store the private data of the current user , Improve the quality of service in the process of sharing data
In real life situations ,Cookic Equivalent to the membership card obtained by the user on the server
2. principle : For the first time, the user through the browser to myweb The website sends a request for OneServlet.OneServlet Create a CooKie Store data related to the current user ,OneServlet After work , take Cookie Write to Response head Return to the current browser .
After the browser receives the response package , take Cookie Stored in the browser cache After a while , User pass The same The browser again directed myweb The website sends a request for TwoServlet when . The browser needs to unconditionally myweb Pushed by the website before Cookie, Write to Request header Send past . here TwoServlet At run time , You can read from the request header cookie In the information , obtain OneServlet Shared data provided
Implement the command : Same website OneServlet And TwoServlet With the help of Cookie Implement data sharing
CooKie ck1 = new Cookie(“key1”, data );
cookie ck2 = new Cookie(“key2”, data );
/*
cookie Equivalent to one map
One cookie Only one key value pair can be stored in the
The key value is right key And value Can only be String
Key value pair key It can't be Chinese
take cookie Write to the response header , Give it to the browser
resp.addCookie(ck1);
resp.addCookie(ck2)
*/
TwoServlet:
//1. Call the request object to get the returned from the browser from the request header Cookie
Cookie CookieArray【】 = request.getCookies();
//2. Traverse the data to get each cookie Of key And value
for(Cookie ck:cookieArray){
String key = ck.getName(); Read key
String value = ck.getValue(); Read value
}
Cookie Destruction opportunity :
1. By default ,cookie Objects are stored in the browser's cache , So as long as the browser closes ,Cookie The object is destroyed .
2. In manual settings , You can ask the browser to receive Cookie Stored on the hard disk of the client computer , At the same time, specify the survival time on the hard disk , When the survival time arrives , Will be automatically deleted from the hard disk .
3.HttpSession Interface
Introduce :
1).HttpSession Interface from Servlet An interface under the specification , Its implementation class consists of Http Provided by the server ,
2), If two Servlet From the same website , And for the same browser / Service provided by users , At this time, with the help of HiipSession Object for data sharing ( Conversation scope )
HttpSession And Cookie difference :
Storage location :Cookie: Stored on the client computer ( Browser memory / Hard disk )
HttpSession: Stored in the server computer memory
data type :Cookie Object storage shared data type can only be String
HttpSession Object can store any type of shared data Object
Number of data : One Cookie Object can only store one shared data
HttpSession Use map Collections store shared data , So you can store any number of shared data
object of reference : Cookie It is equivalent to the customer on the server side 【 Membership card 】
HttpSession It is equivalent to the customer on the server side 【 Private safe 】
Command implementation : Same website (myWeb) Next OneServlet Transfer data to TwoServlet
OneServlet:
HttpSession session = request.getSession();
session.setAttribute("key1", Shared data )
TwoServlet:
HttpSession session = request.getSession();
Object Shared data = session.getAttribute("key1");
.Http How the server connects users to HttpSession Connect :Tomcat take Session id As a cookie Object is written to the response header , Then the browser will automatically send this when it visits the website again cookie Object provides services at this time TwoServlet You can get the user's private locker by virtue of the cabinet number and read it from OneServlet Shared data written
getSession() And getSession(false)
getSession(): If the current user already has his own private locker on the server . requirement tomcat Return this private locker , If the current user does not have his own private locker on the server , requirement tocmat Create a new private locker for the current user
getSession(false): If the current user already has his own private locker on the server . requirement tomcat Return this private locker . If the current user does not have his own private locker on the server , here Tomcat Will return null
.HttpSession Destruction opportunity :
1. Users and HttpSession Used in association Cookie Can only be stored in the browser cache .
2. When the browser closes , It means that the user and his HttpSession The relationship is cut off
3. because Tomcat Unable to detect when the browser closes , Therefore, closing the browser does not cause Tomcat Associate the browser with HttpSession To destroy : To solve this problem ,Tomcat For every one HttpSession Object settings 【 free time 】
This idle time defaults to 30 minute , If at present HttpSession Object idle time reached 30 minute
here Tomcat Think users have given up their HttpSession, here Tomcat Will destroy this HttpSession
4.HttpServletRequest Interface
Introduce : In the same website , If two servlet Through between 【 Request forwarding 】 Method , Share the same request protocol package with each other . A request protocol package corresponds to only one request object , therefore servlet Share the same request object between , At this point, you can use this request object in two Servlet Data sharing between .
Implement... On the request object Servlet Data sharing between , Developers call the request object 【 Request scope object 】
. Command implementation : OneServlet Forward the request through the request to call TwoServlet when , Need to give TwoServlet Provide shared data
OneServlet:
request.setAttribute("key1", data ); // The data type can be any type Object
request.getRequestDispatcher("/two").forward(request,response);
TwoServlet:
Object data = request.getAttribute("key1");
边栏推荐
- Notes on problems -- watching videos on edge will make the screen green
- Javescript variable declaration -- VaR, let, const
- [combinatorics] recursive equation (summary of the solution process of recursive equation | homogeneous | double root | non-homogeneous | characteristic root is 1 | exponential form | the bottom is th
- RedHat 6.2 配置 Zabbix
- 互联网医院HIS管理平台源码,在线问诊,预约挂号 智慧医院小程序源码
- Svn full backup svnadmin hotcopy
- Leetcode Valentine's Day Special - looking for a single dog
- Dagong 21 autumn "power plant electrical part" online operation 1 [standard answer] power plant electrical part
- 数仓任务里面 跑SQL任务的时候用的数据库账号是在哪里配置的
- The difference between i++ and ++i: tell their differences easily
猜你喜欢
问题随记 —— 在 edge 上看视频会绿屏
Unity notes unityxr simple to use
Introduction to SolidWorks gear design software tool geartrax
互聯網醫院HIS管理平臺源碼,在線問診,預約掛號 智慧醫院小程序源碼
1147_ Makefile learning_ Target files and dependent files in makefile
[UE4] brush Arctic pack high quality Arctic terrain pack
Leetcode Valentine's Day Special - looking for a single dog
Redis: operation commands for list type data
Tensorboard quick start (pytoch uses tensorboard)
Simple use of unity pen XR grab
随机推荐
【JokerのZYNQ7020】DDS_ Compiler。
免费数据 | 新库上线 | CnOpenData中国保险中介机构网点全集数据
[combinatorics] recursive equation (example of solving recursive equation without multiple roots | complete process of solving recursive equation without multiple roots)
鸿蒙第四次培训
One brush 147-force deduction hot question-4 find the median of two positive arrays (H)
WEB-UI自动化测试-最全元素定位方法
鸿蒙第三次培训
Is AI too slow to design pictures and draw illustrations? 3 sets of practical brushes to save you
企业级自定义表单引擎解决方案(十一)--表单规则引擎1
自动渗透测试工具核心功能简述
RedHat 6.2 配置 Zabbix
The largest matrix (H) in a brush 143 monotone stack 84 histogram
Internet Hospital his Management Platform source, online Inquiry, appointment Registration Smart Hospital Small program source
Leetcode13. Roman numeral to integer (three solutions)
vs2013已阻止安装程序,需安装IE10
Open vsftpd port under iptables firewall
Apache服务挂起Asynchronous AcceptEx failed.
Installation and configuration of network hard disk NFS
Cloud primordial weekly | CNCF released the 2021 cloud primordial development status report, which was released on istio 1.13
Rsync远程同步