Sie sind auf Seite 1von 1

for ex: if you want the name of your company or your e-mail address appear in all

the pages of your web application then you use servletcontext.

<context-param>
<param-name>developer</param-name>
<param-value>crazy@weird.com</param-value>
</context-param>

in the above xml entry you set the value of context parameter i.e developer as
crazy@weird.com. by making the above entry into your web.xml file you can get the
value of the ontext parameter 'developer' anywhere throughout your web
application. you can have any number of such context parameters.

string s =
getservletcontext.getinitparameter("developer");

you can have the above statement in any of your servlets to get the value of the
string s as "crazy@wierd.com". so you are setting the parameter developer in the
web.xml as a context paramter (which is available throughout the webapp). you get
these context paramters using a servletcontext object as shown in the statement
above.

coming to servletconfig, its a more specific one. the servletcontext is for the
whole web-app but the servletconfig is for one particular servlet.

when you want paramters which cannot be hardcoded in your servlet you use
servletconfig. i cant think of a good example at the moment. lets do with a lame
one;). lets consider you need to have a string value which tells you what a
particular servlet does. remember servletconfig is for a particular servlet and
not for the entire application.

the web.xml file may look like this:

<servlet>
<servlet-name>select</servlet-name>
<servlet-class>select</servlet-class>
<init-param>
<param-name>about the servlet</param-name>
<param-value>this servlet gives you a list of
colors to select from</param-value>
</init-param>
</servlet>

the above entry into a web.xml file is for a servlet named select. <please note
that you are writing the entry within a servlet tag which means that this is for a
particular servlet. unlike for contextparamter where we did not write under any
particular servlet
tag since it was for the whole application.> within the servlet tag you set the
value for a servlet paramter called "about the servlet".

so whenever you do getservletconfig().getinitparamter("about the servlet") you


will get a string "this servlet gives you a list of colors to select from". you
get this value only when you call within the servlet called select because you
have set the paramter only for that particular servlet. unlike context paramters
which you could access anywhere using servletcontext.

to sum it up, every servlet has the same servletcontext but it also has its own
servletconfig.