Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / September 2005

Tip: Looking for answers? Try searching our database.

servletContext inputstream problem

Thread view: 
Yogee - 29 Sep 2005 10:26 GMT
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.)


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.