aspose file tools*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes ServletConfig redundant? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "ServletConfig redundant?" Watch "ServletConfig redundant?" New topic
Author

ServletConfig redundant?

Gennady Shapiro
Ranch Hand

Joined: Sep 25, 2001
Posts: 196
Can someone explain to me why GenericServlet has decided to extend Servlet <B>AND</B> ServletConfig when Servlet already defines ServletConfig as a parameter to init()?
Appears now: ServletConfig.init(SerlvetConfig). Looks a lot like they are trying to inherit <B>AND</B> aggregate.
I see no logic in that.
Also, how come ServletRequest.getParameterValues()is the only method in servlet universe that returns String[], all other such methods return Enumeration? Whats so special about it?
Mark Spritzler
ranger
Sheriff

Joined: Feb 05, 2001
Posts: 17250
    
    6

I think I can answer the second question. In the case of getParameterNames, it is coming from the request from the client, meaning the parameters are in the URL in name value pairs. These values are all Strings always.
In the case of the other methods that return Enumerations, that is because what is being returned are Objects(all different type), not always just Strings.
For example if you are getting the Attributes from a Session object, these are Objects that were bound to the Session Object via setAttribute(Object obj) method.
So it is like a HashSet, you get an Object and usually have to cast it to the actual type.
Mark


Perfect World Programming, LLC - Two Laptop Bag - Tube Organizer
How to Ask Questions the Smart Way FAQ
Gennady Shapiro
Ranch Hand

Joined: Sep 25, 2001
Posts: 196
what about getParameterNames(), getInitParameterNames() and bunch of other methods that return Enumeration of Strings (and Strings only)?
Madhav Lakkapragada
Ranch Hand

Joined: Jun 03, 2000
Posts: 5040
I think I can answer the second question
Damn..me too, but with a different flavor..
Here goes what I understand...
(we know these are NOT static methods but, to
clarify the class they are defined I am using
the class name. Don't pick on me for that.)
ServletReuqest.getParameterNames() and ServletConfig.getInitParameterNames() returns Enumeration of Strings because, the app server doesn't know how many you have defined. Can be zero to many.
ServletReuqest.getParameterValues() returns an array of Strings[]. Given a name, I think all the values it has will be a comma-seperated string, right? So the app server is already going thru a String Tokenizer (?) and getting each name seperatly. If already Enumerates them so I think is already determines how many values a given "name" has. Hence its easy to return a String[]. Not sure if I make sense....guessing!
But it sure is inconsistent. Also did you observe that ServletConfig doesn't support getInitParameterValues().....
For what its worth...
- satya


Take a Minute, Donate an Hour, Change a Life
http://www.ashanet.org/workanhour/2006/?r=Javaranch_ML&a=81
Gennady Shapiro
Ranch Hand

Joined: Sep 25, 2001
Posts: 196
1. You come from the assumption that server initially does not know the number of parameters (or iniit parameters). I dont think that's true, a request (or DD) strictly defines every parameter and getting count of ParamNames and ParamValues is just a matter of one run-through. In this sense ParamNames are not any different from ParamValues. The spec says that before a request is processed(i.e. Request object is constructed) the server gets paramets from GET as well as POST and sets them into Request in a uniformed manner, so by the time Request object is served it has a well-defined collection of Param Name/Values. Same applies to InitParams.
2. I think that the reason ServletConfig doesn't support getInitParameterValues() is that it's uncommon and unreasonable to supply Init Params of the same name. Really, why would you?
Unlike the case with HTTP Params, where it is very common and reasonable -- a series of HTML radio buttons or check boxes could have the same name, for example.
Regardless, I think we may see (in spec 3.0 perhaps) all that done away with [] and Enumerations and migrated to J2 Collections.
John Strong
Greenhorn

Joined: Jul 23, 2006
Posts: 13
Noone is answer the first (to me more interesting) question!! If GenericServlet implements all the methods of ServletConfig, why the heck does the container need to pass a ServletConfig object into the init() method???

Is this because we need a separate ServletConfig object to handle data within the scope of the individual servlet (as opposed to the context of the entire app)???

This question is related to another: is the servlet context created when the first servlet is initialized? Or is it created when the container is deployed (started)?

Thanks, - John Strong (pluviosilla@gmail.com)
Bryan Basham
author
Ranch Hand

Joined: Apr 30, 2001
Posts: 199
Originally posted by John Strong:
Noone is answer the first (to me more interesting) question!! If GenericServlet implements all the methods of ServletConfig, why the heck does the container need to pass a ServletConfig object into the init() method???


The answer is radically simple: convience. You see, there is a one-to-one mapping between a servlet instance (unless using the evil STM) and that servlet's configuration object. In the early servlet days, people had to write code like this getSevletConfig().getInitParameter("myparam") which was rather tedius. So, the API authors decided to make GenericServlet implement ServletConfig and provide all of these convience methods. Consider it "API sugar".

Is this because we need a separate ServletConfig object to handle data within the scope of the individual servlet (as opposed to the context of the entire app)???


Every servlet instance (defined in the DD) has a unique config object. This has nothing to do with the context object, which is global to all servlets within a single webapp.

Also, the config object is not a "scope" like the other scopes: request, session, and application. The data held by the config object is static a reflects the configuration in the DD, such as init params for the servlet.

This question is related to another: is the servlet context created when the first servlet is initialized? Or is it created when the container is deployed (started)?


The servlet context is created once when the webapp is started. A servlet config is created during that servlet's initialization, just before the first execution of the servlet.

Make sense?

Cheers,
Bryan
[ July 23, 2006: Message edited by: Bryan Basham ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: ServletConfig redundant?