aspose file tools*
The moose likes JSP and the fly likes Servlet initParameter in EL Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Servlet initParameter in EL" Watch "Servlet initParameter in EL" New topic
Author

Servlet initParameter in EL

Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
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}


Servlet Spec 2.4/ Jsp Spec 2.0/ JSTL Spec 1.1 - JSTL Tag Documentation
Chetan Parekh
Ranch Hand

Joined: Sep 16, 2004
Posts: 3636
Post your exception.


My blood is tested +ve for Java.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
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

Joined: Sep 16, 2004
Posts: 3636
Originally posted by vishnu prakash:
Exception:

"initparameter"


Replace above with initParameter and try.
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
No use Chetan. Same Error with initParameter.
Ben Souther
Sheriff

Joined: Dec 11, 2004
Posts: 13410

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?


Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
Vishnu Prakash
Ranch Hand

Joined: Nov 15, 2004
Posts: 1026
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

Joined: Dec 11, 2004
Posts: 13410

${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

Joined: Nov 15, 2004
Posts: 1026
<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

Joined: Nov 15, 2004
Posts: 1026
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

Joined: Dec 11, 2004
Posts: 13410

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

Joined: Nov 15, 2004
Posts: 1026


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

Joined: Jan 10, 2002
Posts: 61654
    
  67

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 ]

[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
 
subject: Servlet initParameter in EL