Introduction of common methods in ServletContext _java

Source: Internet
Author: User

I. Gets the initialization parameters for the tomcat context.
1. Gets the initialization parameters for the context set in Tomcat's Server.xml.
For example:

Copy 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>

Mode: Getservletcontext (). Getinitparameter (String name)
2. Gets the initialization parameters for setting the context in the Web.xml under the project.
For example:
Copy Code code as follows:

<context-param>
<param-name>age</param-name>
<param-value>24</param-value>
</context-param>

Mode: Getservletcontext (). Getinitparameter (String name)

Second, record Tomcat log
1. Set up log files
In the Server.xml file, use the logger element to set up the log file.

Copy Code code as follows:

<logger classname= "Org.apache.catalina.logger.FileLogger"
Prefix= "Localhost_log." suffix= ". txt" timestamp= "true"/>

Write log: This.getservletcontext (). log ("test")

Third, access to resource files
3.1 GetResource (String Parh) method where path must be/start representing the root directory of the current Web application. Returns a URL object that represents a resource.
3.2 Getresoutceasstream (String Parh), returns the file stream. The advantage is that you can access all the files in the Web directory using a path relative to the root directory without having to know the absolute path.
For example, under Web-inf, create a new file me.properties, which reads:
Name=yangqisheng
Age=25

Copy 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 the servlet:
Will print out the Yangqisheng25

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.