Because all servlets in a WEB app share the same ServletContext object, multiple servlets implement data sharing through ServletContext objects.
ServletContext objects are also commonly referred to as context domain objects. (also request session page)
String data = "aaaa123"; this. Getservletcontext (). SetAttribute ("Data", data);
this. Getservletcontext (). getattribute ("Data"), Response.getoutputstream (). Write ("data:" + Value). GetBytes ());
When these two are placed in two Servletdemo classes, the value of ServletContext can be accessed on different pages in different browsers.
// reading the value of a property in Webroot\web-inf\web.xml this. Getservletcontext (). Getinitparameter ("data1"); Response.getoutputstream (). Write ( Value.getbytes ());
<servlet> <Servlet-name>ServletDemo5</Servlet-name> <Servlet-class>Cn.itcast.ServletDemo5</Servlet-class> <Init-param> <Param-name>Data1</Param-name> <Param-value>X1x2x3</Param-value> </Init-param></servlet>
The following code implements the forwarding of the Servlet: (1.jsp is in webroot\1.jsp), but because ServletContext is a domain shared by all users, it is not appropriate to use it here.
String data = "Aaa5"; // Bringing the data to 1.jsp cannot go through the ServletContext domain, but through the request domain. this. Getservletcontext (). SetAttribute ("Data"this. Getservletcontext () . Getrequestdispatcher ("/1.jsp"); Rdispatcher.forward (request, response);
<Divstyle= "border:1px solid red;"> <% StringData= (String) Application.getattribute ("Data"); Out.write (data); %> </Div>
The following code reads the key values in the Db.properties configuration file under the SRC directory
//The resource file is placed in the SRC directory in the source code, but appears in the Web-inf\classes directory after it is published. InputStream InputStream = This. Getservletcontext (). getResourceAsStream ("/web-inf/classes/db.properties"); Properties Properties=NewProperties ();p roperties.load (InputStream); String urlstring= Properties.getproperty ("url"); String username= Properties.getproperty ("username"); String Password= Properties.getproperty ("Password"); Response.getoutputstream (). Write ("URL:" +urlstring). GetBytes ()), Response.getoutputstream (). Write (("\r\nusername:" +username). GetBytes ()), Response.getoutputstream (). Write (("\r\npassword" + password). GetBytes ());
If the configuration file is placed under the package, the path will be in the/web-inf/classes/cn/itcast/directory
If the configuration file is in the WebRoot directory, the path becomes/db.properties
If you want to use FileInputStream to read the resource file, use the following code to get the file's true path:
this. Getservletcontext (). Getrealpath ("/web-inf/classes/db.properties");
If it is a Java class, not a Servlet class to read a resource file, use the following code
InputStream InputStream = Userdao. class. getClassLoader (). getResourceAsStream ("db.properties"new properties (); Properties.load (InputStream);
Then, because only one read at the start of the app is needed, it is usually written in the following code:
Private StaticProperties Dbconfig =NewProperties ();Static { Try{InputStream InputStream= Userdao.class. getClassLoader (). getResourceAsStream ("Db.properties"); Dbconfig.load (InputStream); } Catch(Exception e) {Throw NewExceptionininitializererror (e); }}//if the program that reads the resource file is not a Servlet, it can only be read by the class loader Public voidUpdate ()throwsIOException {System.out.println (Dbconfig.getproperty ("url"));}
After such code is published, modifying the configuration file requires a restart of the application, which can be improved using the following methods:
InputStream InputStream = Userdao. class. getClassLoader (). getResourceAsStream ("db.properties"); // The configuration file loaded by the code above is modified and still read to the original value String Path = Userdao. class. getClassLoader (). GetResource ("db.properties"new fileinputstream (path);
Instead of using the first sentence code, use the following code.
[Java] [Web] Application of ServletContext method