`
- 浏览:
55893 次
-
Servlet config is very important in servlet's life. Every servlet gets a reference to servlet config object when its init method runs . Before init method runs(i.e servlet does not have a reference to config object) a servlet is a any other java class. It is only after servlet gets a reference to a config object, it becomes servlet.
A config object has a reference to request and response object. When a servlet is invoked for the first time, it gets reference to a config object which indeed has reference to request and response object.
In a Web application every servlet has its own config object i.e there is one config for every servlet and there is only one servlet context for the entire application.
you can store name-value pairs in config object. you do it in web.xml file. Here is an example on how you do it,
<servlet>
<servlet-name>FirstServ</servlet-name>
<servlet-class>FirstServ</servlet-class>
<init-param>
<param-name>email</param-name>
<param-value>xyz@gmai.com</param-value>
</init-param>
</servlet>
you declare servlet config name-value pairs in <init-param> tags and in the servlet(here FirstServ) when you say getServletConfig().getInitParameter("ema… you get the value in a String
Servlet context is similar but you dont declare it in any servlet tags in web.xml instead you declare it directly inside <web-app> tag, in between <context-param> tags,
<context-param>
<param-name>email</param-name>
<param-value>xyz@gmai.com</param-value…
</context-param>
and in any servlet or jsp you use it by saying getServletContext().getInitParameter("em…
-------------------------------------------------
<!--The init-param element contains a name/value pair as aninitialization param of the servletUsed in: filter, servlet--><!ELEMENT init-param (param-name, param-value, description?)>...<!--The context-param element contains the declaration of a webapplication's servlet context initialization parameters.Used in: web-app--><!ELEMENT context-param (param-name, param-value, description?)>
The easy answer is that you use different methods to access them, however the real difference is in intended use.
Context parameters are accessible to any servlet or JSP in the web-app. The usual example is webmaster's address. Another example might be the name of a system or resource that is required by many parts of the system. These init parameters are within the scrope of the entire application. The parameters defined in <context-param> are available by calling getServletContext().getInitParameter(String name).
The init-params of a servlet are specific to that servlet, eg: a value that the servlet uses. These init parameters are within the scope of the servlet. The parameters defined in <servlet> are available by calling getInitParameter(String name) on the ServletConfig that was passed to the servlet/filter's init method. Since GenericServlet stores this ServletConfig object you can simply call the getInitParameter(String name) that it provides.
For example:
getServletContext().getInitParameter("foo") (returns context init params), getServletConfig().getInitParameters("foo") (returns servlet init params).getInitParameters("foo") (uses method inherited directly from GenericServlet
and returns servlet init params)
--------------------------------------------------------------
Difference between ServletConfig and ServletContext
Both ServletConfig and ServletContext are configuration objects used by servlet container to initialize various init parameters for a web application. However they differ in terms of the scope and availability of init parameters defined by both of them.
Init Parameters in deployment descriptor (web.xml)
Init parameters for ServletConfig are defined within <servlet> element for each specific servlet.
<servlet> <servlet-name>BeerParamTests</servlet-name> <servlet-class>TestInitParams</servlet-class> <init-param> <param-name>foo</param-name> <param-value>bar</param-value> </init-param> <!-- other stuff --></servlet>
Init parameters for ServletContext are defined within the <web-app> element but NOT within a specific <servlet> element.
<web-app> <context-param> <param-name>foo</param-name> <param-value>bar</param-value> </context-param> <!-- other stuff including servlet declarations --></web-app>
Servlet Code
getServletConfig().getInitParameter("foo"); for ServletConfig
getServletContext().getInitParameter("foo"); for ServletContext
Availability
There's only one ServletContext for an entire web app, and all the parts of the web app share it. Hence parameters required by all the servlets should be configured within ServletContext. For example database connection parameters.
But each servlet in the app has its own ServletConfig. Hence the parameters, needed by a particular servlet but should not be accessed by other servlets, should be configured by ServletConfig.
The Container makes a ServletContext when a web app is deployed, and makes the context available to each Servlet and JSP (which becomes a servlet) in the web app.
-----------------------------------------------------------------
As explained by Adeel Ansari, here, it depends on what object are you invoking the method getInitParameter() in the servlet code.
All servlets extends from and hence are instance of GenericServlet.
.
DD elements <context-param> can be retrieved by:
ServletContext context = this.getServletContext();
String paramValue = context.getInitParamter("paramName");
.
DD elements <init-param> both can be retrieved by:
ServletConfig config = this.getServletConfig();
String paramValue = config.getInitParamter("paramName");
Also note that since GenericServlet class implements ServletConfig interface, your servlet class is also ServletConfig (implies this = this.getServletConfig() ). Hence you can also get DD elements <init-param> directly by:
String paramValue = this.getInitParamter("paramName");
.
You can try this by having same param-name in both DD elements with different values and then print it in your servlet.
分享到:
Global site tag (gtag.js) - Google Analytics
相关推荐
ServletContext与ServletConfig关系
ServletConfig对象的试验代码
注意:每个servlet有一个ServletConfig,每个Web应用有一个ServletContext.要把初始化参数认为是部署时常量,可用在运行时得到这些初始化参数,但不能设置初始化参数,根本没有setInitParameter().如果修改XML来改变...
JavaWeb新版教程Servlet-1vlet-ServletConfig类使用介绍.avi
JavaWeb新版教程Servlet-1vlet-ServletConfig类的补充说明.avi
NULL 博文链接:https://shoushounihao.iteye.com/blog/1919541
很清楚的讲解了ServletContext和ServletConfig,不光有理论上的讲解,还有例子。
博文链接:https://hotfisher.iteye.com/blog/92177
1 ServletConfig.getServletContext(); 2 ServletContextEvent.getServletContext(); 3 HttpSession.getServletContext(); ------------------------------------------------ JSP Java Server Page java服务...
超全面javaweb教程28天第9天_6_ServletConfig介绍
Overview Package Class Tree Deprecated Index Help PREV NEXT FRAMES NO FRAMES A B C D E F G H I J L P R S U V -------------------------------------------------------------------------------- ...
ServletConfig是针对特定的Servlet的参数或属性。ServletConfig是表示单独的Servlet的配置和参数,只是适用于特定的Servlet。从一个servlet被实例化后,对任何客户端在任何时候访问有效,但仅对本servlet有效,一个...
下面小编就为大家带来一篇有关ServletConfig与ServletContext的访问。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
ServletConfig getServletConfig() 返回传递到servlet的init()方法的ServletConfig对象 void service(ServletRequest request, ServletResponse response)throws ServletException,IOException 处理request对象中...
ServletConfig : 代表的是该servlet对象的配置信息 2)service(ServletRequest request,ServletResponse response) 何时执行:每次请求都会执行 ServletRequest :代表请求 认为ServletRequest 内部封装的是 ...
黑马 程序员-方立勋老师-javaWeb开发-03-Web开发入门和 04-Servlet开发和ServletConfig和ServletContex对象 知识总结记录
javax.servlet.ServletConfig javax.servlet.GenericServlet javax.servlet.ServletContext javax.servlet.ServletRequest javax.servlet.http.HttpUtils javax.servlet.ServletResponse javax.servlet....
servlet_basics什么是Servlet? Servlet是在客户端发出请求并生成响应...解释javax.servlet.Servlet接口Servlet接口包含生命周期方法init(ServletConfig config)-创建对象后(在构造函数之后)仅被调用一次(在servle