The basic principle of the servlet

    Sun's servlet is to develop extended functionality of the web server component specifications for: the application is running on the server side, and serve under HTTP, in charge of the application process client and server side. Here I need to introduce two concepts, containers and components. First of all I would like to introduce what are the components: Component is compliant, specific functions, and can be deployed on the container software modules, simply say that we write servlet components. Once you have the component is what I come to introduce container: container is compliant, provides operating environment for components and management software program component life cycle common to have a tomcat, webLogic.

    Develop a Servlet:

    We can create a Servlet in three ways. 1. To achieve Servlet interface, 2. abstract class inheritance GenericServlet, 3. Inherit the HttpServlet class. Since we usually develop web applications are HTTP-based Servlet is written so we usually go to inherit HttpServlet, which is overridden method. After you create a Servlet we need to put our written Servlet configuration in web.xml.

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

  <display-name>Archetype Created Web Application</display-name>
  <servlet>
    <servlet-name>testServlet</servlet-name>
    <servlet-class>example.servlet.TestServlet</servlet-class>
  </servlet>
  <servlet-mapping>
    <servlet-name>testServlet</servlet-name>
    <url-pattern>/test</url-pattern>
  </servlet-mapping>
</web-app>

After we put the whole project to compile, publish deployed to tomcat directory, start tomcat, visit Servlet, just type in your browser http: // ip: port / appname / web.xml in the servlet url-pattern corresponding to the configuration can be.

    Process Servlet Web applications

    When the user wants the browser to access the servlet input address: a first browser via ip:. Port connected to the server, then the browser will request data in accordance with standard packages into a HTTP protocol packet sent to the server (tcp three-way handshake protocol). 2. Server (tomcat) creates two objects: the object request and response objects. Server parses the data request packet will request into a request object to help us in the servlet. (Note:. Servlet not resolve the request, to the container to do this step) object request and response here is actually the interface implementation class HttpServletRequest and HttpServletResponse corresponding. 3. Locate the route request based on the resource corresponding servlet configuration, create an instance of the servlet by reflection, and calls the service () method, and the object is to create a good prior request and response as a parameter passed to the service. 4. Write our own business logic in a servlet, and data through the browser's response to the response setting. (Need to develop our own) 5. Response from the server to obtain our written response to the data, package them into a data packet is sent to the browser (response packet) according to the HTTP protocol, closes the connection (tcp waved off four times ) packets (6) of the browser parses the response, data retrieval, and rendering the page. Throughout the process, we only need to write business logic in the servlet process 4, the other by the tomcat container and browser package.

Guess you like

Origin www.cnblogs.com/suyang-java/p/11183699.html