1. servlet definition: A java class that runs on the server side.
2. servle life cycle
<1> WEB container creates Servlet instance
<2> Call the init method of the Servlet
<3> Call the service method of the servlet multiple times (that is, the servlet is a singleton)
<4> When the WEB container is closed, the destroy method of the servlet is called to destroy the servlet
3. Servlet configuration
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> <context-param> <param-name>test2</param-name> <param-value>myServlet2</param-value> </context-param> <servlet> <servlet-name>MyServlet</servlet-name> <servlet-class>com.wr.servlet.MyServlet</servlet-class> <init-param> <param-name>test</param-name> <param-value>myServlet</param-value> </init-param> <load-on-startup>0</load-on-startup> </servlet> <servlet-mapping> <servlet-name>MyServlet</servlet-name> <url-pattern>/MyServlet</url-pattern> </servlet-mapping> </web-app>
load-on-startup: configure how servlets are loaded
<1> When the value is negative, a servlet instance is created when the current servlet is accessed
<2> When the value is a positive number, a servlet instance will be created when the WEB application is loaded. The smaller the value, the higher the priority, and it will be created first.
package com.wr.servlet; import java.io.IOException; import javax.servlet.Servlet; import javax.servlet.ServletConfig; import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRequest; import javax.servlet.ServletResponse; /** * Servlet implementation class MyServlet */ public class MyServlet implements Servlet{ public MyServlet() { System.out.println("MyServlet.MyServlet()"); } public void init(ServletConfig config) throws ServletException { // TODO Auto-generated method stub System.out.println("MyServlet.init()"); String param = config.getInitParameter("test"); System.out.println(param); ServletContext context = config.getServletContext(); System.out.println(context.getInitParameter("test2")); } public ServletConfig getServletConfig() { // TODO Auto-generated method stub System.out.println("MyServlet.getServletConfig()"); return null; } public void service(ServletRequest req, ServletResponse res) throws ServletException, IOException { System.out.println("MyServlet.service()"); // TODO Auto-generated method stub } public String getServletInfo() { System.out.println("MyServlet.getServletInfo()"); // TODO Auto-generated method stub return null; } public void destroy() { System.out.println("MyServlet.destroy()"); // TODO Auto-generated method stub } }
ServletConfig: The configuration object of the current servlet, you can read the initialization parameters configured in the servlet
ServletContext: The context object of the current WEB application, which can read the initialization parameters configured in the WEB configuration file
4. Request forwarding and redirection
// redirect //response.sendRedirect("first.jsp"); // request forwarding request.getRequestDispatcher("first.jsp").forward(request, response);
the difference:
1. The address bar is different
The redirect address bar will change, the forward will not.
2. The request object is different
After redirection, the request is not the same, and the forwarding is the same.
3. "/" means different meanings
When redirecting, "/" represents the root directory of the site, and forwarding represents the root directory of the current WEB application.
4. Forwarding can only forward WEB internal resources, not external resources.
Redirection can be redirected to any resource. (For example: cannot be forwarded to www.baidu.com, only redirected)