About InputStream() in servlet

I'm writing a program using Applet and servlet. I want my applet send an object to one servlet(say servlet1) and wait for reply. Then the applet sends another http text message to another servlet(say servlet2) depending on the reply from the previous servlet(servlet1). I do so by the following code. However, it doesn't work.

String line=null;
log("sendAndRecvServlet1 connecting...");
URL url=new URL(""+pac.getTemplateName());
URLConnection con=url.openConnection();
log("after URLConnection");
InputStream inputStream = con.getInputStream();
log("b4 inputStreamReader");
InputStreamReader inputStreamReader=new InputStreamReader(inputStream);

BufferedReader in = new BufferedReader(inputStreamReader);
catch(Exception e7){

The above code for sending a http text message to servlet2. I have closed the inputStream for connection of the first servlet(servlet1) b4 running the above code fragment.
It flows exception immediately after URLConnection. It seems that it cannot get the input stream.

Any ideas?

Thanks a lot...^^

Who is Participating?
gandalf94305Connect With a Mentor Commented:
Or, instead of writing log("e7"), write log("e7 " + e7);
Dear Sherina,
  it would really be helpful if you could disclose the nature of exception. My suspicion is that either the URL points to a server which is not running (Connection refused) or the template name you use as an argument does not represent valid URL syntax. Use java.net.URLEncoder.encode(pac.getTemplateName()) to be sure the syntax is correct.

What you ought to do is to overload the log() method so that it can accept an exception as a parameter. You don't supply any details on this - can you do so?
sherinaAuthor Commented:
i find the bug. Thanks a lot. I forgot to add info about this servlet in web.xml in Tomcat.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.