`
wwwjjq
  • 浏览: 55893 次
社区版块
存档分类
最新评论

servletConfig--initParam

 
阅读更多
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