Servlet context ServletContext

Source: Internet
Author: User

1. Context: When a web container is started, it creates a corresponding ServletContext object for each web order, representing the current web application. Function: obtains shared data from the memory of a Web application, accesses static resources of a web application, and communicates between Servlet objects through the ServletContext object. 2 The following interface method uses ServletContext application = this in Servlet. getServletContext (); common methods for obtaining the context object application context are as follows: getInitParameter (String name) getResourceAsStream (String path) the input stream returns the resource corresponding to the path. The path parameter must use "/" getInitParameterNames () to obtain the names of all initialization parameters. The returned result is an enumerated value getRequestDispatcher (String name) get RequestDispatcher object setAttribute () based on the forwarded address set the attribute value getAttribute () Get the attribute value getAttributeNames () Get all attribute value names 3 The context parameter is in the web. configure initialization parameters in xml: <context-param> <param-name> adminEmail </param-name> <param-value> webmaster </param-value> <context-param> element for the entire application, therefore, it is not nested in a <servlet> element. This element is a direct sub-element of the <web-app> element. Access the initialization parameter from Servlet: ServletContext application = this. getServletContext (); application. getInitParameter ("email"); 4 context attribute ServletContext application = this. getServletContext (); application. setAttribute ("name", "value"); application. getAttribute ("name"); Enumberation p = application. getAttributeNames ();

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.