当前位置:网站首页>Servlet learning (II)
Servlet learning (II)
2022-06-21 18:50:00 【LvhaoIT】
Servlet Study ( Two )
List of articles
7、 ... and 、 Resource files... Welcome
1. Premise :
Users can remember the website name , But you won't remember the website resource file name
2. The default welcome resource file :
The user sent a message for a website 【 Default request 】 when ,
At this time by Http The resource file automatically returned by the server from the current website
Normal request : http://localhost:8080/myWeb/index.html
Default request : http://localhost:8080/myWeb/
3.Tomcat For the default welcome resource file location rule
Rule location :Tomcat Installation position /conf/web.xml
The rules order :
<welcome-file-list> <welcome-file>index.html</welcome-file> <welcome-file>index.htm</welcome-file> <welcome-file>index.jsp</welcome-file> </welcome-file-list>4. Set the default welcome resource file rules for the current web site
Rule location : Website /web/WEB-INF/web.xml
The rules order :
<welcome-file-list> <welcome-file>login.html</welcome-file> </welcome-file-list>Site settings customize default file location rules , here Tomcat The built-in positioning rules will be invalid
Model writing :
<!-- Customize the default welcome resource file -->
<welcome-file-list>
<welcome-file>login.html</welcome-file>
<!-- Dynamic resource files can also be welcome files , But remove the top of the alias / -->
<welcome-file>user/login</welcome-file>
</welcome-file-list>
8、 ... and 、Http Status code
1. Introduce :
1) A symbol consisting of three digits .
2)Http Before the server pushes the response package , According to the processing of this request
take Http The status code is written into the response package 【 Status line 】 On
3) If Http The server is responding to this request , The corresponding resource file is returned .
adopt Http The status code tells the browser how to handle the result
If Http The server is responding to this request , Unable to return the corresponding resource file
adopt Http The status code explains to the browser why the service cannot be provided
2. classification :
1) form 100—599; It is divided into 5 Two categories:
2)1XX :
The most characteristic 100; Notify the browser of the returned resource file
Not a separate resource file , The browser is required to receive
After the response package , Go on to Http Other resource files that the server depends on3) 2XX:
The most characteristic 200, Inform the browser that the resource file returned this time is a
Complete independent resource file , The browser does not need to... After receiving it
Other related documents
4)3xx:
The most characteristic 302, Notify the browser that the content of a resource file is not returned this time
It's a resource file address , The browser needs to automatically initiate according to this address
Request to request this resource fileresponse.sendRedirect(“ Resource file address ”) Writes to the response header
location
And this behavior leads to Tomcat take 302 The status code is written to the status line5)4XX:
404: Notification browser , Because the accessed resource file is not located on the server
So I can't help 405: Notification browser , The server has located the accessed resource file (Servlet)
But this Servlet The request mode adopted by the browser cannot be processed6)5xx:
500: Notification browser , The server has located the accessed resource file (Servlet)
This Servlet You can receive requests from the browser , however Servlet Processing
During the period of the request , because Java Exception causes processing failure
Nine 、 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, This causes users to manually browse through the browser
Make multiple requests to get service .
This makes it more difficult for users to obtain services , Cause users to give up visiting the current website
2. Rules to improve user experience :
No matter how many... Are involved in this request Servlet, Users only need 【 Manual 】 Notify the browser to initiate
One request is enough
3. Multiple Servlet Call rules between :
1) Redirection solution
2) Request forwarding solution
Ten . Redirection solution :
1. working principle : The user passes for the first time 【 Manual mode 】 Notify the browser to access OneServlet.
OneServlet After work , take TwoServlet Write the address 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 response packet , It will read 302 state . At this point, the browser
Automatically according to... In the response header location Attribute address initiates a second request , visit
TwoServlet To complete the remaining tasks in the request
2. Implement the command :
response.sendRedirect(“ Request address ”)
Write the address to the response header in the response packet location attribute
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
The browser sends a request at least twice , But only the first request is sent manually by the user .
Subsequent requests are automatically sent by the browser .
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
Example :
package com.controller.chongdingxiang;
import java.io.IOException;
public class oneServlet extends javax.servlet.http.HttpServlet {
protected void doPost(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
}
protected void doGet(javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws javax.servlet.ServletException, IOException {
System.out.println("gogogo1");
// Redirect
response.sendRedirect("/servlet/two");
System.out.println("gogogo2");
}
}
package com.controller.chongdingxiang;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class twoServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<p1> Now it is two Of doget</p1>");
}
}
Execute screenshot :

11、 ... and 、 Request forwarding solution :
1. principle : For the first time, the user manually requires the browser to access OneServlet.
OneServlet After work , Replace the browser with the current request object
towards Tomcat Send a request , Apply to call TwoServlet.
Tomcat After receiving this request , Automatically call TwoServlet Come on
Complete the remaining tasks
2. Implement the command : Request object instead of browser to Tomcat Send a request
//1. Generate a resource file application report object through the current request object
RequestDispatcher report = request.getRequestDispatcher("/ Resource file name ");
// Be sure to "/" Start with
//2. Send the report object to Tomcat
report.forward( Current request object , The current response object )
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 , Save the number of round trips between the server and the browser
Increase processing service speed
4. features :
1) Number of requests
During request forwarding , 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 sent only one Http Request agreement package .
All involved in this request Servlet Share the same request protocol package , therefore
these Servlet Received Request mode and browser How to send the request bring into correspondence with
Example :
package com.controller.zhuanfa;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class threeServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("Three page ");
//1. Generate a resource file application report object through the current request object
System.out.println("gogogo3");
RequestDispatcher report = request.getRequestDispatcher("/four");
report.forward(request, response);
System.out.println("gogogo4");
}
}
package com.controller.zhuanfa;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
public class fourServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=utf-8");
PrintWriter out = response.getWriter();
out.println("<p1> Now it is four Of doGet</p1>");
}
}
Execute screenshot :

Twelve 、 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
2.Cookie class
3.HttpSession Interface
4.HttpServletRequest Interface
13、 ... and 、ServletContext Interface :
1. Introduce :
1) From Servlet An interface in the specification . stay Tomcat in servlet-api.jar
stay Tomcat Is responsible for providing this interface implementation class
2) If two Servlet From the same website . Between each other through the website ServletContext
The instance object realizes data sharing
3) Developers are used to putting ServletContext The object is called 【 Global scope object 】
2. 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, you can get from the global scope object
Use this data
3. Global scope object lifecycle :
1) stay Http During server startup , Automatically create in memory for the current web site
A global scope object
2) stay Http When 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) stay Http When the server is ready to shut down , Be responsible for the global scope objects in the current website
Destroy
The global scope object life cycle runs through the entire running period of the website
4. Command implementation :
【 Same website 】OneServlet Share data with TwoServlet
OneServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response){
//1. adopt 【 Request object 】 towards Tomcat Ask for information in the current website 【 Global scope object 】
ServletContext application = request.getServletContext();
//2. Add data to the global scope object as 【 Shared data 】
application.setAttribute("key1", data )
}
}
TwoServlet {
public void doGet (HttpServletRequest request, HttpServletResponse response){
//1. adopt 【 Request object 】 towards Tomcat Ask for information in the current website 【 Global scope object 】
ServletContext application = request.getServletContext();
//2. Get the data corresponding to the specified keyword from the global scope object
Object data = application.getAttribute("key1");
}
}
边栏推荐
- 【艾思软件】微信小程序开发报价方案模版
- Microbial personal notes taxonkit
- 会议聊天室--开发文档
- R语言 bug?报错?关于亚变量0、1 结局outcome,outcome变量经过factor和numeric过程,改变了原始内容?
- C语言__attribute__(packed)属性(学习一下)
- Global installation of node
- 如何通过 dba_hist_active_sess_history 分析数据库历史性能问题
- I/0多路转接之select
- Servlet学习(二)
- 揭秘支撑百度搜索、Feed、小程序三大业务的MVVM框架设计思想,San 核心人员倾力打造
猜你喜欢

I/0多路转接之select

力扣160. 相交链表
![[AISI software] wechat applet development quotation scheme template](/img/eb/2f7e8977910a7c016bb3f6ede286f6.png)
[AISI software] wechat applet development quotation scheme template

C2—Qt实现串口调试助手2021.10.21

Disclose the design idea of MVVM framework supporting Baidu search, feed and applet, and San core personnel have made great efforts to build it

Crawling frog SEO spider

ACL 2022 | 基于自监督图对齐的多语言知识图谱推理

Canvas动态网状背景js特效

C3—Qt实现五子棋小游戏(一)2021.11.07

工地建设动画网页建设中js特效
随机推荐
数据库主键一定要自增吗?有哪些场景不建议自增?
Must the database primary key be self incremented? What scenarios do not suggest self augmentation?
网络爬虫开发工具:Screaming Frog SEO Spider
Three.js 3d粒子动画js特效代码
Three. JS 3D particle animation JS special effect code
写着玩的处理框架
Node的模块导入方式
TypeScript编译生成文件对比
缓存设计问题
启动!阿里巴巴编程之夏2022
Canvas dynamic background text luminous JS effect
Day11QPainter2021-09-26
产品图文列表说明布局样式
VsCode自定义模板,用模板记笔记?!
【艾思软件】微信小程序开发报价方案模版
文末送书 | 李航老师新作!机器学习经典著作《统计学习方法》全新升级
剑指 Offer 28. 对称的二叉树
力扣239. 滑动窗口最大值
R语言 bug?报错?关于亚变量0、1 结局outcome,outcome变量经过factor和numeric过程,改变了原始内容?
协同过滤(Collaborative Filtering)