当前位置:网站首页>Serenvlt first met
Serenvlt first met
2022-06-25 12:59:00 【Bo0o2】
One servelt What is it?
setvelt Is a dynamic page technology , It's a group. Tomcat For programmers API, Help programmers develop simply and efficiently
Two Servlet Operation principle
stay Servlet In our code, we didn't write main Method , So the corresponding doGet How is the code called ? How the response is returned to the browser ?
Tomcat The positioning of
Our own realization is in Tomcat Based on .
When the browser sends a request to the server , Tomcat As HTTP The server , You can receive this request .
HTTP As an application layer protocol , The underlying protocol stack is needed to support the work . As shown in the figure below :
3、 ... and HttpServlet
We write Servlet Code time , The first step is to create a class , Inherited from HttpServlet, And override some of them 
Code instance processing get request :
establish doget Method 
@WebServlet("/method")
public class method extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.getWriter().write("GET response");
}
}

Enter... In the browser url Address , So the browser sends a message to the server get request 
Code instance processing post request :
First create a static html
<button onclick="sendPost()"> send out POST request </button>
<script> function sendPost() {
ajax({
method: 'POST', url: 'method', callback: function (body, status) {
console.log(body); } }) } </script>
Then create a post Method :
stay method Created in dopost Method :
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
resp.setContentType("text/html; charset=utf-8");
resp.getWriter().write("POST Respond to ");
}
Click submit in the browser “post request ‘’
Four What to do if there is an error in accessing ?
Beginners Servlet, There will be many such problems . We don't just have to learn Servlet The basic way of writing code , We should also learn how to check for mistakes
Ideas .
Program debugging BUG Like a doctor .
An experienced programmer is compared to a novice programmer , The biggest advantage is not how well the code is written , But the debugging efficiency is
How high . The same problem may take a novice several days to solve , But an experienced programmer can do it in a few minutes .
be familiar with HTTP The protocol allows us to debug problems with half the effort .
4xx The status code of indicates that the path does not exist , Often need to check URL Whether it is right , And the... Set in the code Context Path as well as
Servlet Path Is it consistent .
5xx The status code of indicates that the server has an error , You often need to observe the content and content of the page prompt Tomcat Your own journal , Observe if
There is an error .
A connection failure often means Tomcat Not started correctly , Also need to observe Tomcat Whether there is an error prompt in your own log .
This situation of blank pages requires us to use the packet capture tool to analyze HTTP The specific interaction process of request response .
边栏推荐
- You can't specify target table 'xxx' for update in from clause
- list.replace, str.append
- 三行代码简单修改jar包的项目代码
- ByteDance dev better technology salon is coming! Participate in the activity to win a good gift, and sign up for free within a limited time!
- 5 kinds of viewer for browser
- Qt显示FFmpeg解码的图片
- Oracle backup or restore database (expdp, impdp)
- 剑指 Offer 04. 二维数组中的查找
- Native JS --- infinite scrolling
- Concat(), join(), reverse(), sort() method in JS array
猜你喜欢
随机推荐
yolov5训练使用的负样本图片
3+1保障:高可用系统稳定性是如何炼成的?
Geospatial search: implementation principle of KD tree
JS picture switching (simple and practical)
[flask tutorial] flask overview
剑指 Offer 第 1 天栈与队列(简单)
RESTful和RPC
Resolution of PPT paper drawing
20220620 interview reply
JS array de duplication
Koa frame
Why are databases cloud native?
时间过滤器(el-table)中使用
2021-09-30
ByteDance dev better technology salon is coming! Participate in the activity to win a good gift, and sign up for free within a limited time!
剑指offer 第 3 天字符串(简单)
几分钟上线一个网站 真是神器
剑指 Offer II 028. 展平多级双向链表
Match regular with fixed format beginning and fixed end
利用cmd(命令提示符)安装mysql&&配置环境








