Java Basics (eight)

Source: Internet
Author: User

1. What is the workflow of a Java Web page request?


The browser is a tool software: 1) completes the interaction with the server side; 2) completes the parsing of HTML (hypertext language). In order to realize the user needs to view the resource information in an intuitive form to show.

In the above process 2 browsers assemble the request into the specified format and send it to the server, and the client and the server end up with HTTP to accomplish the specific interaction. The requested data flow mainly consists of HTTP (Hypertext Transfer Protocol, a protocol based on TCP/IP, mainly used to implement client-server communication), request method (get or post), request URL (URL, Uniform Resource Locator), and some parameters of the request. 5 The server is returned to the client by assembling the response message into a specific message format via HTTP, and the data flow of the response mainly includes a status code (on behalf of the request success or failure), Content-type (text, picture, HTML, etc.), The content of the response message (picture or HTML-formatted content).

2. The difference between get and post methods in HTTP

There are many kinds of methods for HTTP requests: GET, POST, HEAD, TRACE, options, etc., where:

Get is the simplest method of request, the main function is to obtain the user's resources from the server side, as a response to the client, these resources can be HTML pages, pictures, documents and other content, get as the database query operation does not change the state of the resource itself.

Post in addition to the function of get can also upload data to the server, the delivery of data through the HTTP request of the attachment, the amount of data transferred more, the general default is unrestricted. and the content it submits to the server is not visible in the URL and is not visible to the user and is more secure.

3. What is a servlet

Dynamic pages are pages that display different content based on different times and different users. Methods for generating dynamic pages 1) Adopt a Public Gateway interface (CGI, a program written in Perl script). 2) Use the Servlet method. It is a server-side program written in the Java language that runs in a servlet container in a Web server, whose primary function is to provide a request/Response Web service pattern that generates dynamic Web content, which is what HTML does not have.

Servlet advantages: 1. Good portability; 2. High execution rate, because CGI creates a process for each request, and the servlet creates a thread for each request to execute, while creating the line turndown creates a process with little overhead ; 3. Powerful, Servlets can interact with Web servers, and CGI cannot; 4. For ease of use, the servlet provides many very useful interfaces for reading or setting HTTP header messages, processing cookies and tracking session state; 5. Scalability is strong.

Steps for the servlet to handle client requests:

1) The user initiates a request to the servlet by clicking on a link.

2) After the Web server receives the request, it will submit the request to the appropriate container for processing, and when the container finds that this is a request to the servlet, the container will now create two objects: HttpServletResponse and HttpServletRequest.

3) The container can find the corresponding servlet based on the URL message in the request message, then create a separate thread for the request, and pass the two objects created in 2 as parameters to the newly created thread.

4) The container invokes the Servlet's service () method to complete the response to the user request. The Service () method invokes the Dopost () or Doget () method to complete the specific response task while returning the generated dynamic page to the container.

5) The container assembles the response message into an HTTP format that is returned to the client, at which point the thread ends, while deleting 2) creates two objects.


4. Selection of the DoPost () method and the Doget () method

The Doget () method is called when the method property in the HTTP request is get (), and the Dopost () method is called when the Post property is used.

5. The life cycle of the servlet

The entire life cycle is controlled by the container. There are two states: The state is not created and the initialization state is controlled primarily by the Init (), service (), and Destroy () methods. The init () method is used to create or open any servlet-related resources and perform initialization work; The service () method is the method in the servlet that really handles requests from the client, and the Destroy () method frees any servlet-related resources opened in the Init () method. Specifically, the servlet lifecycle can be divided into loading, creating, initializing, processing client requests, and unloading. where the Init () and Destroy () methods will only be called once, and the service () method will create a new thread to process the request when a new client request arrives.

6. Advantages of JSP (Java serverpages)

is a dynamic technical standard, which is an HTML file embedded in Java code. Because of the large flaw in the implementation of the servlet: the user-responsive view must be assembled into a long HTML-formatted string written into the println () method. The idea of JSP is that each servlet is responsible for the processing of its corresponding business logic, allowing the JSP to be responsible for the user's HTML display.

The similarities and differences between JSP and Servlet: JSP is an extension to the servlet, because the JSP page will eventually be converted into a servlet to run, so the processing request is actually a compiled servlet.

7. Implement MVC (model, View, Controller) model using JSP and servlet


Java Basics (eight)

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.