File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Servlet initParameter in EL

 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to print the value of Servlet initParameter in EL. I configured name parameter in wb.xml. I used PageContext to get the value of name parameter. But I get ServletException.

Code in JSP

${pageContext.request.initparameter.name}
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your exception.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Exception:

javax.servlet.ServletException: Unable to find a value for "initparameter" in object of class "org.apache.catalina.core.ApplicationHttpRequest" using operator "."
 
Chetan Parekh
Ranch Hand
Posts: 3640
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vishnu prakash:
Exception:

"initparameter"


Replace above with initParameter and try.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No use Chetan. Same Error with initParameter.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post your web.xml entry.

Did you enter it as a context init-param or as a servlet init-param?
Either way, why are you looking in the request object for it?
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben you misunderstood the question. I am trying to print the value of Servlet initParameter in EL
For context int param I would have used the EL implicit object initParam.

<servlet>
<servlet-name>ST</servlet-name>
<servlet-class>com.example.ServTwo</servlet-class>
<init-param>
<param-name>name</param-name>
<param-value>abc</param-value>
</init-param>
</servlet>


<servlet-mapping>
<servlet-name>ST</servlet-name>
<url-pattern>/two</url-pattern>
</servlet-mapping>

Note:

When we can use ${pageContext.request.method} to know the request method name then I think
the following syntax is correct for getting the servlet init parameter

${pageContext.request.initparameter.name}
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
${pageContext.request... gives you a handle to the the 'request' implicit object variable which points to the instance of HttpServletRequest.

HttpServletRequest has a method called 'getMethod'.
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/http/HttpServletRequest.html

There is nothing in HttpServletRequest or it's parent class for getting init parameters.

There is another implicit object variable 'config' which points you to the ServletConfig object. This has a getInitParameter method:
http://java.sun.com/j2ee/1.4/docs/api/javax/servlet/ServletConfig.html

But that would only allow access to init parameters declared for this JSP's generated servlet; not the controller's.

I think what you would need to do is either declare the init-parameter as a context init param (in which case you would retrieve it from the context implicit object variable) or create a servlet entry for your JSP so that you have access to servlet init params declared in web.xml.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
<servlet>
<servlet-name>JSP</servlet-name>
<jsp-file>/MyELTwo.jsp</jsp-file>
<init-param>
<param-name>name</param-name>
<param-value>abc</param-value>
</init-param>
</servlet>

<servlet-mapping>
<servlet-name>JSP</servlet-name>
<url-pattern>/MyELTwo.jsp</url-pattern>
</servlet-mapping>

Posted by Ben
But that would only allow access to init parameters declared for this JSP's generated servlet;

I think this is correct to get the JSP's generated servlet init parameters.

${pageContext.config.initParameter.name}

But I am getting exception as

javax.servlet.ServletException: Unable to find a value for "config" in object of class "org.apache.jasper.runtime.PageContextImpl" using operator "."

Note: The request is forwared from a servlet to jsp. I also have configuration for the servlet in web.xml. Hope the sequence I am following is not wrong
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally Posted by Ben


create a servlet entry for your JSP so that you have access to servlet init params declared in web.xml.


What does this mean. How to do this? Kindly explain in detail
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by vishnu prakash:
Originally Posted by Ben
What does this mean. How to do this? Kindly explain in detail


I just tried myself and was unable to get a servlet init param using EL.
It may be possible but I haven't found it yet.

However, in the JSP spec, I did see a way to read context init params with el.
This might serve your purpose.

EL:

web.xml



Again, it may still be possible to get a servlet init-param with EL. Look
through the JSP spec (link in my signature).

To create a servlet entry for a jsp, create a regular <servlet> entry but use <jsp-file>/pageName.jsp</jsp-file> instead of a <servlet-class> entry.
From there it's the same as any other servlet entry, add your init-params, mappings, ect.. the same way you would for any other servlet.
 
Vishnu Prakash
Ranch Hand
Posts: 1026
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Originally Posted by Ben

To create a servlet entry for a jsp, create a regular <servlet> entry but use <jsp-file>/pageName.jsp</jsp-file> instead of a <servlet-class> entry.
From there it's the same as any other servlet entry, add your init-params, mappings, ect.. the same way you would for any other servlet.


I have already done that and posted in my previous post. Even that doesn't work.


Regarding context init params, things are clearly explained in HFSJ book. The problem is I couldn't find a way to access servlet init parameter as well as JSP's generated servlet init parameters in my JSP page.

I wish Bear Bibeault takes a look at this thread and provides a solution.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64182
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ben is quite correct. The following expression


${pageContext.request.initparameter.name}


pre-supposes that the request has some means of returning init params, and it does not. Init params are only available from the servletConfig of an individual servlet which is orthogonal to the requests.

The EL has a handful of implicit variables defined, but nothing to obtain the init params for individual servlets (or JSP-generated servlets). If you were to attempt to set something like that up, which servlet would this obtain the params for? The request can be passed around multiple resources, both servlets and JSPs.

Even if the request had a method such as getInitParam(), you wouldn't be able to use it since you would need to pass the name of the param you are trying to obtain to the method. And the EL has no means to do that.

Implicit EL variables such as paramValues work the way they do because they are not methods, but rather Map instances.

My suggestion, if you want access to the initParams for the controller servlet, is to create a Map of the init params for the servlet and place it on the request as a scoped variable -- let's say named initParameters. You would then be able to obtain any param by name with ${requestScope.initParameters.name}.
[ October 05, 2005: Message edited by: Bear Bibeault ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic