Detailed HttpServlet

Detailed explanation of HttpServlet:
The Servlet framework is composed of two Java packages: javax.servlet and javax.servlet.http. 
The javax.servlet package defines the common interfaces and classes that all servlet classes must implement or extend. In javax The .servlet.http package defines the HttpServlet class that uses the HTTP communication protocol.

The core of the Servlet framework is the javax.servlet.Servlet interface, and all servlets must implement this interface. Five methods are defined in the Servlet interface, among which There are three methods representing the declaration cycle of servlets: the
init method, which is responsible for initializing
the service method of the servlet object, and the destroy method, which is responsible for the request of the corresponding client
. When the servlet object exits the declaration cycle, it is responsible for releasing the occupied resources.

When the web container receives a servlet When making a request, the Servlet encapsulates the request into an HttpServletRequest object, and then passes the object to the corresponding service method of the Servlet.
HTTP request methods include DELETE, GET, OPTIONS, POST, PUT and TRACE, which are provided in the HttpServlet class respectively. Service methods, they are, doDelete(), doGet(), doOptions(), doPost(), doPut() and doTrace(). 

HttpServlet functions  
HttpServlet must first read the content of the Http request. The servlet container is responsible for creating the HttpServlet object, and directly encapsulates the Http request into the HttpServlet object, which greatly simplifies the workload of HttpServlet parsing the request data. The HttpServlet container responds to the web client request process as follows:
1) The web client sends 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 and encapsulates the Http request information in this object;
4 ) The Servlet container creates an HttpResponse object;
5) The Servlet container calls the service method of HttpServlet, and passes the HttpRequest and HttpResponse objects as parameters of the service method to the HttpServlet object;
6) HttpServlet calls the relevant methods of HttpRequest to obtain HTTP request information;
7) HttpServlet Invoke the relevant methods of HttpResponse to generate response data;
8) The Servlet container transmits the response result of HttpServlet to the Web client.

2. Steps to create HttpServlet - "Four Steps"
1) Extend HttpServlet abstract class;
2) Override some methods of HttpServlet, such as overriding doGet() or doPost() methods;
3) Obtain HTTP request information. Retrieve the data submitted by the HTML form or the query string on the URL through the HttpServletRequest object;
4) Generate HTTP response results. The response is generated through the HttpServletResponse object, which has a getWriter() method that returns a PrintWriter object.
An example is as follows:
package mypack;
import javax.servlet.*;
import javax.servlet.http.*;
import java.io.*;
public class HelloServlet extends HttpServlet//The first step: extend the HttpServlet abstract class 
{
 //The first Step 2: Override the doGet() method
 public void doGet(HttpServletRequest request,
  HttpServletResponse response) throws IOException, ServletException{
  //Step 3: 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";


  PrintWriter out;
  String title="HelloServlet";
  String heading1="HelloServlet的doGet方法的输出:";
  //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("<h1><p>"+clientName+":您好</h1>");
  out.print("</BODY></HTML>");


  out.close();
 }
}

在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 through URL:
Note:
Implement the service method. 
    The main function of Servlet is to accept HTTP requests (requests) sent from browsers and return HTTP responses (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 methods,
    then the servlet can only respond to POST or GET. If the developer wants to handle all types of requests, he
    can simply implement the service method (but if he chooses to implement the service method, he does not have to implement the doPost or doGet methods unless super.service() is called at the beginning of the service method) .

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325949599&siteId=291194637