File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

EL retrieving init param in servlet element

 
Shiang Wang
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Is there anyway to use EL to retrieve init-param defined inside servlet element?

Thanks
Shiang
 
Colin Fletcher
Ranch Hand
Posts: 200
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, through the implicit objects defined.
 
Jessica White
Ranch Hand
Posts: 30
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
try ${pageContext.servletConfig["init_param_name"]}

since pageContext has a getServletConfig() method(servletConfig property), and servletConfig is a map.
 
Shiang Wang
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What does the spec say about implicit objects ?
 
Shiang Wang
Ranch Hand
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 200
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 96
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Rancher
Posts: 1589
5
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 60
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I just use ${initParam.whatever}
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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 ]
 
Musab Al-Rawi
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
http://faq.javaranch.com/java/InitParamsThruExpressionLanguage
 
Christophe Verré
Sheriff
Pie
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Was it necessary to bring back a thread from 2005 ?
 
Musab Al-Rawi
Ranch Hand
Posts: 231
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic