当前位置:网站首页>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");
边栏推荐
- Apache service suspended asynchronous acceptex failed
- 新库上线 | CnOpenData中国保险机构网点全集数据
- Kubernetes resource object introduction and common commands (V) - (NFS & PV & PVC)
- Leetcode13. Roman numeral to integer (three solutions)
- Luogu: p1155 [noip2008 improvement group] double stack sorting (bipartite graph, simulation)
- Free data | new library online | cnopendata complete data of China's insurance intermediary outlets
- What is your income level in the country?
- PS screen printing brush 131, many illustrators have followed suit
- Enterprise custom form engine solution (XI) -- form rule engine 1
- 【RT-Thread】nxp rt10xx 设备驱动框架之--rtc搭建和使用
猜你喜欢
Swm32 series Tutorial 4 port mapping and serial port application
Applet setting multi account debugging
Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method
PHP online confusion encryption tutorial sharing + basically no solution
Introduction to SolidWorks gear design software tool geartrax
【RT-Thread】nxp rt10xx 设备驱动框架之--Pin搭建和使用
Play with fancy special effects. This AE super kit is for you
Wechat applet for the first time
Internet hospital his management platform source code, online consultation, appointment registration smart hospital applet source code
One brush 147-force deduction hot question-4 find the median of two positive arrays (H)
随机推荐
QT learning diary 9 - dialog box
Hongmeng third training
简单配置PostFix服务器
How to purchase Google colab members in China
Squid service startup script
UE4 official charging resources, with a total price of several thousand
自动渗透测试工具核心功能简述
What is your income level in the country?
How SVN views modified file records
New library online | cnopendata complete data of Chinese insurance institution outlets
[RT thread] NXP rt10xx device driver framework -- RTC construction and use
Leetcode 538 converts binary search tree into cumulative tree -- recursive method and iterative method
Examination questions for the assignment of selected readings of British and American Literature in the course examination of Fujian Normal University in February 2022
STM32H7 HAL库SPI DMA发送一直处于busy的解决办法
Stm32h7 Hal library SPI DMA transmission has been in busy solution
Leetcode Valentine's Day Special - looking for a single dog
An example of HP array card troubleshooting
国内如何购买Google Colab会员
C language modifies files by line
[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