Talk about the Servlet

Servlet is a small storage on the server side, it must rely on the server in order to run for the bridge between pages and databases. The main function is to interactively browse and generate data, generate dynamic Web content. The main role of this paper to explain the servlet, and other content related objects

A, servlet execution flow

  Appreciated servlet execution process is understood process browser requests and server responses, the browser makes a request, the server receives the request and creates servlet objects, the request to the servlet processing, after processing the results returned to the browser, the specific steps:

Two, HttpServletResponse Interface

 This interface is used in response to the encapsulated servlet process, the response is divided into the response header, in response to the line, a blank line in response, response body. Response row contains the protocol version, status codes and status information.

  1, the setting information in a response line:. SetStatus (int sc) The method for setting the response status code, e.g. Response setStatus (404), when the servlet is executed regardless of whether there are other page code, jump eventually to 404 pages (resource not found), see the response information pages, you will find 404 response behavior, of course, there are other state code, but this method is generally used to set the status code to successfully jump

  2, SendError (int I, String str); The method used to set the response in the response line specific information.

  Response head is provided with two major categories Method: In the beginning of the addHeader add (), addIntHeader () and the like; setHeader beginning to set (), setIntHeader () and the like, the difference between them is that the method can add Multi assignment of a property, does not cover, and set methods will be covered, the value of a final display value. This is one of the more commonly used attributes Accept-Ranges, suitable for downloading to tell whether the browser supports breakpoints download. There are common attributes as follows:

Three, HttpServletRequest Interface

  Response is the opposite, the request is also a request line, a request header, the request blank line, the request body

  

Four, ServletContext objects

  ServletContext object represents a web application, each web application, there is one and only one ServletContext object

  Get There ServletContext object in two ways: by acquiring ServletConfig interface, which provides a method GetServletContext (); parent acquired by inheritance class HttpServlet GenericServlet, GenericServlet class provides methods GetServletContext ();

  ServletContext object function: 1, as ServletContext domain objects, storing data;

                2, to obtain application initialization parameters

                3, to obtain the absolute path to the application of any resource web

Five, ServletConfig objects

  It is the servlet configuration object, and each object has a ServletConfig servlet object in the servlet configuration file may be used <init-param> tag to be initialized to s Servlet

  Principle: After initialization servlet configuration parameters, web container when creating a servlet instance objects, they are automatically ServletConfig initialization parameters encapsulated object, and the init method of the servlet is invoked, the object is passed to ServletConfig Servlet. Therefore, the initialization parameter information can be obtained by this Servlet servlet objects.

  By the object,

    We can get servlet name: getServletName ();

    Get the Servlet initialization parameters, String getInitParameter ( "key name");

    Get ServletContext object, getServletContext () returns the Servlet context object representing the current web application project

Six, servlet forwarding and redirection

  Forwarding and Redirection in fact belong to jump, jump to the next page, but these two methods are different.

  First, the action of these two different behaviors of the sender, forwarding is performed by the server, and the redirection is performed by the browser.

  Secondly, forwarding the browser to send only one request, the request header address is always the same; and redirect is more than twice or browser sends a request, and the address change will happen.

  Redirect need to carry parameters applicable to the jump, for example, log on after the jump to the home page (the address has changed this time, an address to the login page address registration page) ; to carry forward the applications that require parameters case, for example, failed to register and then jumps back to the registration page (At this address has not changed)

  重定向:response.sendRedirect(request.getContextPath()+"/html/login.jsp");

  转发:request.getRequestDispatcher("/html/register").forward(request, response);

Seven difference, ServletContext and the ServletConfig

  1, ServletConfig scope only their own that a Servlet, and ServletContext role innings is all Servlet

  2, ServletConfig only get their own initialization parameters, and can get ServletContext parameters, but the parameters and ServletConfig is not the same, and this parameter is a full Servlet shared.

  3, ServletConfig scope too small because the set value, and can set the value ServletContext customized for global use.

 

Guess you like

Origin www.cnblogs.com/WAXX-JML/p/11865384.html