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 2006

Tip: Looking for answers? Try searching our database.

Load Java Properties File using JSP

Thread view: 
RigasMinho - 01 Sep 2006 14:47 GMT
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.
Tim B - 04 Sep 2006 18:43 GMT
> 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.
[quoted text clipped - 22 lines]
>
> 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") );
RigasMinho - 05 Sep 2006 14:05 GMT
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.
> > 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.
[quoted text clipped - 32 lines]
>       p.load( in );
>       out.println( p.getProperty("servername") );
Andrew Thompson - 05 Sep 2006 14:28 GMT
Please refrain from top-posting.  It makes discussions hard
to understand.

> > > 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.
Ed - 05 Sep 2006 14:56 GMT
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:
www.EdmundKirwan.com/servlet/fractal/frac-page130.html
RigasMinho - 05 Sep 2006 15:42 GMT
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.

> Andrew Thompson skrev:
>
[quoted text clipped - 9 lines]
> Download Fractality, free Java code analyzer:
> www.EdmundKirwan.com/servlet/fractal/frac-page130.html
RigasMinho - 05 Sep 2006 15:43 GMT
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.

> Andrew Thompson skrev:
>
[quoted text clipped - 9 lines]
> Download Fractality, free Java code analyzer:
> www.EdmundKirwan.com/servlet/fractal/frac-page130.html
Tim B - 05 Sep 2006 16:21 GMT
> Okay - is this what you're telling me to write?
> public class URL
[quoted text clipped - 6 lines]
>       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.
Andrew Thompson - 05 Sep 2006 16:20 GMT
> 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.
RigasMinho - 05 Sep 2006 16:43 GMT
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 skrev:
> ...
[quoted text clipped - 5 lines]
>
> Andrew T.


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



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