not printed

Discussion in 'Java' started by subi, Dec 27, 2005.

  1. subi

    subi Guest

    Hi,

    I am using property file to get a rate revision date. When I first use
    the
    getServletConfig().getServletContext().getRealPath("ratecheck.properties"),
    i got the non-static method ServletConfig() can not be used in static
    context. So I created one more method getPath() and used a member
    method got it solved.

    But I am still not able to get the RATEREVISION printed (im printing
    the RATEREVISION below after some lines of other codings). It displays
    nothing even an exception. I have both the the servlet and properties
    file in same folder.

    Please let me have a solution.

    class test extends HttpServlet
    {
    String getPath()
    {
    String
    propFile=getServletConfig().getServletContext().getRealPath("ratecheck.properties");
    return propFile;
    }

    static String rateFinder()
    {
    String rate="";

    try
    {
    Properties prop= new Properties();
    String propFile=new test().getPath();

    File file = new File("ratecheck.properties");
    prop.load(new FileInputStream(propFile));

    rate=prop.getProperty("rate-revision");

    }
    catch (Exception e)
    {
    rate=e.getMessage();
    }
    return rate;
    }

    /*
    ...some codings..


    ..*/
    public final static String RATEREVISION = rateFinder();
    }

    Thank you,
    Subi
     
    subi, Dec 27, 2005
    #1
    1. Advertising

  2. subi

    zero Guest

    "subi" <> wrote in
    news::

    > Hi,
    >
    > I am using property file to get a rate revision date. When I first use
    > the
    > getServletConfig().getServletContext().getRealPath("ratecheck.propertie
    > s"), i got the non-static method ServletConfig() can not be used in
    > static context. So I created one more method getPath() and used a
    > member method got it solved.
    >
    > But I am still not able to get the RATEREVISION printed (im printing
    > the RATEREVISION below after some lines of other codings). It displays
    > nothing even an exception. I have both the the servlet and properties
    > file in same folder.
    >
    > Please let me have a solution.
    >


    <code snipped>

    The following is a (non-servlet) test I wrote based on your code:

    public class TestStatic
    {
    public static final String STR = getStr();

    String getPath()
    {
    return "test";
    }

    static String getStr()
    {
    return new TestStatic().getPath();
    }

    public static void main(String args[])
    {
    System.out.println(STR);
    }
    }

    This works fine. So, the problem probably is not with the code you
    showed. RATEREVISION should indeed be assigned correctly (although I
    question the decision of creating an instance of a class to assign a
    static final member variable).

    I'm guessing the problem is in the code that prints this variable. Or,
    possibly it's printing correctly but not to the stream you're expecting.
    Are you printing to HttpServletResponse:getWriter()?

    --
    Beware the False Authority Syndrome
     
    zero, Dec 27, 2005
    #2
    1. Advertising

  3. subi

    subi Guest

    Yes I am using HttpServletResponse. But I have to check as you said it
    could be printing in different stream. Let me check it..

    Thank you for your kind reply.

    Thanks,
    Subi
     
    subi, Dec 28, 2005
    #3
  4. subi wrote:
    > Hi,
    >
    > I am using property file to get a rate revision date. When I first use
    > the
    > getServletConfig().getServletContext().getRealPath("ratecheck.properties"),
    > i got the non-static method ServletConfig() can not be used in static
    > context. So I created one more method getPath() and used a member
    > method got it solved.
    >
    > But I am still not able to get the RATEREVISION printed (im printing
    > the RATEREVISION below after some lines of other codings). It displays
    > nothing even an exception. I have both the the servlet and properties
    > file in same folder.
    >
    > Please let me have a solution.
    >
    > class test extends HttpServlet
    > {
    > String getPath()
    > {
    > String
    > propFile=getServletConfig().getServletContext().getRealPath("ratecheck.properties");
    > return propFile;
    > }
    >
    > static String rateFinder()
    > {
    > String rate="";
    >
    > try
    > {
    > Properties prop= new Properties();
    > String propFile=new test().getPath();
    >
    > File file = new File("ratecheck.properties");
    > prop.load(new FileInputStream(propFile));
    >
    > rate=prop.getProperty("rate-revision");
    >
    > }
    > catch (Exception e)
    > {
    > rate=e.getMessage();
    > }
    > return rate;
    > }
    >
    > /*
    > ...some codings..
    >
    >
    > ..*/
    > public final static String RATEREVISION = rateFinder();
    > }
    >


    You can't just instantiate your servlet class and expect that
    getServletConfig() will work correctly. Why is rateFinder() static?

    HTH,
    Ray

    --
    XML is the programmer's duct tape.
     
    Raymond DeCampo, Jan 1, 2006
    #4
    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. Replies:
    8
    Views:
    501
    Toby Inkster
    Aug 7, 2006
  2. lds
    Replies:
    1
    Views:
    288
  3. Jean Luc Truchtersheim
    Replies:
    5
    Views:
    352
    James Mills
    Sep 17, 2010
  4. Karuna
    Replies:
    0
    Views:
    73
    Karuna
    Sep 27, 2004
  5. Replies:
    2
    Views:
    155
    Peter J. Acklam
    Jul 6, 2004
Loading...

Share This Page