JavaWeb: Servlet Technology

JavaWeb: Servlet Technology

quick start

what is servlet

  A Java servlet is a program that runs on a Web server or application server and acts as an intermediate layer between requests from a Web browser or other HTTP client and a database or application on the HTTP server . Using servlets, you can collect user input from web forms, render records from databases or other sources, and dynamically create web pages.

  The location of the servlet in the web application:

  

Servlet tasks

Servlets perform the following main tasks:

  • Read explicit data sent by the client (browser) . This includes HTML forms on web pages, or forms from applets or custom HTTP client programs.
  • Read the implicit HTTP request data sent by the client (browser). This includes cookies, media types and compression formats that browsers understand, among others.
  • Process data and generate results . This process may require accessing a database, performing an RMI or CORBA call, invoking a Web service, or directly computing the corresponding response.
  • Send explicit data (ie documents) to the client (browser) . The document can be in a variety of formats, including text files (HTML or XML), binary files (GIF images), Excel, and more.
  • Send an implicit HTTP response to the client (browser). This includes telling the browser or other client what type of document to return (eg HTML), setting cookies and caching parameters, and other similar tasks.

Servlet environment configuration

Download Tomcat server

Official website address: https://tomcat.apache.org/download-80.cgi

  

Add the web development package to the environment variable

  

Create our first servlet

We inherit HTTPServlet and implement our own Servlet:

A special class:

ServletContext: Servlet context object, Servlet accesses various supports provided by the container for the current Web application by accessing this object.

Servlet declaration cycle

  

  • The first HTTP request to the server is delegated to the servlet container.
  • The servlet container loads the servlet before calling the service() method.
  • The servlet container then handles multiple requests generated by multiple threads, each executing the service() method of a single servlet instance.
  • init: responsible for initializing the servlet object, the container will call this method after the servlet object is created.
    •   The init method is designed to be called only once . It is called when the servlet is first created, and is not called on each subsequent user request .
    •   A servlet is created the first time a user calls the URL corresponding to the servlet, but you can also specify that the servlet is loaded the first time the server starts.
    •   When a user invokes a servlet, a servlet instance is created, and each user request generates a new thread, which is handed over to the doGet or doPost method as appropriate. The init() method simply creates or loads some data that will be used throughout the life cycle of the servlet.
  • Destory: responsible for releasing the resources occupied by the servlet object. When the servlet object ends its life cycle, the container will call this method.
    •   The destroy() method will only be called once, at the end of the servlet life cycle. The destroy() method allows your servlet to close database connections, stop background threads, write a cookie list or hit counter to disk, and perform other similar cleanup activities.

The relationship between Servlet and Tomcat

 

Directory structure of a web application

 

 

Guess you like

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