Set initialization parameters for each servlet
Initialization parameters can be written for each servlet under the servlet node in the corresponding Web. XML, in the following format:
<init-param>
<param-name>userName</param-name>
<param-value>admin</param-value>
</init-param>
Then get the corresponding parameters in the servlet using the following code:
ServletConfig config = this. Getservletconfig ();
this. Username = Config.getinitparameter ("username");
Set common initialization parameters for all Servlets
You can set common initialization parameters for all Servlets, which are different from the above parameters, which are placed under the corresponding Servlet node, and the common parameters are not allowed to be placed under the servlet node.
<context-param>
<param-name>userName</param-name>
<param-value>admin</param-value>
</context-param>
The global configuration information object We set is also available in the servlet in the following code:
ServletContext Context = this. Getservletcontext ();
String Usernameinglobal = Context.getinitparameter ("UserName");
To set public properties in code
You can set properties in your code for ServletContext, and then you can get them anywhere. This property is a global property that can be used throughout the container once it has been successfully set.
ServletContext Context = this. Getservletcontext (); context. SetAttribute ("MaxNumber", 999); int maxnumebrincontext = (int) context.getattribute ("MaxNumber"); |
Reading configuration information from external files
There are three main forms of reading configuration information from external files through the ServletContext object:
- GetResource
- Returns a URL object, and then calls the OpenStream () method to get the InputStream
- getResourceAsStream
- Directly returns the InputStream object
- Getrealpath
- Returns the absolute path to the resource file, and then reads the data with the FileInputStream class through an absolute path
In the classes directory there is a person.properties file with the following contents:
GetResource Getting external files
@Override Public void Init () throws servletexception { ServletContext ctx = this. Getservletcontext (); String ResourcePath = "/web-inf/classes/person.properties"; Try { URL URL = Ctx.getresource (ResourcePath); InputStream is = Url.openstream (); String name = getpropertiesbykey("name", is); System. out. println (name); } Catch (malformedurlexception e) { TODO auto-generated Catch block E.printstacktrace (); } Catch (ioexception e) { TODO auto-generated Catch block E.printstacktrace (); } } Read the properties information from the stream Public Static String Getpropertiesbykey (String key, InputStream is) { Properties Properties = New properties (); Try { Properties.load (IS); } Catch (ioexception e) { TODO auto-generated Catch block E.printstacktrace (); } String Value = (String) properties.get (key); return value; } |
getResourceAsStream Getting external files
Public void Init () throws servletexception { ServletContext ctx = this. Getservletcontext (); String ResourcePath = "/web-inf/classes/person.properties"; InputStream is = Ctx.getresourceasstream (ResourcePath); String age = Getpropertiesbykey(' age ', is); System. out. println ("Ages:" + age); } |
Getrealpath Getting external files
Public void Init () throws servletexception { ServletContext ctx = this. Getservletcontext (); String ResourcePath = "/web-inf/classes/person.properties"; String Resourcerealpath = Ctx.getrealpath (ResourcePath); Try { InputStream is = new fileinputstream (new File (Resourcerealpath)); String address = Getpropertiesbykey("Address", is); System. out. println ("Address is:" + addresses); } Catch (filenotfoundexception e) { TODO auto-generated Catch block E.printstacktrace (); } } |
Several ways to read parameters in a servlet