Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Sameer - 28 Sep 2007 13:49 GMT
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
shakah - 28 Sep 2007 13:55 GMT
> Dear All,
> I am using Tomcat 6.0 on Windows XP SP2.
[quoted text clipped - 43 lines]
>
> -Sameer

Try application.getInitParamer("db_driver") in your JSP page.
david.karr - 28 Sep 2007 16:41 GMT
Below.

> Dear All,
> I am using Tomcat 6.0 on Windows XP SP2.
[quoted text clipped - 18 lines]
>                 <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.
> </web-app>
>
[quoted text clipped - 21 lines]
>
> -Sameer
Sameer - 29 Sep 2007 06:45 GMT
> Below.
>
[quoted text clipped - 55 lines]
>
> > -Sameer

Thanks!
(I am using Eclipse Europa and it not showing red mark anywhere.)
Manish Pandit - 29 Sep 2007 17:30 GMT
> > Below.
>
[quoted text clipped - 58 lines]
> 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.