wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes EL retrieving init param in servlet element Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "EL retrieving init param in servlet element" Watch "EL retrieving init param in servlet element" New topic
Author

EL retrieving init param in servlet element

Shiang Wang
Ranch Hand

Joined: Jun 20, 2003
Posts: 96
Is there anyway to use EL to retrieve init-param defined inside servlet element?

Thanks
Shiang


SCBCD, SCWCD, SCJP
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
Yes, through the implicit objects defined.


SCJP 1.4 SCWCD 1.4
Jessica White
Ranch Hand

Joined: Sep 24, 2004
Posts: 30
try ${pageContext.servletConfig["init_param_name"]}

since pageContext has a getServletConfig() method(servletConfig property), and servletConfig is a map.
Shiang Wang
Ranch Hand

Joined: Jun 20, 2003
Posts: 96
When I used ${pageContext.servletConfig["firstName"]}, the error is
javax.servlet.ServletException: Unable to find a value for "firstName" in object of class "org.apache.catalina.core.StandardWrapperFacade" using operator "[]"

When I tried
${pageContext.servletConfig.initParameter.firstName} I am getting error:
javax.servlet.ServletException: Unable to find a value for "initParameter" in object of class "org.apache.catalina.core.StandardWrapperFacade" using operator "."

I am sure both ${pageContext.servletConfig} and ${pageContext.page} are not null, but their usage in el are different from the other objects we can access from pageContext, such as session, request, response.. etc.

Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
What does the spec say about implicit objects ?
Shiang Wang
Ranch Hand

Joined: Jun 20, 2003
Posts: 96
According to the spec and EL evaluation rule, pageContext is a PageContext and all of the objects we can access via PageContext should be available in EL as well. Could it be a bug in tomcat? any thought ? Because I am just having problem with ${pageContext.servletConfig.initParameter.xxx}, tried several other objects via pageContext, all works.


JSP.2.7 Implicit Objects
The EL defines a set of implicit objects which depends on the context in which
the EL is being used. When an expression references one of these objects by name,
the appropriate object is returned instead of the corresponding attribute. For example
in the context of JSP pages, ${pageContext} returns the PageContext object,
even if there is an existing pageContext attribute containing some other value. See
Section JSP.2.2.3 for details.


JSP.2.2.3 Implicit Objects
There are several implicit objects that are available to EL expressions used in
JSP pages. These objects are always available under these names:
� pageContext - the PageContext object
� pageScope - a Map that maps page-scoped attribute names to their values
� requestScope - a Map that maps request-scoped attribute names to their values
� sessionScope - a Map that maps session-scoped attribute names to their values
� applicationScope - a Map that maps application-scoped attribute names to
their values
� param - a Map that maps parameter names to a single String parameter value
(obtained by calling ServletRequest.getParameter(String name))
� paramValues - a Map that maps parameter names to a String[] of all values for
that parameter (obtained by calling ServletRequest.getParameterValues(String
name))
� header - a Map that maps header names to a single String header value (obtained
by calling ServletRequest.getHeader(String name))
� headerValues - a Map that maps header names to a String[] of all values for
that header (obtained by calling ServletRequest.getHeaders(String))
� cookie - a Map that maps cookie names to a single Cookie object. Cookies are
retrieved according to the semantics of HttpServletRequest.getKookies(). If the
same name is shared by multiple cookies, an implementation must use the
first one encountered in the array of Cookie objects returned by the getKookies()
method. However, users of the cookie implicit object must be aware that
the ordering of cookies is currently unspecified in the servlet specification.
� initParam - a Map that maps context initialization parameter names to their
String parameter value (obtained by calling ServletContext.getInitParameter(String name))


Thanks.
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
I tried through EL and was unsuccessful. Using a scriptlet (and Tomcat 5.0.29) I was able to get the servlet init paramter by doing this:
<%=config.getInitParameter("attr") %>
Shiang Wang
Ranch Hand

Joined: Jun 20, 2003
Posts: 96
Thanks, I know you can get it using jsp syntax. I am just trying to prove it in EL. It seems to be a dead end to me, so I will move on. Thanks for your help and answer.
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Originally posted by Jessica White:
try ${pageContext.servletConfig["init_param_name"]}

since pageContext has a getServletConfig() method(servletConfig property), and servletConfig is a map.



Howdy -- this will not work because the servletConfig that you get back is *not* a map--it's a ServletConfig object, and it does not have a specific property with your init parameter name. It *does* have a getInitParameter method, but that method takes a String in order to return back the value. I might be missing something really obvious, but I haven't figured out how to pass that String into the method. In other words, initParameter isn't a standard *property* of the ServletConfig object (if you treat ServletConfig as a bean), because the getter method takes a String in order to know *which* attribute to return. You COULD get the enumeration of the init parameter names, probably, but that doesn't really help either.

Unless someone knows a way to do this, my recommendation would be that servlet init params are *old school* and no longer considered a particularly useful thing. So the way around this is to simply not use them, or to have some part of the web app *read* the servlet init param and stick it into one of the attribute scopes. Remember, the servlet init param is intended for a single servlet, not an entire web app anyway. So even though you *can* make servlet init parameters available to the generated JSP, it's not what you'd want to do in the real world anyway...

But again, someone else might have a *real* simple answer that I'm missing.

cheers,
Kathy
Jean-Sebastien Abella
Ranch Hand

Joined: Jul 29, 2005
Posts: 60
I just use ${initParam.whatever}
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026

I just use ${initParam.whatever}


This is to retrieve Context init parameter. Not Servlet init parameter.


Read the Servlet init parameters and make them available in any one of the four scopes in Servlet code. Now this can be accessed in JSP using EL.
[ November 16, 2005: Message edited by: Vishnu Prakash ]

Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Musab Al-Rawi
Ranch Hand

Joined: Aug 06, 2007
Posts: 231
http://faq.javaranch.com/java/InitParamsThruExpressionLanguage


SCBCD - SCWCD - SCJD - SCJP - OCA
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

Was it necessary to bring back a thread from 2005 ?


[My Blog]
All roads lead to JavaRanch
Musab Al-Rawi
Ranch Hand

Joined: Aug 06, 2007
Posts: 231
When I googled for an answer i got this thread as the first result and it was without an answer, so i thought it would be good to help those who will search later.
wenbin liu
Greenhorn

Joined: May 29, 2010
Posts: 2
Musab Al-Rawi wrote:When I googled for an answer i got this thread as the first result and it was without an answer, so i thought it would be good to help those who will search later.


Yes, I googled it. And I saw this thread.

But to be frank, I am not real satisfy with the answer since we cannot get the servlet config init parameters with EL simple, we have to another preparation work(put it into some other scope attribute), which is tedious.

However, I really cannot find a better way.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: EL retrieving init param in servlet element