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