[Original]java Web learning Note 06:servletcontext interface

Source: Internet
Author: User

this blog is original: Integrated still Silicon Valley (http://www.atguigu.com) System tutorial (deep thank) and network of existing resources (blogs, documents, books, etc.), the source of resources I will markThe purpose of this blog: ① summary of their own learning process, equivalent to study notes ② to share their own experience to everyone, learn from each other, communication, not commercialcontent inevitably appear problems, welcome to correct, exchange, discussion, you can leave a message, can also be contacted by the following ways. I Internet technology enthusiasts, Internet technology enthusiastsWeibo: Ivan is in 0221qq:951226918

--------------------------------------------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------- -----------------------------

1. About Servletcontex

1) The servlet engine creates a corresponding ServletContext object for each Web application, and the ServletContext object is contained in ServletConfig (about ServletConfig reference another blog) object, call the Servletconfig.getservletcontext method to return a reference to the ServletContext object .

2) because all servlets in a Web application share the same ServletContext object , the ServletContext object is called a Application object (Web Application object); is important

3) functions that can be implemented (get information on various aspects of the current application)

> Get initialization parameters for a Web application

> Logging

>application Domain-wide properties

> Accessing resource Files

> Get the local path mapped by the virtual path

>web Access between applications

2. Configure ServletContext: In the current web directory of the XML configuration, each configuration of a parameter, you must re-create a <context-param> </context-param> node, after configuration is complete, the initialization parameters for Servletcontex can be obtained for any servlet. Two servlet initialization parameters, only the current servlet can get

1     <!--Configure initialization parameters for the current WEB app -2     <Context-param>3         <Param-name>Driver</Param-name>4         <Param-value>Com.mysql.jdbc.Driver</Param-value>5     </Context-param>6     7     <Context-param>8         <Param-name>Jdbcurl</Param-name>9         <Param-value>Jdbc:mysql:///test</Param-value>Ten     </Context-param>

3. Get configuration information for ServletContext

1) getinitparameter (String parametername): Gets the parameter value of the initialization driver

2) getinitparameternames () : Gets all the parametername values

3) Getrealpath (String path): Gets the absolute path of one of the currently applied files on the server , must be under the WebContent folder

4) Getcontexpath (): Gets the name of the current app  

5) getResourceAsStream (String path) path/is the root directory relative to the Web App : Gets the input stream for a file of the current WEB app (very important), Create a jdbc.properties under SRC

1 @Override2      Public voidInit (ServletConfig servletconfig)throwsservletexception {3System.out.println ("Init");4 5         //1. Get the ServletContext object, the current web app information6ServletContext ServletContext =Servletconfig.getservletcontext ();7 8         //2. Get the parameter values for initializing driver9String Driver = servletcontext.getinitparameter ("Driver");TenSystem.out.println ("Driver:" + driver);//output-Driver One                                                 //: Com.mysql.jdbc.Driver A  -         //3. Get all parametername values, output -enumeration<string> names =servletcontext.getinitparameternames (); the          while(Names.hasmoreelements ()) { -String name =names.nextelement (); -SYSTEM.OUT.PRINTLN (name);//output, driver Jdbcurl -         } +  -         //4. Obtain an absolute path to a file on the server that is currently applied and must be in the WebContent folder +String Realpath = Servletcontext.getrealpath ("/hello.jsp"); A         //Output at         //E:\java_workspase\eclipes\.metadata\.plugins\org.eclipse.wst.server.core\tmp1\wtpwebapps\javaWEB\ hello.jsp - System.out.println (realpath); -  -         //5. Get the name of the current app -String ContextPath =Servletcontext.getcontextpath (); -System.out.println (ContextPath);//Output-/javaweb in  -         //gets the input stream for a file of the current web App to         //Way One: +         Try { -ClassLoader ClassLoader =getclass (). getClassLoader (); theInputStream is = Classloader.getresourceasstream ("Jdbc.properties"); *System.out.println ("1." +is ); $}Catch(Exception e) {Panax Notoginseng e.printstacktrace (); -         } the  +         //Way Two: A         Try { the             //The path is the absolute path to the Web project in the server, where/is the root: that is, the webcontent in development +InputStream Is2 =ServletContext -. getResourceAsStream ("/web-inf/classes/jdbc.properties"); $System.out.println ("2." +is2); $}Catch(Exception e) { -  - e.printstacktrace (); the         } - Wuyi}

4. Summary

1) fully understand the ServletContext object: Represents the entire Web application

2) Understand each method

[Original]java Web learning Note 06:servletcontext interface

Related Article

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.