servletContext inputstream problem

Y

Yogee

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;
}
 
A

Andrea Desole

Yogee said:
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.

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.
 
Y

Yogee

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.
 
A

Andrea Desole

Yogee said:
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.
 
Y

Yogee

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.
 
A

Andrea Desole

Yogee said:
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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top