catalogue
1, Servlet core interfaces and classes
1.2 GenericServlet abstract class
1.3. HttpServlet class (recommended)
2, Two configurations of servlet s
2.1 configuring web XML (used before servlet 2.5)
2.2 # using annotations (supported after servlet 3.0, recommended)
1, Servlet core interfaces and classes
In the Servlet architecture, in addition to implementing the Servlet interface, it can also be written by inheriting the GenericServlet or HttpServlet class.
1.1 Servlet interface
In the Servlet API, the most important is the Servlet interface. All servlets will directly or indirectly contact with the interface, or directly implement the interface, or indirectly inherit from the class that implements the interface.
The interface includes the following five methods:
init(ServletConfig config)
ServletConfig getServletConfig()
service(ServletRequest req,ServletResponse res)
String getServletInfo()
destroy()
1.2 GenericServlet abstract class
GenericServlet makes writing servlets easier. It provides a simple implementation of the life cycle methods init and destroy. To write a general Servlet, you only need to rewrite the abstract Servlet method
1. Abstract class source code:
service is an abstract method, and subclasses must be overridden
2. Inheritance abstract class writing method:
package com.ha.servlet; import javax.servlet.GenericServlet; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import java.io.IOException; public class GenServlet extends GenericServlet { public void service(ServletRequest ServletRequest, ServletResponse ServletRespense) throws IOException { System.out.println("hello world"); } }
3. Add two lines to the configuration file:
4. Run
1.3. HttpServlet class (recommended)
httpServlet is a further extension of GenericServlet.
Provides an abstract class that will be subclassed to create an HTTP servlet for a web site. Subclasses of HttpServlet must override at least one method, which is usually one of the following methods:
doGet, if the servlet supports HTTP GET requests
doPost, used for HTTP POST requests
doPut, used for HTTP PUT requests
doDelete, used for HTTP DELETE requests
1. Abstract class source code
Abstract classes are rewritten to give different processing methods and make different requests
2. Code
Because there is Chinese, this sentence should be added when compiling Java on the cmd command line - encoding utf8
package com.ha.servlet; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class HttpsServlet extends HttpServlet { protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException{ System.out.println("hello HttpsServlet"); } /* * HttpsServlet All service requests are required to be related to the Http protocol, * What are the differences between the following ordinary service and the above overload * 1, Different modifiers * public It is provided by the interface * protected It is overloaded by HttpServlet * 2, The parameter list is different * HttpServletRequest,HttpServletResponse * ServletRequest,ServletResponse */ /* public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { } */ }
3. Add two lines to the configuration file:
4. Run
5. Characteristics of HttpServlet
Process different results according to different request methods
package com.ha.servlet; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import java.io.IOException; public class HttpsServlet extends HttpServlet { protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("This is get Requested content"); } protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("This is post Requested content"); } }
The browser access method is get,
1.4 notes
The names in the configuration file cannot be the same, otherwise tomcat will report an error
Address configuration error (com.ha.servlet...) Or 404 error will also be reported when the URL (... / hss) entered during access is wrong
2, Two configurations of servlet s
2.1 configuring web XML (used before servlet 2.5)
1. Disposition
<?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" metadata-complete="true"> <!--1,add to servlet node--> <servlet> <servlet-name>my</servlet-name> <servlet-class>com.ha.servlet.myServet</servlet-class> <!--To be executed class Add the package name to the package--> </servlet> <!--2,add to servlet-mapping node--> <servlet-mapping> <servlet-name>my</servlet-name> <!--And servlet Medium servlet-name Is consistent--> <url-pattern>/servlet</url-pattern> <!--Enter this in the browser to indicate access servlet-name Corresponding servlet Medium myServlet--> </servlet-mapping> <servlet> <servlet-name>gs</servlet-name> <servlet-class>com.ha.servlet.GenServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>gs</servlet-name> <url-pattern>/gs</url-pattern> </servlet-mapping> <servlet> <servlet-name>hs</servlet-name> <servlet-class>com.ha.servlet.HttpsServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>hs</servlet-name> <url-pattern>/hs</url-pattern> </servlet-mapping> </web-app>
2. URL pattern defines the matching rule, and the value Description:
Exact match / specific name} the Servlet will be triggered only when the url path is a specific name
Suffix match * xxx as long as it is based on The end of xxx matches the starting Servlet
Wildcard matching / * matches all requests, including all resources of the server
Wildcard match / match all requests, including all resources of the server, excluding jsp
Example 1
Enter in the browser:
http://localhost:8080/myweb/abc.action
Will execute httpsservlet Class this file
Example 2
Enter in the browser:
http://localhost:8080/myweb / [anything]
Will execute httpsservlet Class this file
If the input is precisely accessed content such as gs, execute genservlet Class this file
Example 3
Using wildcard matching /, enter in the browser:
http://localhost:8080/myweb/aaa.jsp
The corresponding will not be executed class file
3,load-on-startup
(1) Whether the element tag container should load the servlet when the web application starts.
(2) Its value must be an integer indicating the order in which the servlet is loaded.
(3) If the value of the element is negative or not set, the container will reload when the Servlet is requested.
(4) If the value is a positive integer or 0, it means that the container loads and initializes the servlet when the application is started. The smaller the value, the higher the priority of the servlet, and the earlier it is loaded. If the values are the same, the container will choose its own order to load.
<?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" metadata-complete="true"> <!--1,add to servlet node--> <servlet> <servlet-name>my</servlet-name> <servlet-class>com.ha.servlet.myServet</servlet-class> <!--To be executed class Add the package name to the package--> <load-on-startup>0</load-on-startup> </servlet> <!--2,add to servlet-mapping node--> <servlet-mapping> <servlet-name>my</servlet-name> <!--And servlet Medium servlet-name Is consistent--> <url-pattern>/servlet</url-pattern> <!--Enter this in the browser to indicate access servlet-name Corresponding servlet Medium myServlet--> </servlet-mapping> <servlet> <servlet-name>gs</servlet-name> <servlet-class>com.ha.servlet.GenServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>gs</servlet-name> <url-pattern>/gs</url-pattern> </servlet-mapping> <servlet> <servlet-name>hs</servlet-name> <servlet-class>com.ha.servlet.HttpsServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <servlet-name>hs</servlet-name> <url-pattern>/hs</url-pattern> </servlet-mapping> </web-app>
Operation results:
Note: this function is executed only once
public void init(ServletConfig servletConfig) throws ServletException { System.out.println("GenServet init"); }