Introduction to Servlets
Servlet is a Java applet running on the server side , which is a set of specifications (interfaces) provided by sun company , which is used to process the client's request and respond to the dynamic resources of the browser . The essence of servlet is Java code , which dynamically outputs content to clients through Java API.
servlet specification: contains three technical points
1) servlet technology
2) filter technology---filter
3) listener technology---listener
Servlet Quick Start Steps
1) Create a class to implement the Servlet interface
2) Override methods that have not yet been implemented --- focus on implementing service methods
3) Configure the servler in web.xml
package com.oracle.demo01; 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; public class MyServlet implements Servlet { @Override public void destroy() { System.out.println("dstory is running"); } @Override public void init(ServletConfig arg0) throws ServletException { System.out.println("init is running"); // Get the name of the servlet String servletname = arg0.getServletName(); System.out.println(servletname); // get initialization parameters String value = arg0.getInitParameter("url"); System.out.println(value); // Get the ServletContext object ServletContext sc = arg0.getServletContext(); System.out.println(sc); } @Override public void service(ServletRequest arg0, ServletResponse arg1) throws ServletException, IOException { System.out.println("service is running"); } @Override public ServletConfig getServletConfig() { // TODO Auto-generated method stub return null; } @Override public String getServletInfo() { // TODO Auto-generated method stub return null; } }
But in actual development , we generally do not directly implement the servlet interface , because there are too many methods to be rewritten, we generally create classes to inherit HttpServlet
Implementation steps: 1) Create a class to inherit the HttpServlet class
2) Override the doGet and doPost methods
3) Configure the servlet in web.xml
The entire access process of the servlet
Servlet API (Lifecycle)
(1) Methods in the Servlet interface
1)init(ServletConfig config)
When to execute: Executed when the servlet object is created
ServletConfig: represents the configuration information of the servlet object
2)service(ServletRequest request,ServletResponse response)
When to execute: will be executed on every request
ServletRequest: On behalf of the request, it is considered that the ServletRequest encapsulates the information of the http request
ServletResponse: On behalf of the response, the information that is considered to be encapsulated is the http response
3)destroy()
When to execute: Executed when the servlet is destroyed
(2) Methods of the HttpServlet class
1)init()
2)doGet(HttpServletRequest request,HttpServletResponse response)
3)doPost(HttpServletRequest request,HttpServletResponse response)
4)destroy()
(3) Servlet life cycle (interview questions)
1) When is the servlet created
By default (when the server starts) this object is created when the servlet is accessed for the first time
2) When is the servlet destroyed
The server closes the servlet and destroys it
3) A method that must be executed every time you visit
service(ServletRequest request,ServletResponse response)方法
10 visits to XXXServlet, how many times are init(), destroy(), service(), doGet(), doPost() executed in total? How many request objects are created? How many responses are created?
Answer: init(), destroy(), doGet(), doPost() are executed once each, service() is executed 10 times, and 10 request and response objects are created
Servlet configuration
basic configuration
How to configure url-pattern :
1) Exact match The accessed resources are exactly the same as the configured resources to be accessed
2) Directory matching Format: /virtual directory.../* *represents any, that is to say, as long as you write all the contents of the virtual directory, the following content can be arbitrary
3) Extension matching Format: *.extension As long as the following extension is correct, the preceding content can be arbitrary
Note: Do not mix the second and third /aaa/bbb/*.abcd
Server startup instantiates servlet configuration
When was the servlet created: by default it is created when it is first accessed
default? When configuring the servlet, add a configuration <load-on-startup> The servlet object is created when the server starts
Default Servlet
You can configure a / in url-pattern , which means that the servlet is the default servlet
When the resource address you access does not match all servlets, the default servlet will be responsible for processing. In fact, the response of all resources in the web application is the responsibility of the servlet, including static resources.
Welcome Screen
ServletContext object
ServletContext represents the environment (context) object of a web application. The ServletContext object encapsulates the information of the web application. There is only one ServletContext object for a web application. There can be multiple servlet objects in a web application
The life cycle of the ServletContext object
Create: the web application is loaded (the server starts or publishes the web application (provided the server is started))
Destroyed: The web application is uninstalled (the server is shut down, the web application is removed)
Get the ServletContext object
1)ServletContext servletContext =config.getServletContext();
public void init(ServletConfig config) throws ServletException {
//获得ServletContext对象
ServletContext context=config.getServletContext();
}
2)ServletContext servletContext =this.getServletContext();(常用)
The role of ServletContext
(1) Obtain the global initialization parameters of the web application
Configure initialization parameters in web.xml
Get parameters through the context object
(2) Obtain the absolute path of any resource in the web application
String path =context.getRealPath(relative address relative to the web application);
Create the file a.txt under WebContent, create b.txt under WEB-INF, and create c.txt under src. When the relative paths are passed in as file names, calling the getRealPath() method with the context object will return a full path, i.e. absolute path
(3) ServletContext is a domain object
The area where data is stored is the domain object
The scope of the ServletContext domain object: the entire web application (all web resources can freely access data to the servletcontext domain, and the data can be shared)
Generic methods for domain objects:
setAtrribute(String name,Object obj);
getAtrribute(String name);
removeAttribute(String name);