Home php教程 PHP开发 servlet summary

servlet summary

Dec 01, 2016 pm 01:20 PM
servlet

ervlet summary

1. Servlet is responsible for processing user requests on the server side. When the client sends a request to the server, the server finds the corresponding Servlet through the web.xml configuration file to process the request. A servlet must be bound to a URL, and users access the servlet by accessing the URL.



2. Three ways to implement servlet

Java.servlet.Sertvlet class

Java.servlet.GenericServlet class

Java.servlet.HttpServlet class


3.serv in let interface Method

//Initialization

void init (ServletConfig config)

void service (ServletRequest request, ServletResponse response)

//Destruction

void destroy()


String getServletInfo()

ServletConfig getServletConfig ()

The client sends a request, and the server will find the corresponding Servlet through the URL. If the Servlet object does not exist, the server will create the Servlet object

After creating the object, the server immediately calls init (ServletConfig config) to complete the initialization work. The server calls the Servlet's service (ServletRequest req, ServletResponse res) method. At this time, it needs to pass the parameters request and response to it.

When the client accesses this Servlet again, because the Servlet object already exists, it will not be created. Servlet is a singleton.

(Note: The singleton pattern is a commonly used software design pattern. Its core structure only contains a special class called a singleton class. The singleton pattern can ensure that there is only one instance of a class in the system and This instance is easy to access from the outside, which facilitates control of the number of instances and saves system resources. If you want only one object of a certain class to exist in the system, the singleton mode is the best solution)

When the server needs it. When destroying the Servlet object, destroy() will first be called to complete the release of some resources, and then destroy the object

4. Configure the servlet in web.

cn.edu.aynu.rjxy.servlet.HelloWorldServlet

HelloWorldServlet


/helloWorld

< ;/servlet-mapping>

The configuration is to bind the URL and Servlet together. When the user accesses, the corresponding Servlet will be found to complete the request processing.

5.ServletConfig: The parameter of the init() method, which represents the Servlet configuration object and its corresponding Servlet configuration information.

In fact, the configuration information is the element in the web.xml file.

;/ servlet-class>

ServletConfig object is created by the server. When the server calls the init() method, it is passed to the method init() as an actual parameter.

We can use the init() method use it in.

(1) String getInitParameter(String name): Used to obtain the initialization parameters configured in web.xml and obtain the parameter value through the parameter name.

(2) Enumeration getInitParameterNames(): Used to obtain all initialization parameter names configured in web.xml.

(3) ServletContext getServletContext(): used to return the ServletContext object

(4) String getServletName(): used to return the name of the Servlet configured in web.xml, that is, the name specified by the element

                                                                           ;init -param>

                                                                                            ;

                                                                                                                                      ,,,,, Complete initialization immediately

school=aysfxy

major=java

class1=2

school=aysfxy

class1=2

major=java

school=aysfxy

major=java

class1=2

school=aysfxy

class1=2

major=java

6. GenericServlet: It is an abstract class that implements the javax.servlet.Servlet interface. Inheriting this class can also implement Servlet.

The getServletContext(), getInitParameter() and other methods in the class can be used directly. If you want to do some initialization parameter work,

Don’t override the method init (ServletConfig config), but override the method init()

HttpServlet: It is a Servlet class specially used to handle Http protocol requests. We can implement Servlet by inheriting HttpServlet

This is the simplest way. We will do this in the future, because the request sent by the client only has get and post. So we only need to rewrite these two methods.


7.HttpServlet: It is a Servlet class specially used to handle Http protocol requests. We can implement Servlet by inheriting HttpServlet

This is the simplest way, we will do this in the future, because the client There are only two methods of sending requests: get and post. So we only need to rewrite these two methods.

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1662
14
PHP Tutorial
1262
29
C# Tutorial
1235
24
The servlet life cycle is divided into several stages The servlet life cycle is divided into several stages Feb 23, 2023 pm 01:46 PM

The Servlet life cycle refers to the entire process from creation to destruction of a servlet, which can be divided into three stages: 1. Initialization stage, calling the init() method to initialize the Servlet; 2. Running stage (processing requests), the container will Request to create a ServletRequest object representing an HTTP request and a ServletResponse object representing an HTTP response, and then pass them as parameters to the service() method of the Servlet; 3. Destruction phase.

What is a servlet What is a servlet Jan 28, 2023 am 09:51 AM

The full name of Servlet is "Java Servlet", which means small service program or service connector in Chinese. It is a program running on a Web server or application server. It serves as a request from a Web browser or other HTTP client and a database on the HTTP server or The middle layer between applications. Servlet has the characteristics of being independent of platform and protocol. Its main function is to browse and generate data interactively and generate dynamic Web content.

How does Java Servlet implement distributed session management? How does Java Servlet implement distributed session management? Apr 16, 2024 pm 02:48 PM

There are two ways to implement distributed session management in JavaServlet: 1. Session replication: Copy session data to each server. 2. Session distribution: Use a centralized storage service to store session data and access it from multiple servers. The specific implementation methods are: session replication configures true in the web. session data.

What are the application scenarios of Java Servlet? What are the application scenarios of Java Servlet? Apr 17, 2024 am 08:21 AM

JavaServlet can be used for: 1. Dynamic content generation; 2. Data access and processing; 3. Form processing; 4. File upload; 5. Session management; 6. Filter. Example: Create a FormSubmitServlet to handle form submission, taking name and email as parameters, and redirecting to success.jsp.

Java technology stack for web development: Understand Java EE, Servlet, JSP, Spring and other technologies commonly used in web development Java technology stack for web development: Understand Java EE, Servlet, JSP, Spring and other technologies commonly used in web development Dec 26, 2023 pm 02:29 PM

JavaWeb development technology stack: Master JavaEE, Servlet, JSP, Spring and other technologies used for Web development. With the rapid development of the Internet, in today's software development field, the development of Web applications has become a very important technical requirement. As a widely used programming language, Java also plays an important role in the field of Web development. The JavaWeb development technology stack involves multiple technologies, such as JavaEE, Servlet, JSP, Spr

Servlet Container Revealed: A Deeper Understanding of the Servlet Runtime Environment Servlet Container Revealed: A Deeper Understanding of the Servlet Runtime Environment Feb 19, 2024 pm 01:00 PM

The Servlet container is an application that provides the Servlet running environment. It is responsible for managing the life cycle of the Servlet and providing necessary WEB services, such as security, transactions, etc. There are many types of Servlet containers, the most common of which are Tomcat and Jetty. The main functions of the Servlet container are life cycle management: The Servlet container is responsible for managing the life cycle of the Servlet, including startup, initialization, service and destruction. Web services: The Servlet container provides web services, such as security, transactions, etc. Resource management: Servlet container manages resources, such as Servlet, jsP, html pages, etc. Class loading: The Servlet container is responsible for adding

Java Errors: Servlet Errors, How to Fix and Avoid Java Errors: Servlet Errors, How to Fix and Avoid Jun 25, 2023 pm 06:34 PM

Servlet is a very commonly used technology in Java Web application development. However, some Servlet errors will inevitably occur during the development process. How to solve and avoid Servlet errors has become a top issue for many Java developers. This article will introduce some common Servlet errors and their solutions based on personal experience and related information. ClassNotFoundException When we try to load a class, if the class does not exist or cannot be accessed by the system,

HttpSession interface in Servlet HttpSession interface in Servlet Sep 02, 2023 am 10:05 AM

In the world of Java Web development, understanding the HttpSession interface is key to creating dynamic and responsive web applications. In this article, we will explore what the HttpSession interface is, how it works, and why it plays a crucial role in the Servlet specification. What is the HttpSession interface? At its core, the HttpSession interface is a fundamental component of the JavaServlet API, which enables web developers to track a user's session across multiple HTTP requests. When a user accesses a web application for the first time, a unique session is created to represent their interaction. This session allows the application to maintain state between requests and remember information about

See all articles