File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes A question about servlet initParameter Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "A question about servlet initParameter" Watch "A question about servlet initParameter" New topic
Author

A question about servlet initParameter

ZHAN QING LUO
Greenhorn

Joined: Mar 18, 2005
Posts: 3
Anyone know how to use EL to get the servlet initParameter? Thanks.
Colin Fletcher
Ranch Hand

Joined: Sep 10, 2004
Posts: 200
You may want to read some EL primer documents. I believe there are some links off the ranch page.


SCJP 1.4 SCWCD 1.4
Jose Esteban
Ranch Hand

Joined: Nov 28, 2004
Posts: 102
I propose you three questions:
1) What servlet want you get the init parameters of?
2) Can you get those servlet init parameters not using EL?
3) Can a JSP page have servlet init parameters?

Hope this helps.
Praveen Kumar Mathaley
Ranch Hand

Joined: Apr 14, 2003
Posts: 45
1) What servlet want you get the init parameters of?
jsp servlet( that's the jsp translated into servlet by container)
2) Can you get those servlet init parameters not using EL?
since jsp is translated into servlet...
3) Can a JSP page have servlet init parameters?
yes you can pass init parameter to JSP ( servlet) by using
<servlet-name>name</servlet-name> <jsp-file>/a.jsp</jsp-file>

regarding getting init parameters of servlet from JSP using EL..
we cannot get directly using EL as no implicit object is defined for getting servlet init parameters
Jean Meira
Greenhorn

Joined: Jan 14, 2005
Posts: 8
I believe that this works:

${pageContext.config.initParameter['paramName']}

[ March 22, 2005: Message edited by: Jean Meira ]
[ March 22, 2005: Message edited by: Jean Meira ]
Jose Esteban
Ranch Hand

Joined: Nov 28, 2004
Posts: 102
Originally posted by Praveen Kumar Mathaley:
1) What servlet want you get the init parameters of?
jsp servlet( that's the jsp translated into servlet by container)

Sure, if it is possible, this has to be the servlet.

2) Can you get those servlet init parameters not using EL?
since jsp is translated into servlet...)

Try to do it. If you can, please, tell me how you did it.

3) Can a JSP page have servlet init parameters?
yes you can pass init parameter to JSP ( servlet) by using
<servlet-name>name</servlet-name> <jsp-file>/a.jsp</jsp-file> )

OK, the servlet generated from the JSP could have init parameters, but the question is: Can you get them?
Please, don't be theoric in your answers. I wrote code to get the init parameters and it was no possible. Here's the code:
Jose Esteban
Ranch Hand

Joined: Nov 28, 2004
Posts: 102
Originally posted by Jean Meira:
I believe that this works:

${pageContext.config.initParameter['paramName']}


Nope, it can't work since it isn't even correct syntactically. There's no such thing as a getConfig() bean style method in pageContext.

It would be syntactically correct to write ${pageContext.servletConfig} (since there's a getServletConfig() method in pageContext), but you can't do anything else.

In fact, I'm pretty sure that you can't get the init parameters even if you don't use EL.
ZHAN QING LUO
Greenhorn

Joined: Mar 18, 2005
Posts: 3
Hello, every one, Thanks very much for your responses and discussion. I'm preparing for the SCWCD exam. I know it's very easy to use script to get servlet init Parameter.like this:

<%= config.getInitParameter("name")%>

but I'm just curious that if we can use EL to do the same thing. I know this works: ${pageContext.servletConfig.initParameterNames}

but this one don't work: ${pageContext.servletConfig.initParameter['name']}
for example 'name' is the parameter

so, what's the next... Is EL can't do this???
[ March 22, 2005: Message edited by: ZHAN QING LUO ]
Jose Esteban
Ranch Hand

Joined: Nov 28, 2004
Posts: 102
Originally posted by ZHAN QING LUO:
I know it's very easy to use script to get servlet init Parameter.like this:

<%= config.getInitParameter("name")%>

It's obvious that this works in a servlet but, what about a JSP page? Try to do it, and tell me if it works for you. It doesn't work for me.


I know this works: ${pageContext.servletConfig.initParameterNames}

Really? Have you tried to do it? I did it and it doesn't work.

Has anybody REALLY tried to set and then get an init parameter in a JSP page?
Bassam Zahid
Ranch Hand

Joined: Mar 09, 2005
Posts: 61
Yes I am pretty sure you can get both context and servlet init paramters with JSP.

Here is how I did it:

1) You have to bind init parameters for JSP in DD:


2) Use this scriptlet in JSP to access defined init parameters:

(You can also use pageContext object for above code)

3) You can even do this !


So it is pretty clear now. You can access init parameters any way round, either by config or by pageContext object. Same holds true for context init parameters.

However, when using EL, you cannot access init parameters. Although init paramter names can be retrieved using EL:

Yes, you can only get that far, and yes for both config and application objects.

I hope its clear now.


BS<br />SCJP 1.4 88%<br />SCWCD 1.4 86%
Jose Esteban
Ranch Hand

Joined: Nov 28, 2004
Posts: 102
Thanks Bassam

Everything is clear now. I was missing the <servlet-mapping> in the DD and that's why I couldn't get the servlet init parameters in the JSP page.

Cheers,
Jose
Praveen Kumar Mathaley
Ranch Hand

Joined: Apr 14, 2003
Posts: 45
at last reached conclusion of what i have said...
thank you bassam...
Bassam Zahid
Ranch Hand

Joined: Mar 09, 2005
Posts: 61
Everybody is welcome.
Jose Esteban
Ranch Hand

Joined: Nov 28, 2004
Posts: 102
Can anybody figure out why people who designed the EL gave us objects to easily get attributes in any scope, request parameters, request headers, cookies and context init parameters, but they made the EL in a way that it is impossible to get servlet init parameters?

Any suggestions are welcome.
[ March 30, 2005: Message edited by: Jose Esteban ]
Bassam Zahid
Ranch Hand

Joined: Mar 09, 2005
Posts: 61
I think support for init parameters will be added in future EL versions through JCP.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: A question about servlet initParameter