Java Web (ii)--HTTP protocol & Servlet

Source: Internet
Author: User

HTTP protocol &servlet
Version 1.0 request data, after the server returns, will disconnect 1.1 request data, after the server returns, the connection will remain. Unless the server | The client shuts down. There is a certain time limit, if the connection is empty, then the back will be broken off.  Demonstrates how the client communicates with the server side. Type in the Address bar to enter the network or the usual registration, click on the registration button, the browser can show something. So secretly the browser and the server is how to communicate. What data they are transmitting. 1, install the Grab Kit tool HttpWatch (ie plugin) 2, open Tomcat. Enter localhost:8080 Open first Page 3, found on the home page example 6.x and 7.x document pages are different, but as long as found example can find example Project 4, select servlet Example---> Request Parame ter then click on the Execute hyperlink of request Parameters

Execute the Tomcat example and check the docking details of the browser and Tomcat server

HTTP Request data Interpretation request data contains three parts: request line, request header, request body 1, request line Post/examples/servlets/servlet/requestparamexample http/1.1
  Post: The request method, to post to submit the data/examples/servlets/servlet/requestparamexample request address path, is to access where. http/1.1 protocol version 2, request header accept:application/x-ms-application, Image/jpeg, Application/xaml+xml, Image/gif, Image/pjpeg, app  LICATION/X-MS-XBAP, */* referer:http://localhost:8080/examples/servlets/servlet/requestparamexample ACCEPT-LANGUAGE:ZH-CN user-agent:mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; trident/4.0; SLCC2;. NET CLR 2.0.50727;. NET CLR 3.5.30729;. NET CLR 3.0.30729; Media Center PC 6.0;. net4.0c;. net4.0e) content-type:application/x-www-form-urlencoded accept-encoding:gzip, deflate host:localhost:8080 Content-L   ength:31 connection:keep-alive Cache-control:no-cache Accept: The client indicates to the server what type of data I can support.   Referer: True requested address path, full path Accept-language: Support language format user-agent: User agent indicates to the server that the current client information is being visited. Content-type: The submitted data type.   The data accept-encoding:gzip of the form form urlencoding encoded, deflate: compression algorithm. Host: Address content-length: Data length connection:keep-alive remain connected Cache-control : For cache operation 3, the data sent by the request body browser to the server is Key=value, if there is more than one data, then use & firstname=zhang&lastname=sansanhttp response Data Parsing please The data in the request contains three parts: Response line, response Head, Response body http/1.1-OK server:apache-coyote/1.1 content-type:text/html;charset=iso-8859-1 C ontent-length:673 Date:fri, 02:53:02 GMT ... There's a lot of data here ...     1. Response Line http/1.1-OK Protocol version status Code what is the result of this interaction is a code.    200: Success, normal processing, get data. 403:for Bidden reject 404:not Found 500: Server exception OK corresponds to the previous status Code 3, response header server: Which type of server is. Tomcat Content-type: Server returned to client your content type Content-length: Data length returned date: Date of the communication, time of the response get and POST request differences

1, Post① data is written in a stream of the past, will not be displayed on the address bar.  It is now common to submit data to the server using Post② to write data in a stream, so there is no size limit to the data. 2, Get① will be in the address bar after stitching data, so there is a security risk. Generally from the server to obtain data, and the client does not have to submit the above data, you can use Get② can bring limited data, 1KB size Web resources in the HTTP protocol, the request and response to both sides, the client and server side.   Web-related resources. There are two categories of static resources HTML, JS, CSS
Dynamic resource servlet/jspservletHello servlet1, to write a Web project, to have a server.  2. Test run Web Project ① Create a new class that implements the Servlet interface ② configuration servlet, meaning: Tell the server that our application has such thousand servlet. In the Webcontent/web-inf/web.xml, write the following content.
<!--report to Tomcat, I have this servlet in this app, named HelloServlet, the path is Com.itheima.servlet.HelloServlet -<servlet><Servlet-name>HelloServlet</Servlet-name><Servlet-class>Com.itheima.servlet.HelloServlet</Servlet-class></servlet>          <!--registers the mappings for the servlet. Servletname: Find the specific servlet registered above, Url-pattern: The path on the address bar must be preceded by A/ -<servlet-mapping><Servlet-name>HelloServlet</Servlet-name><Url-pattern>/A</Url-pattern></servlet-mapping>
3. Enter the name of the HTTP://LOCALHOST:8080/project in the Address bar/aservlet Execution Process! [Icon] (img/img05.png)common wording for ServletsServlet (interface) | | genericservlet| | HttpServlet (Request for processing HTTP) 1. Define a class that inherits HttpServlet replication Doget and dopost! [Icon] (img/img06.png) The life cycle of the servlet 1, life cycle > A period of time from creation to destruction * life cycle methods > The methods that are called from creation to destruction. * The Init method executes this method when it creates an instance of the servlet. A servlet is initialized only once, and the Init method executes only once, by default: the first time that the servlet is accessed, the instance is created.  * Service method This method is executed as long as the client comes up with a request. The method can be executed many times. A request that corresponds to the invocation of the service method * The Destroy method is executed when the servlet destroys the method 1.  The project is removed from the inside of Tomcat. 2. Normal shutdown tomcat will execute Shutdown.bat > doget and Dopost are not life cycle methods, the so-called life-cycle approach refers to the method from the creation of objects to destruction, but these two methods do not necessarily execute. # # #让Servlet创建实例的时机 in advance. 1. By default, the Init method is only executed when the servlet is first accessed. Sometimes, we may need to do some initialization work in this method, or even to do some more time-consuming logic. 2. At this time, the initial visit may take too long to stay in the Init method. Then there is no way to make this initialization time a little earlier. 3. When configured, use the Load-on-startup element to specify that the smaller a given number, the sooner it will start. Generally do not write negative numbers, starting from 2 can be. <servlet> <servlet-name>HelloServlet04</servlet-name> <servlet-class> Com.itheima.servlet.helloservlet04</servlet-class> <load-on-startup>2</load-on-startup> </ servlet># #ServletConfig >servlet Configuration, this object allows the servlet toConfiguration of the time some information > first say, in writing how to use, and finally say what is the use. 1. Get the Servlet configuration object dedicated to configuring the servlet information servletconfig config = Getservletconfig ();//Get to the configuration servlet inside servlet-name text content string Servletname = Config.getservletname (); System.out.println ("servletname=" +servletname);//2,. You can get a specific parameter. String address = Config.getinitparameter ("Address"); System.out.println ("address=" +address)//3. Get all parameter names enumeration<string> names = Config.getinitparameternames ();//traverse out all parameter names while (Names.hasmoreelements ()) {string key = (string) names.nextelement (); String value = Config.getinitparameter (key); System.out.println ("key===" +key + "value=" +value);} # # #为什么需要有这个ServletConfig1. Some of the applications we develop ourselves in the future, using some technology, or some code, we don't. But someone wrote it out. Its code is placed in its own servlet class. 2. Just this servlet needs a number or a variable value. However, this value cannot be fixed. So the company that asked to use this servlet, when registering the servlet, must be in Web. XML, declaring that Init-params is less used in development. Just this experiment, hope that the basis of good or interested students, go back to do it yourself. # #总结 * HTTP protocol 1. Take a look at the details behind the HTTP request using the Httpwacht capture package. 2. Basic understanding of request and response data content request line, request header, request body Response Line, response header, Response body 3. The difference between get and post * Servlet "focus" 1. will use simple Servlet1. Write a class that implements the interface SERvlet2. Configure Servlet3. Will access the Setvlet2. The life cycle of the servlet init once creates the object the default initial access is called or can be configured to make it load-on-startupservice many times in advance, one request corresponding to the servicedestory once destroyed from the server removed or Gracefully shuts down server 3. ServletConfig Gets the configuration information, the params

Java Web (ii)--HTTP protocol & Servlet

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.