[Reprint] [Servlet first chapter introduces Servlet, HTTP protocol, WEB directory structure, preparation of entry procedures Servlet, Servlet life cycle]

The first chapter introduces [Servlet Servlet, HTTP protocol, WEB directory structure, preparation of entry procedures Servlet, Servlet life cycle]

https://segmentfault.com/a/1190000013124026

 

What is Servlet?

Servlet java class is actually a follow Servlet development. Servlet is invoked by the server, running on the server side.

Why A Servlet?

We write java program want to achieve in the online chat, posting, so some of the interactive features, ordinary java technology is very difficult to complete. sun Servlet company provides the technology for us to use.

HTTP protocol

What is HTPP agreement

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

In simple terms, HTTP protocol is a format communications client and server interaction.

Example: In the browser, click a link, the browser opens the page the link for me.

How it works: When you click this link in a browser, the browser sends a text to the server and tell the server which is a request to open a Web page. After the server receives the request, it returns a text to the browser, the browser will parse the text, and then displayed. This text is to follow the HTTP protocol specification.

HTTP1.0 and the difference HTTP1.1

HTTP1.0 agreement, the client establishes a connection with the web server, a web resource can only be obtained [short connection, after the access to resources is disconnected]

HTTP1.1 protocol that allows a client to establish connection with the web server, access to multiple web resources on a connection [connection] remain

HTTP request

When a browser requests a web resource to the server, called the browser sends a http request to the server.

A complete http request should contain three parts:

  1. Description request line the way the client request, the name of the requested resource, as well as the use of HTTP protocol version number]
  2. Description plurality of message header which hosts, as well as some environmental information of the client requests, the client]
  3. A blank line

Request line

The request line: GET /java.html HTTP / 1.1

A GET request line call mode request, request methods are: POST, GET, HEAD, OPTIONS, DELETE, TRACE, PUT.

Commonly used are: POST, GET

Generally speaking, when we click on a hyperlink to access all get request by way of the address bar. Data submitted through the form of generally post way.

GET can be simply understood way to query data, POST way to submit data, get faster than post submission

GET way: in the URL address comes after the parameter is limited, its data capacity is usually no more than 1K.

POST method: the server may transmit data to the content request entity, unlimited amount of data transferred.

Request header

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

HTTP response

An HTTP response indicates that the server send data back to the browser

A complete HTTP response should contain four parts:

  1. [A status line for the processing result of the server request description. ]
  2. [Description for the plurality of message header information describing the basic server, and data server through description of these data, the client may inform how it returns processing while the data]
  3. A blank line
  4. [Entity content server to the client to send data back]

State line

Format: HTTP status code version number of reasons described

Status line: HTTP / 1.1 200 OK

Status code is used to indicate the processing result of the request to the server, which is a three-digit decimal number. Response status code into five categories

Response header

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

The role of the Servlet

Servlet gives us the biggest role is to bring the browser to handle the HTTP request and returns a response to the browser, enabling interaction between the browser and the server.


JAVAWEB directory structure

Above diagram illustrates:

  • bbs directory represents a web application
  • html, jsp files in the directory can be accessed directly bbs browser
  • Resources under WEB-INF directory can not be accessed directly browser
  • web.xml file is the main configuration file for web applications
  • All classes files are placed in the classes directory
  • jar file in the lib directory

Implement the interface Servlet Servlet written procedures

My program is written by the idea, we must first configure Tomcat on the idea, there are tutorials on my other blog post!

Step Servlet written procedures

  • Create a custom class that implements the Servlet interface

  • We found five methods need to be rewritten, there is init [initialization], destroy [the destruction], service [service], ServletConfig Servlet configuration [], getServletInfo Servlet [information].
  • In this and saw that the service () method is the most likely place to write logic code.
  • First, write a first entry hellword
  • ServletResponse object's method calls HelloWorld output to the browser

  • Xml configuration file, the light is not enough to write a Servlet, Tomcat Servlet also know how to access the browser.

  • Access to write their own programs Servlet


Servlet life cycle

  • Here we look at the life cycle Servlet

  • First visit Servlet, we found init () and service () are called

  • Second visit Servlet, service () is called

  • Third visit Servlet, or service () is called

  • When we shut the Tomcat server time, destroy () is called!

Servlet life cycle can be divided into five steps

  1. Loading Servlet. When Tomcat Servlet's first visit, Tomcat will be responsible for creating an instance of the Servlet
  2. initialization. When the Servlet is instantiated, Tomcat calls the init () method initializes the object
  3. Processing services. When the browser to access the Servlet, Servlet calls the service () method to process the request
  4. destroy. When the Tomcat Servlet detected from closed or deleted when Tomcat will automatically call destroy () method, so that the share of resources freed example. Servlet If a long time without being used, Tomcat will be automatically destroyed
  5. Uninstall. When the Servlet End call destroy () method, wait for garbage collection. If you need to use this Servlet again, it will re-invoke init () method initializes.
  • Brief summary: ** Just visit Servlet, service () is called. init () when only the first visit to the Servlet will be invoked.

destroy () only closed when the Tomcat will be called. **


Servlet written procedures inherited HttpServlet

In the above we implement the Servlet interface, to achieve the five methods. This is too much trouble! The HttpServlet class has achieved all Servlet interface methods, the preparation of Servlet, need only to extend HttpServlet, you need to override the method, and it adds some processing methods and HTTP Servlet interface in the original agreement, it is more than Servlet interface the more powerful.

  • Generally, when we develop are rewritten doGet () and doPost () methods. For the idea, it has helped to create a Servlet when you rewrite Well


If the article is wrong place please correct me, we can exchange ideas. Used to see the article in the micro-channel technology students can focus on micro-channel public number: Java3y

Guess you like

Origin www.cnblogs.com/jinanxiaolaohu/p/12552516.html