servletContenxt objects

1. Definitions

ServletContext objects created by the server, a project is only one object. Whether conduct is the same object acquired in any position to get the project, different user-initiated request to get is the same object, and the object is jointly owned by the user.

2. Features

[1] server creation

[2] users to share

[3] A project is only one life cycle: start server-to-server shut down

[4] Scope: within the project

3. Use

(1) How to get ServletContext object

this.getServletContext();

(2) Set Properties

Adding attributes: setAttribute (String name, Object obj);

Get the value: getAttribute (String name), this method returns Object

Delete Attribute: removeAttribute (String name)

Request (3) to achieve the forwarding Servlet

this.getServletContext().getRequestDispatcher("/url").forward(request, response);

(4) using the ServletContext object read (such as properties files) resource file:

// This method reads the default path is the root directory of Web applications 
InputStream Stream = the this .getServletContext () getResourceAsStream ( "dbinfo.properties." );
 // create an object attribute 
the Properties the Properties = new new the Properties ();
properties.load(stream);
String name = properties.getProperty("name");
String password = properties.getProperty("password");
out.println("name="+name+";password="+password);

 

Guess you like

Origin www.cnblogs.com/WhiperHong/p/11025435.html