Servlet Part 1 [Introduction to Servlet, HTTP protocol, WEB directory structure, writing introductory servlet programs, servlet life cycle]

 

What is Serlvet?

Servlet is actually a java class that follows Servlet development . Serlvet is called by the server and runs on the server side .

Why use Serlvet?

We write java programs to realize chatting, posting, and other interactive functions on the Internet, which are very difficult to complete with ordinary java technology . Sun company provides Serlvet this technology for us to use.

HTTP protocol

What is the HTTP protocol

HyperText Transfer Protocol (HTTP) is the most widely used network protocol on the Internet. All WWW documents must comply with this standard. It is an application layer protocol of the TCP/IP protocol

Simply put, the HTTP protocol is a communication format for the interaction between the client and the server .

Example: Clicking a link in the browser, the browser will open the linked webpage for me.

Principle: When the link is clicked in the browser, the browser will send a piece of text to the server to tell the server which web page is requested to open. After the server receives the request, it returns a piece of text to the browser, and the browser parses the text and displays it. **This text follows the HTTP protocol specification.

The difference between HTTP1.0 and HTTP1.1

In the HTTP1.0 protocol, after the client establishes a connection with the web server, it can only obtain one web resource [short connection, disconnect after obtaining the resource]

HTTP1.1 protocol, which allows the client to obtain multiple web resources on one connection after establishing a connection with the web server [keep the connection]

HTTP request

When the browser requests a web resource from the server, it is called the browser sending an http request to the server.

