Detailed description of servlet resource path loading (3), detailed description of servlet

Source: Internet
Author: User

Detailed description of servlet resource path loading (3), detailed description of servlet

1. For the servlet to read the resource file, consider the project file path and the file after the tomcat server is deployed, so be sure to find out the path of the file to be loaded, and consider which method:
There are generally three methods:

(1). Use the traditional method, that is,InputStream is = new FileInputStream ("config. properties ");

(2). Use the ServletContext object to load

(3). Use a Class Loader

2. Verify with code

Package cn. wwh. www. web. servlet;
Import java. io. File;
Import java. io. FileInputStream;
Import java. io. IOException;
Import java. io. InputStream;
Import java. util. Properties;
Import javax. servlet. ServletContext;
Import javax. servlet. ServletException;
Import javax. servlet. http. HttpServlet;
Import javax. servlet. http. HttpServletRequest;
Import javax. servlet. http. HttpServletResponse;
/**
* Category: Study the meaning of the data read in the servlet and the path strength of the loaded file
*
*
* @ Author
* @ Version 1.0
* @ Creation Time: 11:24:05 AM
*/
Public class DealFilePath extends HttpServlet {
Public void doGet (HttpServletRequest request, HttpServletResponse response)
Throws ServletException, IOException {
/**
* A traditional method is used to load files. For example, the path of the file to be loaded is FileInputStream ("config. properties ").
* The path is located in the config. properties file under the bin directory of the tomcat server.
* If the loaded path is src/config. properties, the file is real.
* The config. properties file in src, a folder under the tomcat server, is relative to the bin directory in the traditional method (that is, in jdk ).
*
*/
/* InputStream is = new FileInputStream ("config. properties ");
Properties prop = new Properties ();
Prop. load (is );
System. out. println ("name:" + prop. getProperty ("age "));
*/

/**
* For Files loaded in this way, "/" indicates the directory Summer_Exercise6.
* That is, the file loading path is relative to the project file name under the webapps directory in tomcat.
* 1. context. getResourceAsStream ("/config2/config. properties ")
* The full path is webapps/Summer_Exercise6/config2/config. properties.
* 2. the full path of context. getResourceAsStream ("/config. properties") is:
* Webapps/Summer_Exercise6/config. properties
* 3. If you want to load files under the src directory of the project, such as the config1/config. properties file
* It should be written like this:/WEB-INF/classes/config1/config. properties
* "/" Still indicates the file path is Summer_Exercise6.
*/
/* ServletContext context = this. getServletContext ();
InputStream is = context. getResourceAsStream ("/config2/config. properties ");
Properties prop = new Properties ();
Prop. load (is );
System. out. println ("name:" + prop. getProperty ("name "));*/

ServletContext context = this. getServletContext ();
InputStream is = context. getResourceAsStream ("/WEB-INF/classes/config1/config. properties ");
Properties prop = new Properties ();
Prop. load (is );
System. out. println ("name1:" + prop. getProperty ("name "));

/**
* When the class loader is used for processing, "/" indicates the src directory under the current project name, that is,/WEB-INF/classes/
* The full path of cl. getResourceAsStream ("/config1/config. properties") is:
* Summer_Exercise6/The WEB-INF/classes/config1/config. properties
*/

// Returns the current bytecode object
Class clazz = this. getClass ();
// Obtain the class loading object
ClassLoader cl = clazz. getClassLoader ();
// Load the resource file through the Class Loader
Is = cl. getResourceAsStream ("/config1/config. properties ");
Prop = new Properties ();
Prop. load (is );
System. out. println ("name2:" + prop. getProperty ("name "));
}
}


3. directory structure:

(1) project directory diagram;


(2) directory structure of the tomcat server;

4. Summary:

(1) If you read the resource file (txt/xml/properties) in a traditional way, it is relative to the bin/directory of the web server.
(2) If the resource file (txt/xml/properties) is read in the form of ServletContext, it is relative to the current web application directory of the web server. At this time,/indicates the current web application, that isSummer_Exercise6
(3) the class loader can only load the resource file under the src directory under the IDE tool, other directories cannot load at this time/representation:/WEB-INF/classes/directory

(4) differences between ServletContext and class loaders:
ServletContext mode:
1) The File Location of the resource is arbitrary.
2) When a file is loaded, it will only be read as a stream and will not load all at once
3)/indicates the current web application, that is, Summer_Exercise6
Classloader method:
1) the location of the resource file can only be placed under the class path, that is, under the src directory of the IDE Tool
2) When a file is loaded, all files will be loaded at one time.
3)/indicates current/WEB-INF/classes/

Note:

1> physical hard drive path \ [window System]
2> network path usage/
3> If/indicates the client path, it indicates tomcat/webapps/directory




How to call Servlet for processing

<Description> *: specify a text description for the Servlet. <Display-name> *: provides a brief name for the Servlet to be displayed by some tools. <Icon> *: specify an icon for the Servlet to represent the Servlet in the graphic management tool. <Servlet-name>: Servlet name, which is unique. <Servlet-class> or <jsp-file>: <servlet-class> is the complete Servlet Path. <Jsp-file> specify the full path of the jsp file in the web application to map the URL of a jsp file. <Init-param> *: defines Servlet initialization parameters. <Description> *: Initialize the parameter text description. <Pararm-name>: name of the initialization parameter. <Param-value>: value of the initialization parameter. <Load-on-startup>?: The order in which the Servlet is loaded when the WEB application is started. The content is an integer value. Negative or this element is not set to indicate that the Servlet container will load positive values or o when the client requests this Servlet, indicating that the WEB application will initialize the Servlet from small to large when it is started. If both <jsp-file> elements and <load-on-startup> are included, the jsp file is also pre-compiled and loaded. <Run-as>?: Role for executing the component <descripttion>: text description. <Role-name>: specifies the role name of the Execution Component. <Security-role-ref> *: Declares security role references in the code of the component or deployed component. <Description> *: security role text description. <Role-name>: name of the security role used. <Role-line>?: Specifies a reference to a security role. 2. The <servlet-mapping> element and its child elements define a ing between the servlet and url. <Servlet-name>: Servlet name, which is unique and consistent. It is the same as the name declared in the <servlet> element. <Url-pattern>: Specifies the URL path relative to the Servlet. This path is relative to the root path of the web application context. 3. The Context object of the container processes the Request Path (URL) during Servlet loading. After removing the Context path of the request URL, match by path ing rules with Servlet ing path (<url-pattern>). If the matching succeeds, the Servlet is called to process the request. Matching rules: the request URL is exactly matched. If the request is successful, the Servlet is called to process the request. Match the longest path prefix. Use/as the path separator to perform step-by-step Matching Based on the path tree. Select the longest matching Servlet for processing. If none of the above matches are successful, the container will ask the WEB application to call Servlet to process the request. If no default Servlet is defined, the container will send the 404 error message to the client (the requested resource does not exist. Scala Servlet in Scala language description Servlet API discussion Introduction Servlet container and ContextServlet source file to Class process detailed explanation Listener listen to Http Session

The servlet cannot find the path.

You do not need to manually set the classpath. follow these steps:
1. Set the jdk path in eclipse.
2. Set the tomcat path in elipse.
3. When creating a project, remember to create a web project. This will automatically import the j2ee package to the project.

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.