aspose file tools*
The moose likes Servlets and the fly likes passing initial parameters to jsp Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "passing initial parameters to jsp" Watch "passing initial parameters to jsp" New topic
Author

passing initial parameters to jsp

Srinath R
Ranch Hand

Joined: Jun 28, 2000
Posts: 71
HI all
I struck with the passing initial parameters to JSP page on TOMCAT webserver. to pass initial parameter to a servlet can be achieved with the help of web.xml file, but how to pass initial parameters to a JSP page.
thank you
Dmitriy Pavlyuk
Ranch Hand

Joined: Mar 25, 2001
Posts: 33
You may write initial parameters in jsp in special tags <%! %>
It calls as in servlet init() method. For fully information see jsp and servlet specification on java.sun.com
Vishnu Murthy
Ranch Hand

Joined: Jan 04, 2001
Posts: 56
my JSP page is on TOMCAT webserver i want to pass initial parameters to this JSP, to pass initial parameters to servlet we make changes in web.xml file we set the servlet name and initial parameter name, values to it.
but my question is in the same we way how we have done it for servlets can i pass the parameters to JSP page.
Marcos Maia
Ranch Hand

Joined: Jan 06, 2001
Posts: 977
I don�t think so, I�m using WL6.0 and when I need to pass initial parameters to jsps I have to write another xml file called weblogic.xml and than I can use:
<jsp-descriptor>
<jsp-param>
<param-name>
jsp param name
</param-name>
<param-value>
my value
</param-value>
</jsp-param>
</jsp-descriptor>
and since I have to write a diferent file to do so, problably yo�ll not be able to do using web.xml, but noticed that I�m not really sure and if you find out a way pls post here that I�d love to learn it.

Phil Hanna
Ranch Hand

Joined: Apr 05, 2001
Posts: 118
You pass init parameter to a JSP the same as a servlet, using the web.xml deployment descriptor. The key is using the <jsp-file> element instead of <servlet-class>:
<servlet>
<servlet-name>foo</servlet-name>
<jsp-file>myFile.jsp</jsp-file>
<init-param>
<param-name>flavor</param-name>
<param-value>vanilla</param-value>
</init-param>
</servlet>
Note, however, that you need to call the JSP by its servlet name, which would be %WEBAPPNAME%/servlet/foo, in the case above. If you don't, the servlet engine will not be aware that you are using the <servlet> element with its init parameters.
You can additionally supply a servlet mapping if you still want to use a "jsp" name:
<servlet-mapping>
<servlet-name>foo</servlet-name>
<url-pattern>myFile.jsp</url-pattern>
</servlet-mapping>
If you've never read the JSP specification or the DTD, it can be of great help to you.

------------------
Phil Hanna
Sun Certified Programmer for the Java 2 Platform
Author of :
JSP: The Complete Reference
Instant Java Servlets


Phil Hanna<BR>Sun Certified Programmer for the Java 2 Platform<BR>Author of :<BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072127686/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">JSP: The Complete Reference</A><BR><A HREF="http://www.amazon.com/exec/obidos/ASIN/0072124253/electricporkchop/107-3548162-1137317" TARGET=_blank rel="nofollow">Instant Java Servlets</A>
Vishnu Murthy
Ranch Hand

Joined: Jan 04, 2001
Posts: 56
Hi i tried the following
in web.xml deployment descriptor file i have written as
<servlet>
<servlet-name>RSRemoteAccess</servlet-name>
<jsp-file>RSRemoteAccess.jsp</jsp-file>
<servlet-mapping>
<servlet-name>RSRemoteAccess</servlet-name>
<url-pattern>RSRemoteAccess.jsp</url-pattern>
</servlet-mapping>
<init-param>
<param-name>sqldsn</param-name>
<param-value>coldsql</param-value>
</init-param>
</servlet>
and i am calling this jsp page with url http://machinename ort/mydirectory/RSRemoteAccess.jsp
String param = getInitialParameter("sqldsn);
out.println("parameter is : "+param);
but it is showing retrieved parameter as null
can u pl. tell me how to solve this problem
Vishnu Murthy
Ranch Hand

Joined: Jan 04, 2001
Posts: 56
Dear Phil
after setting initial parameters in web.xml file when i am trying to invoke JSP with the url http://servarname/examples/servlet/RSRemoteAccess
i am getting the following error
Error: 500
Location: /examples/servlet/RSRemoteAccess
Internal Servlet Error:
java.lang.NullPointerException
at org.apache.tomcat.core.ServletWrapper.handleRequest(ServletWrapper.java, Compiled Code)
my web.xml file is like this..

<servlet>
<servlet-name>RSRemoteAccess</servlet-name>
<jsp-file>RSRemoteAccess.jsp</jsp-file>
<init-param>
<param-name>sqldsn</param-name>
<param-value>coldsql</param-value>
<description>set for checking for the First parameter</description>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>RSRemoteAccess</servlet-name>
<url-pattern>RSRemoteAccess.jsp</url-pattern>
</servlet-mapping>
i am not able to solve this
please help me in this matter
thanking you
vishnu
Carl Trusiak
Sheriff

Joined: Jun 13, 2000
Posts: 3340
Your only giving half of things here. Your not showing the line the error is occuring on. I'm assuming it's on
String param = getInitialParameter("sqldsn");
out.println("parameter is : "+param);
It looks like your trying to access the method getInitialParameter defined in your JSP page. There isn't one unless you've defined it yourself inside scriptlet. (And your not showing enough of your jsp for me to tell)
However, I believe what you really want is
String param = config.getInitParameter("sqldsn");
out.println("parameter is : "+param);
config is the implicate object reference available in your JSP to access anything you would use ServletConfig in a normal servlet.

------------------
Hope This Helps
Carl Trusiak, SCJP2
[This message has been edited by Carl Trusiak (edited August 07, 2001).]


I Hope This Helps
Carl Trusiak, SCJP2, SCWCD
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: passing initial parameters to jsp
 
Similar Threads
JSP include
Best practise to load javascript settings from server
passing parameters
question in empty operator
one jsp to another