当前位置:网站首页>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 :
 Insert picture description here

3、 ... and HttpServlet

We write Servlet Code time , The first step is to create a class , Inherited from HttpServlet, And override some of them
 Insert picture description here
Code instance processing get request :
establish doget Method
 Insert picture description here

@WebServlet("/method")
public class method  extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.getWriter().write("GET response");
    }
}

 Insert picture description here
Enter... In the browser url Address , So the browser sends a message to the server get request
 Insert picture description here
Code instance processing post request :
First create a static html
 Insert picture description here

<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 ‘’
 Insert picture description here

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 .

原网站

版权声明
本文为[Bo0o2]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/176/202206251218299606.html