HttpServlet Explanation (Note!) Imitation

Source: Internet
Author: User

The framework of a servlet is comprised of two Java packages: Javax.servlet and javax.servlet.http. The common interfaces and classes that all servlet classes must implement or extend are defined in the Javax.servlet package. The HttpServlet class with the HTTP communication protocol is defined in the Javax.servlet.http package.

The core of the servlet's framework is the Javax.servlet.Servlet interface, which must be implemented by all servlets. 5 methods are defined in the Servlet interface, with 3 methods representing the servlet's declaration period:

Init method, responsible for initializing the Servlet object
Service method, responsible for the corresponding customer's request
The Destory method is responsible for releasing the owning resource when the Servlet object exits the declaration cycle

When the Web container receives a servlet request, the servlet encapsulates the request into a HttpServletRequest object and then passes the object to the corresponding service method of the servlet.

HTTP requests include Delete,get,options,post,put and Trace, which provide the corresponding service methods in the HttpServlet class, namely, DoDelete (), Doget (), Dooptions (), DoPost (), DoPut () and Dotrace ().

Features of the HttpServlet

The HttpServlet container Response Web client request process is as follows:

1) The Web client makes an HTTP request to the servlet container;

2) The Servlet container parses the HTTP request of the Web client;

3) The servlet container creates an HttpRequest object that encapsulates the HTTP request information;

4) The servlet container creates a HttpResponse object;

5) The Servlet container invokes the service method of HttpServlet and passes the HttpRequest and HttpResponse objects as parameters of the service method to the HttpServlet object;

6) HttpServlet Call the method of HttpRequest, get the HTTP request information;

7) HttpServlet Call the method of HttpResponse, generate the response data;

8) The Servlet container transmits the HttpServlet response results to the Web client.

The following describes the HTTP request and response, an HTTP request that contains the request method, the request URL, the header field, and the request body. HTTP1.1 defines the following request method:

1. Get: Gets the resource identified by the request URL

2. POST: Send unlimited length data to the Web server

3. PUT: Store A resource to the requested URL

4. Delete: Delete the resource identified by the URL

5. Head: Return header information for URL identification

6. OPTIONS: Returns the HTTP method supported by the server

7. Trace: Returns the header field that came with the trace request

The HTTP response includes the response code, the header field, and the response body. The HTTP protocol requires that the response code and all header fields be returned before any response body.

The following is an introduction to the various service methods for Servlets.

1. Doget (): Invokes the server's resource and returns it as a response to the client. The Doget () call shows the data being transmitted to the servlet in the URL, which can pose some problems in the security aspects of the system, such as when a user logs on, The user name and password in the form need to be sent to the server side, and the Doget () call will display the user name and password in the URL of the browser.

2. DoPost (): It is used to pass the client's data to the server, which can be used to send data in a hidden way. Post is suitable for sending large amounts of data.

3. DoPut (): Calls are similar to Dopost (), and it allows the client to store real files on the server, rather than just transmitting data.

4. DoDelete (): It allows the client to delete server-side files or Web pages. It has very little use.

5. Dohead (): It is used to process the client's head call and returns a response. When the client only needs to respond to the header, it issues a header request. In this case, the client is often concerned about the length of the response and the MIME type of the response.

6. Dooptions (): It is used to process the options call of the client, through which the client can obtain the methods supported by this servlet. If the servlet overrides the Dopost () method, it will return:

Allow:post,trace,options,head

In general, you do not need to overwrite this method.

Steps to create a HttpServlet--"Four songs"

1) Extended HttpServlet abstract class;

2) Some methods covering httpservlet, such as covering doget () or Dopost () method;

3) Get HTTP request information. Retrieves the data submitted by the HTML form or the query string on the URL by using the HttpServletRequest object;

4) Generate HTTP response results. Generates a response result from the HttpServletResponse object, which has a getwriter () method that returns a PrintWriter object.

Here's an example:

Package mypack;
Import javax.servlet.*;
Import javax.servlet.http.*;
Import java.io.*;
public class HelloServlet extends httpservlet//first step: Extend HttpServlet abstract class
{
Step two: Overwrite the Doget () method
public void doget (HttpServletRequest request,
HttpServletResponse response) throws ioexception,servletexception{
Step three: Get the parameter information in the HTTP request
String clientname=request.getparameter ("ClientName");
if (clientname!=null)
Clientname=new String (clientname.getbytes ("iso-8859-1"), "GB2312");
Else
Clientname= "my Friend";

Fourth step: Generate HTTP Response results
PrintWriter out;
String title= "HelloServlet";
String heading1= "Output of the Doget method of HelloServlet:";
Set Content Type
Response.setcontenttype ("text/html;charset=gb2312");
Write HTML page
Out=response.getwriter ();
Out.print ("<HTML><HEAD><TITLE>" +title+ "</TITLE>");
Out.print ("</HEAD><BODY>");
Out.print (HEADING1);
Out.println ("Out.print ("</BODY></HTML>");

Out.close ();
}
}

Add in Web. xml

<servlet>
<servlet-name>HelloServlet</servlet-name>
<servlet-class>mypack. Helloservlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>

Access HelloServlet via URL:

Attention:

Implement the Service method.

The main function of the servlet is to accept HTTP requests sent from the browser (request) and return an HTTP response (response). This work is done in the service method. The service method includes obtaining client data from the request object and creating output to the response object.

If a servlet inherits from Javax.servlet.http.HttpServlet and implements the Dopost or Doget method, the servlet can only respond to post or get. If the developer wants to process all types of requests (request), simply implement the service method (but if you choose to implement the service method, you do not have to implement the Dopost or Doget method, Unless you call Super.service () at the beginning of the service method.

HttpServlet Explanation (Note!) Imitation

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.