Load Java Properties File using JSP

Discussion in 'Java' started by RigasMinho, Sep 1, 2006.

  1. RigasMinho

    RigasMinho Guest

    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.
    RigasMinho, Sep 1, 2006
    #1
    1. Advertising

  2. RigasMinho

    Tim B Guest

    "RigasMinho" <> wrote in message
    news:...
    > 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") );
    Tim B, Sep 4, 2006
    #2
    1. Advertising

  3. RigasMinho

    RigasMinho Guest

    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" <> wrote in message
    > news:...
    > > 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") );
    RigasMinho, Sep 5, 2006
    #3
  4. RigasMinho wrote:

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

    > Tim B wrote:
    > > "RigasMinho" <> wrote in message
    > > news:...
    > > > 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.
    Andrew Thompson, Sep 5, 2006
    #4
  5. RigasMinho

    Ed Guest

    Ed, Sep 5, 2006
    #5
  6. RigasMinho

    RigasMinho Guest

    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:
    > www.EdmundKirwan.com/servlet/fractal/frac-page130.html
    RigasMinho, Sep 5, 2006
    #6
  7. RigasMinho

    RigasMinho Guest

    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:
    > www.EdmundKirwan.com/servlet/fractal/frac-page130.html
    RigasMinho, Sep 5, 2006
    #7
  8. 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.
    Andrew Thompson, Sep 5, 2006
    #8
  9. RigasMinho

    Tim B Guest

    "RigasMinho" <> wrote in message
    news:...
    > 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.
    Tim B, Sep 5, 2006
    #9
  10. RigasMinho

    RigasMinho Guest

    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.
    RigasMinho, Sep 5, 2006
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael
    Replies:
    1
    Views:
    13,964
    Andrew Thompson
    Feb 25, 2004
  2. Replies:
    0
    Views:
    4,354
  3. Replies:
    8
    Views:
    3,458
    Manish Pandit
    Oct 11, 2006
  4. RigasMinho
    Replies:
    8
    Views:
    6,032
    RigasMinho
    Mar 1, 2007
  5. David Shen

    Problem with xml.load & xsl.load in JSP

    David Shen, Aug 28, 2003, in forum: Javascript
    Replies:
    0
    Views:
    115
    David Shen
    Aug 28, 2003
Loading...

Share This Page