A complete http request should contain three parts:

  1. Request line [Describe the client's request method , the requested resource name , and the HTTP protocol version number used ]
  2. Multiple message headers [describe which host the client requests, and some environmental information about the client, etc.]
  3. a blank line

request line

Request line: GET /java.html HTTP/1.1

The GET in the request line is called the request method , and the request methods are: POST, GET, HEAD, OPTIONS, DELETE, TRACE, PUT.

Commonly used are: POST, GET

Generally speaking, when we click on a hyperlink, access through the address bar is a get request method . The data submitted through the form is generally in the post method .

It can be easily understood that the GET method is used to query data , the POST method is used to submit data , and the submission speed of get is faster than that of post.

GET method: The parameters attached to the URL address are limited, and the data capacity usually cannot exceed 1K .

POST method: You can send data to the server in the requested entity content, and the amount of data transmitted is unlimited .

request header

  • Accept: text/html,image/* [The browser tells the server what data types it supports]
  • Accept-Charset: ISO-8859-1 [The browser tells the server which character set it supports ]
  • Accept-Encoding: gzip,compress [The browser tells the server which compression formats it supports ]
  • Accept-Language: en-us,zh-cn [The browser tells the server its locale]
  • Host: www.it315.org:80 [The browser tells the server which host it wants to access]
  • If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT [The browser tells the server when to cache the data]
  • Referer: http://www.it315.org/index.jsp [The browser tells the server that the client is from which page--- anti-hotlinking ]
  • 8.User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0) [The browser tells the server what the browser's kernel is]
  • Cookie [the browser tells the server what the cookie is brought ]
  • Connection: close/Keep-Alive [The browser tells the server whether to disconnect or keep the link after the request is complete]
  • Date: Tue, 11 Jul 2000 18:23:51 GMT [the browser tells the server the time of the request]

HTTP response

An HTTP response represents the server sending data back to the browser

A complete HTTP response should contain four parts:

  1. A status line [used to describe the result of the server's processing of the request.
  2. Multiple message headers [used to describe the basic information of the server and the description of the data , the server can notify the client how to process the data it sends back after a while through the description information of the data ]
  3. a blank line
  4. Entity content [ data sent back from the server to the client ]

status line

Format: HTTP version number Status code Reason statement

Status line: HTTP/1.1 200 OK

The status code is used to indicate the server's processing result of the request , and it is a three-digit decimal number . Response status codes are divided into 5 categories

 

 

response header

  • Location: http://www.it315.org/index.jsp [The server tells the browser which page to jump to ]
  • Server:apache tomcat [The server tells the browser what the model of the server is]
  • Content-Encoding: gzip [The server tells the browser the format of data compression ]
  • Content-Length: 80 [The server tells the browser to send back the length of the data]
  • Content-Language: zh-cn [The server tells the browser the locale of the server]
  • Content-Type: text/html; charset=GB2312 [The server tells the browser the type of data sent back ]
  • Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT [The server tells the browser when the resource was last updated]
  • Refresh: 1;url=http://www.it315.org[The server tells the browser to refresh regularly ]
  • Content-Disposition: attachment; filename=aaa.zip [The server tells the browser to open the data in download mode ]
  • Transfer-Encoding: chunked [The server tells the browser to send back data in chunks]
  • Set-Cookie:SS=Q0=5Lb_nQ; path=/search [The server tells the browser to save the cookie ]
  • Expires: -1 [The server tells the browser not to set the cache ]
  • Cache-Control: no-cache [The server tells the browser not to set the cache ]
  • Pragma: no-cache [The server tells the browser not to set the cache ]
  • Connection: close/Keep-Alive [The server tells the browser how to connect]
  • Date: Tue, 11 Jul 2000 18:23:51 GMT [The server tells the browser when to send the data back]

The role of servlets

The biggest role that Servlet brings us is that it can process HTTP requests brought by the browser and return a response to the browser, so as to realize the interaction between the browser and the server .

JAVAWEB directory structure

 

 

The above figure explains:

  • The bbs directory represents a web application
  • The html and jsp files in the bbs directory can be accessed directly by the browser
  • The resources in the WEB-INF directory cannot be directly accessed by the browser
  • The web.xml file is the main configuration file of the web program
  • All classes files are placed in the classes directory
  • The jar file is placed in the lib directory

Implement Servlet interface and write servlet program

I write all the programs under the idea. First, I need to configure Tomcat on the idea. There are tutorials in my other blog posts!

Steps to write a servlet program

  • Create a custom class that implements the Serlvet interface

 

 

  • We found that there are 5 methods that need to be rewritten, including init [initialization], destroy [destroy], service [service], ServletConfig [Servlet configuration], and getServletInfo [Serlvet information].

  • At this point, it turns out that the service() method is the most likely place to write the logic code.

  • First write a hellword to get started

  • Call the method of the ServletResponse object to output HelloWorld to the browser

 

 

  • To configure the xml file, it is not enough to just write a servlet. Tomcat also needs to know how the browser accesses this servlet.

 

 

  • Access to Serlvet programs written by yourself

 

 

Serlvet Lifecycle

  • Let's take a look at the life cycle of Servlet

     

  • The first time we visited the servlet, we found that both init() and service() were called

 

 

  • The second time the servlet is accessed, service() is called

 

 

  • The third time the servlet is accessed, service() is still called

 

 

  • When we shut down the Tomcat server , destroy() was called!

 

 

Servlet life cycle can be divided into 5 steps

  1. Load the servlet . When Tomcat accesses the servlet for the first time, Tomcat will be responsible for creating an instance of the servlet
  2. Initialize . When the servlet is instantiated, Tomcat will call the init() method to initialize the object
  3. Processing services . When the browser accesses the servlet , the servlet  will call the service() method to process the request
  4. destroy . When Tomcat shuts down or detects that the servlet is to be deleted from Tomcat, the destroy() method is automatically called to let the instance release the occupied resources . If a servlet is not used for a long time, it will also be automatically destroyed by Tomcat
  5. Uninstall . When the servlet calls the destroy() method, it waits for garbage collection. If the servlet needs to be used again, the init() method will be called again for initialization .
  • Brief summary: whenever a servlet is accessed, service() will be called. init() is only called when the servlet is accessed for the first time. destroy() will only be called when Tomcat is shut down.

Inheriting HttpServlet to write servlet program

In the above we implement the Servlet interface, to implement 5 methods. This is too much trouble! The HttpServlet class has already implemented all the methods of the Servlet interface . When writing a Servlet, you only need to inherit HttpServlet and rewrite the methods you need , and it adds some HTTP protocol processing methods to the original Servlet interface , which is more efficient than the Servlet interface . function is more powerful .

  • Generally, when we develop, we rewrite the doGet() and doPost() methods . For idea, it has been rewritten for you when you create a servlet

 

 

If there are any mistakes in the article, please correct me, and we can communicate with each other. Students who are used to reading technical articles on WeChat can pay attention to the WeChat public account: Java3y

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325120119&siteId=291194637