How to read data from web.xml (JSP)?

S

Sameer

Dear All,
I am using Tomcat 6.0 on Windows XP SP2.
What to do if i dont want to hard-code the database driver and server
address etc. into code.
I put it into web.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID">
<display-name>ithelplinereports</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>db_driver</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
<param-name>db_server</param-name>
<param-value>192.168.31.41</param-value>
<param-name>db_port</param-name>
<param-value>1521</param-value>
</context-param>
</web-app>


I used the following code to access it in JSP.

ServletContext context = getServletContext();
String driver = context.getInitParameter("db_driver");
String server = context.getInitParameter("db_server");
String port = context.getInitParameter("db_port");
<%
System.out.println(driver);
%>

But I am getting the null values on the console.

By using System.out.println(context), I noted that
the context object is not null and its value is:
org.apache.catalina.core.ApplicationContextFacade@1a0d866

Then why I am getting the null value for
context.getInitParameter("db_driver").

Please revert.
Is this the right way to read data from web.xml? Any suggestions?

-Sameer
 
S

shakah

Dear All,
I am using Tomcat 6.0 on Windows XP SP2.
What to do if i dont want to hard-code the database driver and server
address etc. into code.
I put it into web.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID">
<display-name>ithelplinereports</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>db_driver</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
<param-name>db_server</param-name>
<param-value>192.168.31.41</param-value>
<param-name>db_port</param-name>
<param-value>1521</param-value>
</context-param>
</web-app>

I used the following code to access it in JSP.

ServletContext context = getServletContext();
String driver = context.getInitParameter("db_driver");
String server = context.getInitParameter("db_server");
String port = context.getInitParameter("db_port");
<%
System.out.println(driver);
%>

But I am getting the null values on the console.

By using System.out.println(context), I noted that
the context object is not null and its value is:
org.apache.catalina.core.ApplicationContextFacade@1a0d866

Then why I am getting the null value for
context.getInitParameter("db_driver").

Please revert.
Is this the right way to read data from web.xml? Any suggestions?

-Sameer

Try application.getInitParamer("db_driver") in your JSP page.
 
D

david.karr

Below.

Dear All,
I am using Tomcat 6.0 on Windows XP SP2.
What to do if i dont want to hard-code the database driver and server
address etc. into code.
I put it into web.xml as follows:

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID">
<display-name>ithelplinereports</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<context-param>
<param-name>db_driver</param-name>
<param-value>oracle.jdbc.driver.OracleDriver</param-value>
<param-name>db_server</param-name>
<param-value>192.168.31.41</param-value>
<param-name>db_port</param-name>
<param-value>1521</param-value>
</context-param>

You're not using "context-param" correctly. Each "context-param"
element only takes a single "param-name" and "param-value" element.
I'm surprised this got past validation. I wouldn't be surprised if
the value of "db_server" is also null, but the value of "db_port" is
not.

Change this to three "context-param" elements, each with a "param-
name" and "param-value" element, and that should get you closer.
 
S

Sameer

Below.




You're not using "context-param" correctly. Each "context-param"
element only takes a single "param-name" and "param-value" element.
I'm surprised this got past validation. I wouldn't be surprised if
the value of "db_server" is also null, but the value of "db_port" is
not.

Change this to three "context-param" elements, each with a "param-
name" and "param-value" element, and that should get you closer.

Thanks!
(I am using Eclipse Europa and it not showing red mark anywhere.)
 
M

Manish Pandit

Thanks!
(I am using Eclipse Europa and it not showing red mark anywhere.)

Did you fix the deployment descriptor and then try it (as David
suggested) ?

Eclipse may not be validating against the DTD, hence you do not see
the red marks.

-cheers,
Manish
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,990
Messages
2,570,211
Members
46,796
Latest member
SteveBreed

Latest Threads

Top