Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Load Java Properties File using JSP

Reply
Thread Tools

Load Java Properties File using JSP

 
 
RigasMinho
Guest
Posts: n/a
 
      09-01-2006
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("Administratio n.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.

 
Reply With Quote
 
 
 
 
Tim B
Guest
Posts: n/a
 
      09-04-2006

"RigasMinho" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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("Administratio n.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") );


 
Reply With Quote
 
 
 
 
RigasMinho
Guest
Posts: n/a
 
      09-05-2006
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.
Tim B wrote:
> "RigasMinho" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> > 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("Administratio n.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") );


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      09-05-2006
RigasMinho wrote:

Please refrain from top-posting. It makes discussions hard
to understand.

> Tim B wrote:
> > "RigasMinho" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) ups.com...
> > > 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?

....
> > 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");


> 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.

 
Reply With Quote
 
Ed
Guest
Posts: n/a
 
      09-05-2006

Andrew Thompson skrev:

>
>
> Andrew T.


Andrew, how's the head today?

..ed

--
www.EdmundKirwan.com - Home of The Fractal Class Composition.

Download Fractality, free Java code analyzer:
http://www.EdmundKirwan.com/servlet/...c-page130.html

 
Reply With Quote
 
RigasMinho
Guest
Posts: n/a
 
      09-05-2006
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.

Ed wrote:
> Andrew Thompson skrev:
>
> >
> >
> > Andrew T.

>
> Andrew, how's the head today?
>
> .ed
>
> --
> www.EdmundKirwan.com - Home of The Fractal Class Composition.
>
> Download Fractality, free Java code analyzer:
> http://www.EdmundKirwan.com/servlet/...c-page130.html


 
Reply With Quote
 
RigasMinho
Guest
Posts: n/a
 
      09-05-2006
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.

Ed wrote:
> Andrew Thompson skrev:
>
> >
> >
> > Andrew T.

>
> Andrew, how's the head today?
>
> .ed
>
> --
> www.EdmundKirwan.com - Home of The Fractal Class Composition.
>
> Download Fractality, free Java code analyzer:
> http://www.EdmundKirwan.com/servlet/...c-page130.html


 
Reply With Quote
 
Andrew Thompson
Guest
Posts: n/a
 
      09-05-2006
Ed wrote:
> Andrew Thompson skrev:

....
> > Andrew T.

>
> Andrew, how's the head today?


Not great. But I feel a lot better, than I deserve to.

Andrew T.

 
Reply With Quote
 
Tim B
Guest
Posts: n/a
 
      09-05-2006

"RigasMinho" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
> 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.


 
Reply With Quote
 
RigasMinho
Guest
Posts: n/a
 
      09-05-2006
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";

}
}


}
}

Andrew Thompson wrote:
> Ed wrote:
> > Andrew Thompson skrev:

> ...
> > > Andrew T.

> >
> > Andrew, how's the head today?

>
> Not great. But I feel a lot better, than I deserve to.
>
> Andrew T.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
JSP Properties File Load In / Websphere RigasMinho Java 8 03-01-2007 03:15 PM
Declaring a variable in one JSP file and accessing it in another JSP file anubhakhurana@gmail.com Java 8 10-11-2006 01:15 AM
[JSP] difference between jsp:forward and jsp:include alexjaquet@gmail.com Java 0 06-02-2006 01:21 PM
Unable to load Properties file from JAR file Michael Java 1 02-25-2004 12:56 PM
Problem with xml.load & xsl.load in JSP David Shen Javascript 0 08-28-2003 06:12 PM



Advertisments