http,servlet

What is http
hypertext transfer control protocol, application layer protocols define the browser (or may be another program) and the process of communication between the web server and format the data
during communication
1) the browser to the server connection establishment request .
2) packing the browser first request data, sends a request to the server.
3) The server processes the request, the data package is then transmitted to the browser.
4) After the server transmits the data, and closing the connection.
Request packet 2 response packet
Packet Structure:
1: request line (one line content packet)
comprising:
? Request embodiment (get / post) get
requests a resource path (contents port number Ho, such as / appname / servlet)? www.baidu.com request path
type is not the version of the protocol x`?
2: a number of message header (header is a special meaning of some key-value pairs defined by W3C)
style of the message header, such as: content-type = text / html;
server and the browser will abide by the agreement of the heads of these messages.
Mai header message generated by the server from the general who quit browser, but may be generated programmatically
3: Content Entity

If the request method is a post way, request parameters and values will be placed here.
If the request is to get embodiment mode, the request is not worth the parameter included in the request resource path inside.

Response Packet Structure:
first part: the status line
type is not the version of the protocol
status code (status code is a number, different state numbers represent

? 500: System error (ie, program code incorrectly)
404:? Can not find the resources (access path error)
200:? Correct
405

Second part: a plurality of message header
Part III: solid content
server returns a processing result to the browser


It is the sun expand the company to develop components for web services specifications
Servlet main tasks:
to read the data sent by the client
to generate the results
send explicit data (ie documentation) to the client and implicit data (HTTP response data)

Work process:
issuing a request to the web server at the client

After the web server receives the request sent to the Servlet

Generating a Servlet container of this instance of the object and call the appropriate methods ServletAPI the HTTP request to the client for processing and then returns the response to the result of the processing of the WEB server.

Servlet response from the web server configuration instance object received is sent back to the client.

 

Container:
Web container: WEB can be deployed in environments with multiple applications.

Tomcat container: a Java Servlet, JavaServer Pages, open source implementation of Java Expression Language and Java WebSocket (Java EE) technology.

Servlet container: full name of the server applet, which means service program. The main role is to the upper container (the Tomcat) provided doGet () and doPost () method and the like. Its life cycle instantiation, initialization, calling the destruction controlled by the Tomcat container.


Web container is responsible for the management and operation Servlet
container support for Servlet include
communication support (to request information and response information into a request object and a response object)
Servlet life cycle management (generated Servlet and destruction of Servlet, and servlet-related method calls)
multithreading support (thread creation, thread starts, the thread running thread synchronization)
JSP support for
handling security

Simply put:
Web container is mainly used in the application components to (Servlet, JSP) container provided an environment in which Servlet, JSP container interacts directly with the environment variable. In this way, we can focus on the business logic to achieve with Servlet, without paying attention to other system problems.

 

 

With
1) the client initiates a HTTP request to the Web server.

2) HTTP Web server request is accepted, if the request is for static pages, by the Web server handles. If the request is Java Web components (Servlet or JSP), then transferred to the Web container. Web container can start the process with a host of processes, different processes or other Web services host.

3) the specific Web-based Servlet container Servlet profile determined according to the call, and the target request, response object passed to it.

4) Servlet request by the client object knows who the user is, the customer's request and what information is some other information. After the request has been handled Servlet information to be returned in response to the client object is returned.

5) Upon completion of the processing request Servlet, Web container refreshes response, the control returns to the web server.

 

 


servlet life cycle
life cycle refers to how to create a servlet container instance, allocating resources, call its methods, examples of the destruction process of
Phase One: instantiated (servlet is to create an object, call the constructor)
will be the object instance in the following two cases of.
The first case:
when the request reaches the container, the servlet container to find out whether an object exists, and if not, will create an instance.
The second case:
when a container at startup, or the deployment of a new application, which checks the web.xml, whether there servlet load-on-starup configuration. If so, the servlet instance is created.
load-on-starup parameter value is smaller, the higher priority level (minimum value is 0, the highest priority).
<the servlet>
<the servlet-name> LoginServlet </ the servlet-name>
<the servlet-class> com.gezhi.controller.LoginServlet </ the servlet-class>
<Load-ON-Startup>. 1 </ Load-ON-Startup>
< / servlet>
experiment 1: set when the constructor, and the observation of multiple access method for performing service has
experiment 2: load-on-starup added in the configuration file, the timing of the observed instance

Phase II: initialization
for the allocation of resources servlet, call the init (ServletConfig config);
initialization parameter method config object can be used to access the servlet.
Initialization parameters using the parameter init-param configuration.
init can override.
Experiment 1: override init process performs the actual observed (and upper mating experiments)
Experiment 2: Add <init-param>, and by the value config
<the init-param>
<param-name> name </ param-name>
< value-param> zhangsan </ param-value>
</ the init-param>



Config = getServletConfig the ServletConfig ();
String = config.getInitParameter the Add ( "address");
, ------------------------------ ---------------
If you are not allowed to participate in the init then get the ServletConfig
name = config.getInitParameter ( "name");

Stage III: Ready / call
request arrives the container, the container calls the service servlet object () method.
The HttpServlet service () method will be based on the request method to invoke doGet () or doPost () method.
However, these two methods do by default, will throw an exception, need to subclass to override.

 

Phase IV: the destruction of
the vessel based on its own algorithm, the servlet object is no longer needed removed.
Before deleting, calls the servlet object's destroy () method.
destroy () method for releasing resources.
Among the entire life cycle of a servlet, init, destroy only once, while the service method will be executed multiple times.

 

Guess you like

Origin www.cnblogs.com/heqi-linxin1314/p/10953300.html
Recommended