How to include a file with connection parameters in JSP

P

Per Magnus L?vold

Hi, all!
I have a series of JSP pages wich connect to an Oracle DB using JDBC.
Several of these pages use the same connection string for JDBC, and
now that the connection parameters change, I have to uådate every one
of them.
So now I am trying to set the parameters in an included file, so I
only have to keep one file up-to-date.
I've searched the web and newsgroups, and found several tips, but none
have worked for me. One specific I am trying to make work is this:

*** file with parameters (test.jsp) ***
<%
request.setAttribute("test", "Display this");
%>
*** end file w params ***

*** main file (main.jsp) ***
<html>
<head></head>
<body>
<%@include file="test.jsp"%>
The test parameter is: <%= test %>.<br>
</body>
</html>
*** end main file ***

This won't compile. I get the error message
"variable test not found in class _main"

I can't see why this shouldn't work. Any ideas how to make this work?

Regards, Per Magnus
 
R

Ryan Stewart

Per Magnus L?vold said:
Hi, all!
I have a series of JSP pages wich connect to an Oracle DB using JDBC.
Several of these pages use the same connection string for JDBC, and
now that the connection parameters change, I have to uådate every one
of them.
So now I am trying to set the parameters in an included file, so I
only have to keep one file up-to-date.
I've searched the web and newsgroups, and found several tips, but none
have worked for me. One specific I am trying to make work is this:

*** file with parameters (test.jsp) ***
<%
request.setAttribute("test", "Display this");
%>
*** end file w params ***

*** main file (main.jsp) ***
<html>
<head></head>
<body>
<%@include file="test.jsp"%>
The test parameter is: <%= test %>.<br>
</body>
</html>
*** end main file ***

This won't compile. I get the error message
"variable test not found in class _main"

I can't see why this shouldn't work. Any ideas how to make this work?

Regards, Per Magnus

Yes:
<%= request.getAttribute("test") %>

A much better way would be to create context parameters in web.xml to hold
your information:
<context-param>
<param-name>name</param-name>
<param-value>value</param-value>
</context-param>

Then:
<%= application.getInitParameter("name") %>

You could also put your data in a .properties file. See
java.util.Properties.
 
P

Per Magnus L?vold

Thank you!
This is working fine with request.getAttribute() :)

I'm curious why putting the parameter in web.xml is a much better
solution?
During testing I have seen that I do not even need the web.xml file
for running the application. I thought this file was used for
overriding default server parameters or something.

Regrads, Per Magnus
 

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,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top