Several key points of Servlet and several key points of servlet

Source: Internet
Author: User

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 "/"

 

 

  

  

  

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.