• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

ServletConfig & Context ?

 
Ranch Hand
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what this two :

1] ServletConfig
2] Context

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

Thanks .
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 13411
Firefox Browser VI Editor Redhat
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 3852
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are they just used for sending initialization parameters or is there any other
use as well?
 
Sripathi Krishnamurthy
Ranch Hand
Posts: 232
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic