Detailed description of Servlet in javaWeb, javawebservlet

Source: Internet
Author: User

Detailed description of Servlet in javaWeb, javawebservlet
Servlet details

1. servlet Introduction

Servlet is one of the three major components of javaweb. It and filter and listener constitute three major components of javaweb. Servlet (Server Applet) is short for Java Servlet, it is interpreted as a java Applet running on the server side,

Role: used to receive client requests, process business logic, and respond to Dynamic Content

2. Use

Create a java class, implement the Servlet interface, and rewrite the abstract method.

1 public class MyServlet implements Servlet {2/** 3 * Servlet execute 4 */5 @ Override 6 public void init (ServletConfig config) during initialization) throws ServletException {7 // TODO Auto-generated method stub 8 9} 10/** 11 * When the request comes, execute 12 */13 @ Override14 public void service (ServletRequest req, servletResponse res) throws ServletException, IOException {15 // TODO Auto-generated method stub16 17} 18/** 19 * execute 20 */21 @ Override22 public void destroy () when the Servlet is destroyed () {23 // TODO Auto-generated method stub24 25} 26 27 @ Override28 public String getServletInfo () {29 // TODO Auto-generated method stub30 return null; 31} 32 @ Override33 public ServletConfig getServletConfig () {34 // TODO Auto-generated method stub35 return null; 36} 37 38}

3. Servlet lifecycle (the Servlet object is a singleton object)

Create: by default, when a request comes, the server will check whether the object of this class has been created. If not, the server will reflect the object of this class, call the init method to complete the initialization, and then call the serivce method to complete the corresponding business logic.

Service: one request is sent once.

Destroy: The Serlvet object will be destroyed when the server is normally shut down or the project is uninstalled.

1/** 2 * Servlet initialization execution time: by default, When you request the server, the server will check whether the Servlet object exists, 4 * If no, it will reflect the creation of this class object, and execute the init method to complete the initialization operation, and then call the 5 * service method to complete the service 6 * execution times: once 7 */8 @ Override 9 public void init (ServletConfig config) throws ServletException {10 // TODO Auto-generated method stub11 12} 13/** 14 * service: 15 * execution time: 16 * execution times per request: 17*18 */19 @ Override20 public void service (ServletRequest req, ServletResponse res) throws ServletException, IOException {21 // TODO Auto-generated method stub22 23} 24/** 25 * destroy 26 * execution time: when the server is closed or the project is uninstalled, will call this method 27 * execution times: 1 28 */29 @ Override30 public void destroy () {31 // TODO Auto-generated method stub32 33}

4. Detailed configuration of Servlet in web. xml

 

Graphic Configuration File Execution Process

 

 

Url-pattern
1. The exact match statement must be followed by a custom/aa/bb/aa/ccc/dddd
2. The directory matching method must be/customized in the middle of the header to end with/*/aa/*/aaa/dd /*
3. The suffix matching method must be *. headers *. jsp *. action *. do

Load-on-startup configuration item
Modify the default servlet initialization time. The servlet will be loaded at server startup.

<Load-on-startup> positive integer </load-on-startup>
Positive Integer: the smaller the value, the higher the priority.

Defaultservlet:
Path/
Matches all resources but has the lowest priority.

You don't care about him.
You cannot manage it.

 

5. Servlet running process

 

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.