Several ways to read parameters in a servlet

Source: Internet
Author: User

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

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.