In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
It is believed that many inexperienced people are at a loss about how to access ServletConfig and ServletContext parameters. Therefore, this paper summarizes the causes and solutions of the problem. Through this article, I hope you can solve this problem.
HttpServletRequest,HttpServletResponse: these two attributes have the least scope of action.
Time: as long as the request and response are completed, it becomes invalid. Of course, forwarding is to take out the current request object and pass it to another resource. In fact, its own request object only survives until the end of this request, and so is response.
Spatially: the client that can only send the request is valid.
HttpSession: once the connection to the client is closed, the scope of time is larger than the above two, and the scope of space appointment is the same.
ServletConfig: after being instantiated from a servlet, access is valid for any client at any time, but only for this servlet. The ServletConfig object of one servlet cannot be accessed by another servlet.
ServletContext: for any servlet, anyone is valid at any time, and this is the real global object. So how exactly should the ServletConfig and ServletContext parameters be used and obtained?
In general, the configuration of the entire application, in order not to use "hard coding", should be configured with ServletContext parameters, such as character set settings.
< web-app >. < init-param > < param-name > charset < / param-name > < param-value > GB2312 < / param-value > < / init-param >. < / web-app >
Note that the above format is only 2. For the standard format after 0. 0, the old container (engine) is configured in the service provider's own format. Note that its parent element should be < web-app >, that is, it works on an application.
If there is only one parameter to be set by a specific servlet and other servlet cannot be shared, it should be configured as the ServletConfig parameter. For example, an servlet reading attachments will use an absolute directory, while other servlet will not:
< servlet > < servlet-name > GetAtt < / servlet-name > < servlet-class > mail.GetAttServlet < / servlet-class > < init-param > < param-name > absPath < / param-name > < param-value > / usr/mail/ax/axman/Maildir/ < / param-value > < / init-param > < / servlet >
Needless to say, because name and class are already specified in the < servlet > tag, that is, path can only be fetched from the servlet of mail.GetAttServlet, while no other Servlet can be fetched.
So how do you access the parameters of these two objects?
Access ServletConfig parameters:
First get the ServletConfig object, and then call its getInitParameter (); method. To access the ServletConfig object, the config built-in object is directly used in jsp, but since the compiled servlet of your JSP is not usually added to the web.xml, it is not common to get the configuration parameters of the compiled servlet of this JSP through jsp, so there are two ways to get the ServletConfig object in servlet:
Get it in the inii () method: pass it through the overloaded method of init
. Public class Test extends HttpServlet {ServletConfig config; public void init (ServletConfig config) throws ServletException {this.config = config;}. }
You can then access the config object in the following method. Note, however, that to ensure that you can get from the constructor to the config object of the current servlet, you should call the constructor of the parent class:
. Public class Test extends HttpServlet {ServletConfig config; public void init (ServletConfig config) throws ServletException {super.init (config); this.config = config;}. }
The advantage of getting there directly through the getServletConfig () method is that you don't have to manually pass the property, and you want to get it at any time.
There is a third way to implement some interfaces yourself, which is not covered here as a general discussion.
To access the ServletContext object, simply getServletContext () from the existing ServletConfig object, and then call its getInitParameter () method to get its parameters.
After reading the above, have you mastered the method of how to access and analyze ServletConfig and ServletContext parameters? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!
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.