1, What is a Servlet
-
servlet is one of the Java EE specifications, which is the interface
-
Servlet is one of the three major components of Java web. The three major components are: servlet program, filter and Listener listener.
-
Servlet is a Java applet running on the server. It can receive the request sent by the client and respond to the data to the client.
2, Manually implement Servlet program
-
Write a class to implement the Servlet interface
-
Implement the service method, process the request and respond to the data
-
To the web Configure the access address of servlet program in xml (the following is the configuration and meaning in xml)
<!-- servlet Label to Tomcat to configure Servlet program --> <servlet> <!-- servlet-name label servlet The program has an alias (usually the class name)--> <servlet-name>first</servlet-name> <!-- servlet-class yes Servlet Full class name of the program --> <servlet-class>com.example.FirstServlet.HelloServlet</servlet-class> </servlet> <!-- servlet-mapping Label to servlet Program configuration access address --> <servlet-mapping> <!-- servlet-name The function of the tag is to tell the server to which my current address is given Servlet Program use--> <servlet-name>first</servlet-name> <!-- url-pattern Label configuration access address / When the server resolves, the address is: http://ip:port / Project path /first Indicates that the address is: http://ip:port / Project path / hello -- > <url-pattern>/first</url-pattern> </servlet-mapping>
How can the browser find the corresponding resources through the address:
[the external chain picture transfer fails. The source station may have an anti-theft chain mechanism. It is recommended to save the picture and upload it directly (img-defalni3-1645100178216)( https://note.youdao.com/yws/res/5782/WEBRESOURCE6da00204cc61d0746aaced1128c096a6 )]
3, Life cycle of Servlet
- Execute Servlet constructor method
- Execute init method
Step 1 and step 2: during the first access, the Servlet program will call (by default)
On the web The < load on startup > tag is configured in the servlet tag in XML. When the tag value is positive, it is preloaded, the one with larger value is loaded first, and the one with negative tag value is delayed loading. The default value is - 1.
- Execute service method
Step 3: every access will call
- Execute destroy method
Step 4: call when the web project stops.
4, Three implementation methods of servlet
-
To implement the servlet interface, you must rewrite five methods
public class Test implements Servlet { @Override public void init(ServletConfig servletConfig) throws ServletException { } @Override public ServletConfig getServletConfig() { return null; } @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { } @Override public String getServletInfo() { return null; } @Override public void destroy() { } }
-
To inherit the genericServlet abstract class, you must override the service method
public class Test extends GenericServlet { @Override public void service(ServletRequest servletRequest, ServletResponse servletResponse) throws ServletException, IOException { } }
-
Inherit HttpServlet without rewriting any methods. Write doGet() and doPost() methods as needed
public class Test extends HttpServlet{ }
tip: the relationship between the three. GenericServlet implements the Servlet interface, and HttpServlet inherits the GenericServlet class.
5, ServletConfig class
Both the Servlet program and the ServletConfig object are created by tomcat,
-
Three functions of ServletConfig class
-
Gets the value of the alias Servlet name of the Servlet program
servletConfig.getServletName();
String servletName = servletConfig.getServletName(); System.out.println("servlet Alias for:"+servletName);
-
Get the initialization parameter init param. You can only get your own parameters
servletConfig.getInitParmeter("username");
String age = servletConfig.getInitParameter("age"); System.out.println(age);
-
Get ServletContext object
servletConfig.getServletContext();
-
-
ServletContext class
-
What is a ServletContext object
- Interface, representing Servlet context object
- A web project has only one ServletContext object instance
- The ServletContext object is a domain object
What is a domain object?
Domain objects, like maps, can access a number of objects
The domain here refers to the operation range of accessing data
-
Four functions of ServletContext class
-
Get web Configure the context parameter context param in XML
//Get global parameters ServletContext context = getServletConfig().getServletContext(); String username = context.getInitParameter("username");
-
Get the current project path, format: / Project path
//Get current project path ServletContext context = getServletConfig().getServletContext(); System.out.println(context.getContextPath());
-
Get the absolute path on the server hard disk after project deployment
//Get the path of project deployment /** * / The address of the inclined rod resolved by the server is http://ip:port/ Project name/ */ ServletContext context = getServletConfig().getServletContext(); System.out.println(context.getRealPath("/"));
-
Access data like a Map. When the project is destroyed, the data disappears.
//Get servletContext object ServletContext servletContext = getServletContext(); servletContext.setAttribute("key1","value1"); System.out.println(servletContext.getAttribute("key1"));
-
-