Several key points of Servlet and several key points of servlet
1. ServletConfig
ServletConfig is the Servlet configuration file. Corresponds to the <servlet> </servlet> label in web. xml. ServletConfig is an interface provided by java, and its implementation class is provided by tomcat.
Its APIs include:
String getServletName (); // get the content in <servlet-name>ServletContext getServletContext (); // get the Servlet context objectString getInitParameter (String name) // obtain the value of the specified initialization parameter through the name Enumeration getInitParameterNames () // obtain the names of all initialization parameters
2. ServletContext
OneProjectThere is only one ServletContext, which is equivalent to the <web-app> </web-app> tag. You can obtain this unique object from different servlets and transmit data to multiple servlets. Tomcat is created at startup and destroyed only when tomcat is disabled.
Obtain public initialization parameters and prepare them for all servlets. <Servlet> </servlet> <init-param> </init-param> is prepared for the current Servlet.
<context-param> <param-name>name</param-name> <param-value>value</param-value></context-param>
Obtain the resource path:
// Convert the relative path to the actual path String path = this. getServletContext (). getRealPath ("relative path"); // after obtaining the resource path, create the output stream InputStream in = this. getServletContext (). getResourceAsStream ("/index. jsp "); // obtain all resource paths in the current path. Set <String> paths = this. getContext (). getResourcePaths ("/WEB-INF ");
3. Servlet 3.1, Servlet, and thread security
Thread security means that multi-threaded access to the same code segment does not produce different results. Thread-safe code writing relies on thread synchronization. Servlet is NOT thread-safe and highly efficient. Therefore, do not create a member variable in the Servlet. just create a local variable. If you create a member variable, it can be stateless. You can create a stateful member. The member must be read-only. Prevents one thread from performing read operations and one thread from executing write operations.
3.2 when the server is started, the Servlet <load-on-startup> 0 </load-on-startup> is created as a non-negative integer. The smaller the value, the higher the execution priority.
<servlet> <servlet-name></servlet> <servlet-class></servlet-clss> <load-on-startup>0</load-on-startup></servlet><servlet> <servlet-name></servlet> <servlet-class></servlet-clss> <load-on-startup>1</load-on-startup></servlet>
3.3 <url-pattern>
<Url-pattern> is a child element of <servlet-mapping>. Specifies the Servlet access path. Starting "/"