Servlet overview
- A servlet is a small Java program (that is, a server-side applet) running in a Web server. Servlets typically receive and respond to requests from Web clients over HTTP (Hypertext Transfer Protocol).
- Preparation process:
1. Write a java class to implement the servlet interface
2. Modify the web.xml file to provide the servlet with an accessible URI address
<!--Create a servlet Example--> <servlet> <servlet-name>servletdemo1</servlet-name> <servlet-class>day08_00_servlet.servletdemo</servlet-class> </servlet> <!--to servlet Provide a url address--> <servlet-mapping> <servlet-name>servletdemo1</servlet-name> <url-pattern>/demo1</url-pattern> </servlet-mapping> <!--among/demo1 Equals http://localhost:8080/.../demo1,If not'/',Will become http://localhost:8080/demo1-->
- Deploy application to tomcat server
- test
servlet life cycle
- Construct the servlet and initialize it using the init method
- Handles all calls to the service method from the client
- Take the servlet out of the service, destroy it with destroy method, recycle it and terminate it
- Instantiation initialization service destroy
- ps: how to create a servlet when the server starts:
<servlet> <servlet-name>servletdemo1</servlet-name> <servlet-class>day08_00_servlet.servletdemo</servlet-class> <load-on-startup>2</load-on-startup> </servlet>
Three ways to create a Servlet
- Implement the javax.servlet.Servlet interface (the first is the method shown above)
- Inherit javax.servlet.GenericServlet class (adapter mode)
- Inherit javax.servlet.http.HttpServlet class (template design pattern) (commonly used in development) (do not override the service method of the parent class)
public class ServletDemo3 extends HttpServlet{ @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("*******doGet *******"); System.out.println(req.getRemoteAddr()); } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { System.out.println("**********doPost**********"); } }
Servlet mapping:
- You can configure multiple mapping paths:
<servlet-mapping> <servlet-name>servletdemo1</servlet-name> <url-pattern>/demo1</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>servletdemo1</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name>servletdemo1</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping>
- Wildcard * can represent any string
<!--/* Any string can be accessed--> <servlet-mapping> <servlet-name>servletdemo1</servlet-name> <url-pattern> /* </url-pattern> </servlet-mapping> <!--with*.String requests can be accessed, but do not add/--> <servlet-mapping> <servlet-name>servletdemo1</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!--with /action The first requests are accessible--> <servlet-mapping> <servlet-name>servletdemo1</servlet-name> <url-pattern>/action/*</url-pattern> </servlet-mapping>
- Match rule: priority: absolute match > / start match > extension method match
Thread safety of Servlet
Single instance: multiple threads per access
Problem: the following code example like this, because it is multithreaded, will have the same number, resulting in inaccurate data
public class ServletDemo3 extends HttpServlet { int num = 1; protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { num++; System.out.println(num); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
Solution: do not write global variables, but local variables
public class ServletDemo3 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { int num = 1; num++; System.out.println(num); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
Servlet getting configuration information
Using ServletConfig
- Method 1:
public class ServletDemo3 extends HttpServlet { //1. Define local variables private ServletConfig config; public void init(ServletConfig config) throws ServletException { //2. Get the config of the parent class this.config = config; } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //3. Get by getInitParameter method String name = config.getInitParameter(""); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
- Mode two:
public class ServletDemo3 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { //Obtained by using the method of inheriting the parent class String name = this.getServletConfig().getInitParameter(""); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
- Mode three:
public class ServletDemo3 extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String name = this.getInitParameter(""); } protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
ServletContext(*)
- Overview: ServletContext: represents the whole application. An application has only one ServletContext object, single instance.
- Effect:
1. Domain object: within a certain range (in the current application), multiple servlets can share data
* 1.1
*1.2 common methods:
*1.2.1 void setAttribute (string name, object value); / / add data to the map attribute of the ServletContext object
*1.2.2 Object getAttribute(String name); / / get data from the map of ServletContext object
*1.2.3 void rmoveAttribute(String name); / / remove data according to name
2. Global configuration information can be obtained
*1. String getInitParameter(String name) / / get value according to the key in the configuration file
3. Access to resources
*String getRealPath(String path); / / get the absolute path of the resource according to the resource name
4. It can realize Servlet forwarding
//Implement request forwarding ServletContext sc = this.getServletContext(); RequestDispatcher rd = sc.getRequestDispatcher(String path);//path means to jump there rd.forward(request,response);