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
zero - 27 Dec 2005 18:43 GMT
> Hi,
>
[quoted text clipped - 11 lines]
>
> 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()?

Signature
Beware the False Authority Syndrome
subi - 28 Dec 2005 05:26 GMT
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
Raymond DeCampo - 01 Jan 2006 01:46 GMT
> Hi,
>
[quoted text clipped - 49 lines]
> 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

Signature
XML is the programmer's duct tape.