Load Java Properties File using JSP

R

RigasMinho

I'm trying to load a java properties file and then read from that file.
But each time I try this it returns the value of Null.

Any Ideas?
String osname;
Properties prop = System.getProperties();
osname = prop.getProperty("os.name");

iff (osname.equals("Windows XP"))
{

prop.getClass().getResourceAsStream("Administration.properties");

}
strServername = prop.getProperty("servername");
%>
This is my JSP page. <%= strServername %><br>

----------
So in the properties file there's a line that says:
"servername=10.10.10.10"

It technically should load up the ip address on the web app page. The
properties file is setup right.

I'm using Tomcat right now.
 
T

Tim B

RigasMinho said:
I'm trying to load a java properties file and then read from that file.
But each time I try this it returns the value of Null.

Any Ideas?
String osname;
Properties prop = System.getProperties();
osname = prop.getProperty("os.name");

iff (osname.equals("Windows XP"))
{

prop.getClass().getResourceAsStream("Administration.properties");

}
strServername = prop.getProperty("servername");
%>
This is my JSP page. <%= strServername %><br>

----------
So in the properties file there's a line that says:
"servername=10.10.10.10"

It technically should load up the ip address on the web app page. The
properties file is setup right.

I'm using Tomcat right now.

There are a number of ways to do this, but the following works for me on
OC4J stand-alone:

URL
myURL=application.getResource("/WEB-INF/Administration.properties");
InputStream in = myURL.openStream();
Properties p = new Properties();
p.load( in );
out.println( p.getProperty("servername") );
 
R

RigasMinho

Gives me an error of:
Syntax error, insert "AssignmentOperator Expression" to complete
Expression
this is for teh variable URL.

I've got it to work in a java core file but cant figure out how to do
this in jsp.
 
A

Andrew Thompson

RigasMinho wrote:

Please refrain from top-posting. It makes discussions hard
to understand.
Gives me an error of:
Syntax error, insert "AssignmentOperator Expression" to complete
Expression
this is for teh variable URL.

URL is a class, myURL is a variable.

You may have missed the '='. If not, please be more clear.
I've got it to work in a java core file but cant figure out how to do
this in jsp.

That is always a good strategy, to do it in 'core Java'
(where possible) before putting it into something like
an applet or web-service, where it is more hassle to
deploy, and harder to debug.

Andrew T.
 
R

RigasMinho

Okay - is this what you're telling me to write?
public class URL
{

myURL=application.getResource("/WEB-INF/Administration.properties");
InputStream in = myURL.openStream();
Properties p = new Properties();
p.load( in );
out.println( p.getProperty("servername") );
}

even that gives me errors:
- Syntax error on token "myURL", VariableDeclaratorId expected after
this token
- Syntax error on token(s), misplaced construct(s)

I guess i'm not understanding this too well.

I wrote my own java code that loads the properties but when i copy it
over to a jsp file it just doesnt work.
 
R

RigasMinho

Okay - is this what you're telling me to write?
public class URL
{

myURL=application.getResource("/WEB-INF/Administration.properties");
InputStream in = myURL.openStream();
Properties p = new Properties();
p.load( in );
out.println( p.getProperty("servername") );
}

even that gives me errors:
- Syntax error on token "myURL", VariableDeclaratorId expected after
this token
- Syntax error on token(s), misplaced construct(s)

I guess i'm not understanding this too well.

I wrote my own java code that loads the properties but when i copy it
over to a jsp file it just doesnt work.
 
T

Tim B

RigasMinho said:
Okay - is this what you're telling me to write?
public class URL
{

myURL=application.getResource("/WEB-INF/Administration.properties");
InputStream in = myURL.openStream();
Properties p = new Properties();
p.load( in );
out.println( p.getProperty("servername") );
}

No. The first line wrapped just after "URL", unfortunately

what you want is:
<%
URL myURL=application.getResource("/WEB-INF/myfile.properties");
InputStream in = myURL.openStream();
Properties p = new Properties();
p.load( in );
out.println( p.getProperty("servername") );
%>

and you'll need an import in the jsp - java.net.URL, I think.
 
R

RigasMinho

Holy - crud it works.

Thanks - its weird the below code is the core java i wrote which works.

But somehow doesnt work in JSP. Gotta learn JSP better.

Here's a question - the code you wrote for the JSP properties loader -
the properties file would be stored in the project workspace right?

And not under the tomcat workspace deployed place right?


import java.util.Properties;
import java.io.*;
import java.*;

class Prop{


public static void main(String args[])
{

String strServername;
String myURL;
String osname;
Properties prop = System.getProperties();
osname = prop.getProperty("os.name");
if (osname.equals("Windows XP"))
{

try
{

Properties defaultProps = new Properties();
FileInputStream in = new
FileInputStream("PasswordAdministration.properties");
defaultProps.load(in);
in.close();
strServername = defaultProps.getProperty("portnumber");
System.out.println(strServername);
}
catch(IOException e)
{
String g;
g="error";

}
}


}
}
 

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,982
Messages
2,570,190
Members
46,736
Latest member
zacharyharris

Latest Threads

Top