Forums Register Login

Servlet initParameter in EL

+Pie Number of slices to send: Send
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}
+Pie Number of slices to send: Send
Post your exception.
+Pie Number of slices to send: Send
Exception:

javax.servlet.ServletException: Unable to find a value for "initparameter" in object of class "org.apache.catalina.core.ApplicationHttpRequest" using operator "."
+Pie Number of slices to send: Send
 

Originally posted by vishnu prakash:
Exception:

"initparameter"



Replace above with initParameter and try.
+Pie Number of slices to send: Send
No use Chetan. Same Error with initParameter.
+Pie Number of slices to send: Send
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?
+Pie Number of slices to send: Send
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}
+Pie Number of slices to send: Send
${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.
+Pie Number of slices to send: Send
<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
+Pie Number of slices to send: Send
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
+Pie Number of slices to send: Send
 

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.
+Pie Number of slices to send: Send
 



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.
+Pie Number of slices to send: Send
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 ]
I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 3885 times.
Similar Threads
how to get InitParameter from JSP
A question about servlet initParameter
Initparameter not working
JSP - Problem in reading InitParameter
Mock question-InitParameter
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 06:48:43.