Hi,
I was trying out variants on the property file reading inside a
servlet. I m facing a problem.
The code given below in which InputStream is a private memeber variable
shows the value of key only once, later invocation of the same servlet
shows the value as null. ( CODE 1 ).
But when I run the CODE 2, everything works fine and I get the proper
value of Key.
Why is the inputstream stuff in CODE 1 not working. I m pretty new to
servlets.
------------------------ CODE 1 -------------------
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Property extends HttpServlet {
public void init( ServletConfig config ) throws ServletException {
super.init( config );
try {
is =
config.getServletContext().getResourceAsStream("/WEB-INF/test.properties");
} catch ( Exception ie ) {
ie.printStackTrace();
}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println( "Hello...." );
Properties prop = new Properties();
prop.load( is );
is.close();
System.out.println( prop.get( "Key" ) );
}
InputStream is;
}
------------------------ CODE 2 -------------------
import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class Property extends HttpServlet {
public void init( ServletConfig config ) throws ServletException {
super.init( config );
prop = new Properties();
try {
InputStream is =
config.getServletContext().getResourceAsStream("/WEB-INF/test.properties");
prop.load( is );
is.close();
} catch ( IOException ie ) {
ie.printStackTrace();
}
}
public void doGet(HttpServletRequest request,
HttpServletResponse response)
throws IOException, ServletException {
response.setContentType("text/plain");
PrintWriter out = response.getWriter();
out.println( "Hello...." );
System.out.println( prop.get( "Key" ) );
}
Properties prop;
}
Andrea Desole - 29 Sep 2005 12:09 GMT
> Hi,
> I was trying out variants on the property file reading inside a
[quoted text clipped - 9 lines]
> Why is the inputstream stuff in CODE 1 not working. I m pretty new to
> servlets.
you should look at how servlet instances work. In the default case you
have only one instance of the servlet per application. When the servlet
is created init is called, and then (more times) the service method,
which calls doGet().
Therefore, your init is called only once.
Yogee - 30 Sep 2005 06:04 GMT
That is clear to me.
But why do I loose control of the InputStream, is it because property
constructor consumes it at the first instance and then on successive
calls no data is available. Dont know exactly wether its a correct
guess.
What say.
Andrea Desole - 30 Sep 2005 09:03 GMT
> That is clear to me.
>
> But why do I loose control of the InputStream, is it because property
> constructor consumes it at the first instance and then on successive
> calls no data is available. Dont know exactly wether its a correct
> guess.
If I understand what you mean yes, it is a correct guess.
But it's not related to servlet programming.
Yogee - 30 Sep 2005 11:29 GMT
Yeah I checked that out, u r correct.
Yogee - 30 Sep 2005 11:31 GMT
I have one more question, What exactly a context means ( in terms of it
being shared among different servlets ). Does it define the
ServletContext that one get from ServletConfig class.
Andrea Desole - 30 Sep 2005 11:44 GMT
> I have one more question, What exactly a context means ( in terms of it
> being shared among different servlets ). Does it define the
> ServletContext that one get from ServletConfig class.
without making confusion with a page context, basically yes. The
documentation for ServletContext has a good explanation:
There is one context per "web application" per Java Virtual Machine. (A
"web application" is a collection of servlets and content installed
under a specific subset of the server's URL namespace such as /catalog
and possibly installed via a .war file.)