permaculture playing cards*
The moose likes Servlets and the fly likes ServletConfig & Context ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Servlets
Bookmark "ServletConfig & Context ?" Watch "ServletConfig & Context ?" New topic
Author

ServletConfig & Context ?

ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
what this two :

1] ServletConfig
2] Context

can you post some code , how to use them ...

Thanks .
H Wilson
Ranch Hand

Joined: Feb 23, 2005
Posts: 33
Head First Servlets and JSP has a very clear and easy-to-understand explanation of these two objects. In a nutshell, servlet config pertains to a single servlet and servlet context pertains to an entire application. Both are configurable in the deployment descriptor (web.xml). I am beginning to believe there is no excuse for not owning a copy of Head First Servlets and JSP.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletConfig.html
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletContext.html

The first place a Java programmer should look to learn about any Java Object, is the API documentation for that object.


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Sripathi Krishnamurthy
Ranch Hand

Joined: Mar 07, 2005
Posts: 232
sample code to use servletconfig and servletcontext.

to start with, You need to know about web.xml,
here is a sample web.xml

<servlet>
<servlet-name>Testing</servlet-name>
<description>This is just for testing</description>
<servlet-class>mypackage.com.Testing</servlet-class>
<init-param>
<param-name>useMe</param-name>
<param-value>Hurray..I am being used</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>Testing</servlet-name>
<url-pattern>/Testing</url-pattern>
</servlet-mapping>

<context-param>
<param-name>locale</param-name>
<param-value>utf-8</param-value>
</context-param>

First ServletConfig, use the code inside the servlet "Testing"

ServletContext context = getServletContext();
ServletConfig config = context.getServletConfig ();
Enumeration enum = config.getInitParameters ();
//or else you can use
String value = config.getInitParameter("useMe");
//print "value" and you will see "Hurray..I am being used"
NOTE that getinitparam is only available for this servlet since this is declared in web.xml. other servlets have to define their own init-params in web.xml.
This implies that every servlet will have "its own" servletconfig.

Now Servlet Context,

ServletContext context = getServletContext();
String value = context.getInitParameter("locale");
//print "value" and you will see "utf-8"

Any servlet within the web application can use the context param. hence ServletConfig is available to all the servlets inside the webapp.

ServletConfig is for that servlet or the page.
ServletContext is for the entire application.
Adeel Ansari
Ranch Hand

Joined: Aug 15, 2004
Posts: 2874
Originally posted by H Wilson:
I am beginning to believe there is no excuse for not owning a copy of Head First Servlets and JSP.


Why?? Is it become free?
I saw a mate looking for a free legal copy of it, like we have "Core Servlet & JSP", and "Servlets and JavaServerPages" legally free.
H Wilson
Ranch Hand

Joined: Feb 23, 2005
Posts: 33
Originally posted by Adeel Ansari:
[QB]

Why?? Is it become free?
QB]


Certainly lack of funds is a legitimate excuse for not owning Head First books. I just find that so many of the basic questions about Servlets and JSP are answered very, very clearly and simply in that book. For that reason I would recommend a new Java programmer to read that book first and the Sun documentation second. It's definitely a great place to start if Servlets and JSP are new to you. Some people may be annoyed by the "dumbed-down" approach, but I find I can get past that because the information is so clearly explained.
ankur rathi
Ranch Hand

Joined: Oct 11, 2004
Posts: 3830
Thanks a lot Sripathi ,
I got it very well . but why we need this :

<servlet-mapping>
<servlet-name>Testing</servlet-name>
<url-pattern>/Testing</url-pattern>
</servlet-mapping>

Thanks a lot again .
osman cinar eren
Ranch Hand

Joined: Jan 25, 2005
Posts: 78
thus,

if such a URL comes to your application,
http://yourserver.yourdomain:yourport/yourapplication/XXX
(yourapplication-->context)
then the servlet container look at the servlet mappings in your web.xml. and find the servlet that corresponds to /XXX and will send the client's request to that particular servlet.

hope this helps.
regards.


SCJP/SCWCD
Pravin Jagan
Greenhorn

Joined: Feb 28, 2005
Posts: 27
Just to add more clearity...

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.
karthik Bhat
Greenhorn

Joined: Mar 23, 2005
Posts: 1
Are they just used for sending initialization parameters or is there any other
use as well?
Sripathi Krishnamurthy
Ranch Hand

Joined: Mar 07, 2005
Posts: 232
Any servlet within the web application can use the context param. hence ServletContext is available to all the servlets inside the webapp.

ServletConfig is for that servlet or the page.
ServletContext is for the entire application. //this means that the context can be used to set and get paramaters from all the servlets. init-param is one way to do this.
You can also use the below methods in ServletContext

setAttribute(java.lang.String name, java.lang.Object object)
//Binds an object to a given attribute name in this servlet context.

getAttribute(java.lang.String name)
//Returns the servlet container attribute with the given name, or null if there is no attribute by that name.
 
GeeCON Prague 2014
 
subject: ServletConfig & Context ?