In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains "detailed introduction of ServletContext". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn the detailed introduction of ServletContext.
First, when the WEB container starts, it creates a corresponding ServletContext object for each WEB application, which represents the current WEB application.
Second, the ServletContext object can obtain the reference to the Servlet object through the ServletConfig.getServletContext method, and it can also obtain the reference to its object through the this.getServletContext90.
3. Because all Servlet in a WEB application share the same ServletContext object, Servlet objects can communicate with each other through ServletContext object. The ServletContext object is also commonly referred to as the context domain object. It is used in public chat rooms.
4. How to use ServletContext?
1. How do I get a ServletContext object?
ServletContext sc=this.getServletContext ()
two。 Think of ServletContext as a table, which is very similar to session.
Add attribute: setAttribute (String name,Object ob)
Value obtained: getAttribute (String name)
Delete attribute: removedAttribute (String name)
3. Life cycle
The life cycle of properties in ServletContext starts from creation to the end of server shutdown
5. ServletContext application
1. Can be shared by multiple servlet in the project
two。 Gets the initialization parameters of the wenb application. For example, we have a code like this in web.xml
Name
Scott
If it is a brotherly relationship with the element
String value=this.getServletContext () .getInitParameter (name)
The result value of value is scott
If it's the son of the element,
String value=this.getServletConfig () .getIn.getInitParameter (name)
The value of value is also scott.
3. Can request forwarding
(1) response.sendRedirect ("/ web application name / resource name")
(2) request.getRequestDispatcher ("/ resource name") .forward (request,response) = = this.getServletContext.getRequestDispatcher ("/ resource name") .forward (request,response)
4. Read the resource file of web application
(1) Resource files are written under WebRoot
1 > read the contents of the resource file, for example, there is a msql.properties file
The contents of the document are as follows:
Username=scott
Password=123
How to read:
InputStream is=this.getServletContext () .getresourceAsStream ("msql.properties"); / / read to a file
Propreties pp=new Propreties ()
Pp.load (is)
System.out.println ("name=" + pp.getProperty ("username")); / / the result is scott
2 > read the full path of the file, for example, there is a / imgs/1.jpg file under webRoot
String Path=this.getServletContext () .getRealPath ("/ imgs/1.jpg")
(2) use class loader for resource files under src
Randomly pick a class under the src file, such as: Servlet.java
InputStream is=Servlet.class.getClassloader.getResourceAsStream ("File name")
At this point, I believe that you have a deeper understanding of the "detailed introduction of ServletContext", you might as well come to the actual operation! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.