Servlet learning notes
Servlet is actually a small Java program running on the web server, which is used to process requests sent from the web client and respond to requests.
use
The Servlet interface is used as a starter:
- Implement Servlet interface
- Configuring implementation classes in web.xml
HttpServlet class is generally used in actual programming:
Learning reference: https://www.cnblogs.com/libingbin/p/5960456.html
At the beginning of SUN's design, it was ambitious. In the future, the Internet will not only use HTTP protocol, but can be realized through GenericSrvlet. HttpServlet is a protocol related Servlet, which is specially used to process HTTP protocol requests. Usually, writing a Servlet will make the Servlet inherit Httpervlet and rewrite the service method.
In the service method, different doXXX methods are executed according to different request methods (the doGet method is executed for get requests, and the doPost method is executed for post requests).
Therefore, after inheriting HttpServlet, you do not need to rewrite the service method, but only need to rewrite the doGet and doPost methods. Often, the code requesting the content to be processed is consistent, so it is necessary to make doGet and doPost call each other to simplify programming.
Learning reference: https://www.cnblogs.com/isme-zjh/p/11820298.html
life cycle
Introduction to English documents:
This interface defines methods to initialize a servlet, to service requests, and to remove a servlet from the server. These are known as life-cycle methods and are called in the following sequence:
- The servlet is constructed, then initialized with the init method.
- Any calls from clients to the service method are handled.
- The servlet is taken out of service, then destroyed with the destroy method, then garbage collected and finalized.
ServletConfig object
The object used to obtain the relevant configuration of the Servlet
Code demonstration:
web.xml configuration
<servlet> <servlet-name>ServletConfig</servlet-name> <servlet-class>servlet.demo.ServletConfig</servlet-class> <init-param> <param-name>username</param-name> <param-value>root</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>ServletConfig</servlet-name> <url-pattern>/config</url-pattern> </servlet-mapping>
Classes that inherit HTTPServlet:
public class ServletConfig extends HttpServlet { @Override public void init(jakarta.servlet.ServletConfig config) throws ServletException { super.init(config); System.out.println("initialization"); System.out.println("Program alias:"+config.getServletName()); System.out.println("Initialization parameters username Value of:"+config.getInitParameter("username")); } @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
Or write it directly
@WebServlet(name = "Servlet", urlPatterns = "/config", initParams = { @WebInitParam(name = "username", value = "root") } ) public class ServletConfig extends HttpServlet { @Override public void init(jakarta.servlet.ServletConfig config) throws ServletException { super.init(config); System.out.println("initialization"); System.out.println("Program alias:"+config.getServletName()); System.out.println("Initialization parameters username Value of:"+config.getInitParameter("username")); } @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { } }
ServletContext object
Get web project information
ServletContext: the context object of the Servlet. The ServletContext object controls the contents before and after the Servlet
We all know. This object - only one web project. Create one for each web project when the server starts
A separate ServletContext object.
public class ServletContextDemo extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Gets the MIME type of the file ServletContext servletContext = this.getServletContext(); String mimeType = servletContext.getMimeType("aa.txt"); System.out.println(mimeType); //Get the project name of the requested path String path = servletContext.getContextPath(); System.out.println(path); //Get global initialization parameters String username = servletContext.getInitParameter("username"); String password = servletContext.getInitParameter("password"); System.out.println(username+" "+password); Enumeration<String> names = servletContext.getInitParameterNames(); while (names.hasMoreElements()) { String name = names.nextElement(); String value = servletContext.getInitParameter(name); System.out.println(name + " " + value); } } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
web.xml configuration
Note that the < context > tag is written outside the < servlet >
<context-param> <param-name>username</param-name> <param-value>root</param-value> </context-param> <context-param> <param-name>password</param-name> <param-value>123123</param-value> </context-param> <servlet> <servlet-name>ServletContext</servlet-name> <servlet-class>servlet.demo.ServletContextDemo</servlet-class> </servlet> <servlet-mapping> <servlet-name>ServletContext</servlet-name> <url-pattern>/context</url-pattern> </servlet-mapping>
Read the file under the web project
Previously, you can read files (in java projects) using the I0 stream. Now it is a web project. The web project can only be accessed by publishing to tomcat. If the traditional 10 is used to obtain the files under the web project, there will be a problem (reason: the path uses a relative path, and the relative path is the JRE environment).
@WebServlet(name = "Servlet", urlPatterns = "/context") public class ServletDemo extends HttpServlet { @Override public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { Properties properties = new Properties(); //Create an input stream for a file InputStream is = this.getServletContext().getResourceAsStream("/WEB-INF/classes/servlet/demo/db.properties"); // String path = this.getServletContext().getRealPath("/WEB-INF/classes/servlet/demo/db.properties"); // System.out.println(path); // InputStream is = new FileInputStream(path); properties.load(is); //get data String driverClassName = properties.getProperty("driverClassName"); String url = properties.getProperty("url"); String username = properties.getProperty("username"); String password = properties.getProperty("password"); System.out.println(driverClassName); System.out.println(url); System.out.println(username); System.out.println(password); } @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request,response); } }
Domain object:
Domain object: refers to storing data into a domain object, and the data will have a certain scope. Domain refers to a certain scope.
ServletContext is to create a separate ServletContext object for each web project when the server starts. When a web project is removed from the server or the server is shut down, the ServletContext object is destroyed. The data saved to the ServletContext always exists (when the server is shut down, the SenvletContext object is destroyed, and then the data in it will become invalid). Scope: entire web application.