1. Obtain the initialization parameters of Tomcat's Context.
1. Get the initialization parameters of Context set in Tomcat's server.xml.
For example:
Copy the code code as follows:
<Context path="/testcontext" docBase="/context"
privileged="true" antiResourceLocking="false" antiJARLocking="false"
debug="0" reloadable="true">
<Parameter name="name" value="yangqisheng" />
</Context>
Method: getServletContext().getInitParameter(String name)
2. Obtain the initialization parameters that set the Context in web.xml under the project.
For example:
Copy the code code as follows:
<context-param>
<param-name>age</param-name>
<param-value>24</param-value>
</context-param>
Method: getServletContext().getInitParameter(String name)
2. Record Tomcat logs
1. Set up log files
In the server.xml file, use the logger element to set up the log file.
Copy the code code as follows:
<Logger className="org.apache.catalina.logger.FileLogger"
prefix="localhost_log." suffix=".txt" timestamp="true"/>
Write log: this.getServletContext().log("Test")
3. Access resource files
3.1 getResource(String parh) method: The path must start with /, which represents the root directory of the current web application. Returns a returned URL object representing a resource.
3.2 getResoutceAsStream(String parh), returns the file stream. The advantage is that you can access all files in the web directory using paths relative to the root directory without having to know the absolute path.
For example, create a new file me.properties under WEB-INF with the following content:
name=yangqisheng
age=25
Copy the code code as follows:
this.getServletContext().getResourceAsStream("/WEB-INF/me.properties");
Properties me = new Properties();
me.load(is);
out.write(me.getProperty("name"));
out.write(me.getProperty("age"));
Then execute in Servlet:
will print out yangqisheng25