PackageCn.jiemoxiaodi.servlet_servletcontext;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjava.util.Enumeration;ImportJavax.servlet.ServletContext;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classFservletextendsHttpServlet {/*** The Doget method of the servlet. <br> * * This method was called when a form have its tag value me Thod equals to get. * * @paramRequest * The request send by the client to the server *@paramResponse * The response send by the server to the client *@throwsservletexception * If an error occurred *@throwsIOException * If an error occurred*/ Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException {ServletContext context= This. Getservletcontext (); String username=context.getinitparameter ("username"); Enumeration ENU=Context.getinitparameternames (); while(Enu.hasmoreelements ()) {String key=(String) enu.nextelement (); String value=Context.getinitparameter (key); SYSTEM.OUT.PRINTLN (Key+ "-========>" +value); } }}
<?xml version= "1.0" encoding= "UTF-8"? ><web-app version= "2.5" xmlns= "Http://java.sun.com/xml/ns/javaee"Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd "><display-name></display-name> <context-param> <param-name>username</param-name> <param-value>tomcate</param-value> </context-param> <context-param> <param-na Me>pwd</param-name> <param-value>123456</param-value> </context-param> <!--configuration S Ervlet to Projects--<servlet> <!--you can just fill it out with the name of the other servlet (simple class name is recommended)--<servlet-name >AServlet</servlet-name> <!--Configuring the full class name of the servlet--<servlet-class>cn.jiemoxiaodi.servlet.aservlet</servlet-class> <init-param> <param-name>aa</param-name> <param-value>sdfds</ Param-value> </init-param> </servlet> <!--configuration This servlet uses that path to be accessed--<servlet-ma Pping> <!--can not be written here, it must correspond to the above servlet, indicating that the servlet is configured with a path--<servlet-name>aservlet</servlet -name> <!--Configure access to this servlet's path (relative path)/==>http://localhost:8080/day10_servlet/http://localhost: 8080/day10_servlet/aservlet --<url-pattern>/AServlet</url-pattern> </servlet-mapping> <welcome-file-list> <welco me-file>index.jsp</welcome-file> </welcome-file-list> <servlet> <servlet-name>bser Vlet</servlet-name> <servlet-class>cn.jiemoxiaodi.servlet_config. bservlet</servlet-class> <init-param> <param-name>a</param-name> <param-value>123456sdfd& lt;/param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>b servlet</servlet-name> <url-pattern>/BServlet</url-pattern> </servlet-mapping> <se Rvlet> <servlet-name>CServlet</servlet-name> <servlet-class>cn.jiemoxiaodi.mygenericservlet.cservlet</servlet-class> <init-param> <param-name>name</param-name> <param-value> need Oh, </. param-value> </init-param> </servlet> <servlet> <servlet-name>dservlet</ Servlet-name> <servlet-class>cn.jiemoxiaodi.http.dservlet</servlet-class> </servlet> <servlet> <servlet-name>FServlet</servlet-name> <servlet-class>cn.jiemoxiaodi.servlet_servletcontext. fservlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CServlet</servlet-name> <url- pattern>/cservlet</url-pattern> </servlet-mapping> <servlet-mapping> <servlet-name> ;D servlet</servlet-name> <url-pattern>/DServlet</url-pattern> </servlet-mapping> <se Rvlet-mapping> <servlet-name>FServlet</servlet-name> <url-pattern>/fservlet</url-pattern > </servlet-mapping></web-app>
Http://localhost:8080/projectname/FServlet
PackageCn.jiemoxiaodi.servlet_servletcontext;Importjava.io.IOException;ImportJava.io.PrintWriter;Importjavax.servlet.ServletException;ImportJavax.servlet.http.HttpServlet;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse; Public classHservletextendsHttpServlet {/*** The Doget method of the servlet. <br> * * This method was called when a form have its tag value me Thod equals to get. * * @paramRequest * The request send by the client to the server *@paramResponse * The response send by the server to the client *@throwsservletexception * If an error occurred *@throwsIOException * If an error occurred*/ Public voiddoget (httpservletrequest request, httpservletresponse response)throwsservletexception, IOException { This. Getservletcontext (). SetAttribute ("AA", "DSFD"); This. Getservletcontext (). SetAttribute ("ADFA", "DSFD");
This.getservletcontext (). RemoveAttribute ("AA");
This.getservletcontext (). RemoveAttribute ("ADFA");
System.out.println(this. Getservletcontext (). getattribute ("AA") + ' ===> ' this . Getservletcontext (). getattribute ("ADFA"));} }
ServletContext Object Introduction1. This object represents our entire project .==>There is only one instance of this object in a project. 2When you start a project, it is automatically created. The instance is destroyed when the server is shut down. 3. How does the object get it? ==>One method in ServletConfig is called the Getservletcontext method to obtain. Function One: You can get some configuration in the project (Web. xml) getinitparameter (String name) to get the value based on the key Getinitparameternames () Get all keys above these two methods have the same name as the ServletConfig. But the configuration information obtained is<context-param> <param-name>userName</param-name> <param-va Lue>tom</param-value> </context-param> <context-param> & Lt;param-name>password</param-name> <param-value>1234</param-value> </context-param>two: As one of the three major domain objects in our servlet technology. If you include a JSP, there are actually 4 domain page fields. Application==>ServletContext Session Request domain object involves 4 methods GetAttribute (key) Gets the value SetAttribute (Key,value) from the key to the map in the application domain to hold a key-value pair RemoveAttribute (key) removes a key-value pair from a map in the application domain Getattributenames (); Get all the keys from the map in the application domain
// how to get resources // This.getservletcontext (). Getrealpath (path) // This.getservletcontext (). getResourceAsStream (path) System.out.println (this. Getservletcontext (). Getrealpath ("/web-inf/demo.xml")); InputStream inputthis. Getservletcontext (). getResourceAsStream ("/web-inf/demo.xml"); SYSTEM.OUT.PRINTLN (input);
Three: You can get the resources in the project. (e.g. Stu.xml for students in the project)//You want to operate on the web-inf/demo.xml. We need to read the file out first.//How to read it? //File File = new file ("e:/code/workspaces0808/day10-servlet/webroot/web-inf/demo.xml"); //It is obviously wrong to get the Demo.xml fill in the path above. Get the project and we're going to get the final deployment on the server.//File File = new file ("E:/apache-tomcat-6.0.35/webapps/day10-servlet/web-inf/demo.xml"); //This is the right way to get it. But if the project is redeployed elsewhere, the dead path will not work. //This is similar to the example of obtaining external resources, so the ServletContext object gives a workaround. //The Getrealpath ("/") ==> obtains the absolute path, based on the relative path in the parameter. "/" Relative to Webroot//getResourceAsStream ("/"); ==> directly gets the stream of the resource you want to find. parameter or fill relative path, ibid.Example: String path= Getservletcontext (). Getrealpath ("/web-inf/demo.xml"); SYSTEM.OUT.PRINTLN (path); InputStream is= Getservletcontext (). getResourceAsStream ("/web-inf/demo.xml"); System.out.println (is);
Using class and ClassLoader to get the resource path in your project
Web-inf under the
String path1 = Getservletcontext (). Getrealpath ("/web-inf/demo.xml");
Lib under the
String path2 = Getservletcontext (). Getrealpath ("/web-inf/lib/demo.xml");
SRC under the
String path3 = Getservletcontext (). Getrealpath ("/web-inf/classes/demo.xml");
Under the Cn.itcast.servlet.servlet_context bag.
String Path4 = Getservletcontext (). Getrealpath ("/web-inf/classes/cn/itcast/servlet/servlet_context/demo.xml");
In fact, in the above example, Path3 and Path4 have some trouble in both ways
Introducing a way to get resources is the application of the. Class ==> class and ClassLoader
InputStream is = This.getclass (). getResourceAsStream ("Demo.xml"); If you do not add "/" ==> is relative to the current (package) directory of the class
InputStream Is2 = This.getclass (). getResourceAsStream ("/demo.xml");//If added "/" ==> "/" On behalf of Classes directory (SRC)
In the next classLoader there is also a method ClassLoader is the class loader. ==> dedicated to reading the input stream of class files
It also provides a way to read other files. (use ClassLoader not to read larger files)
This.getclass (). getClassLoader () ==> get ClassLoader for class
InputStream is3 = This.getclass (). getClassLoader (). getResourceAsStream ("/demo.xml");//No addition "/", relative to the classes directory, The relative path to add "/" in class is the same.
You can also get the absolute path of the file using class or ClassLoader
URL url = this.getclass (). GetResource ("Demo.xml");
System.out.println (Url.getpath ());
The ServletContext of the Servlet