Solved

About InputStream() in servlet

Posted on 2002-07-21
4
822 Views
Last Modified: 2013-11-24
Hi,
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;
         
try{
log("sendAndRecvServlet1 connecting...");
URL url=new URL("http://127.0.0.1:8080/fyp/ErrChk1Servlet?templateName="+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);
while(true){
     line=in.readLine();
     if(line==null){
          break;
     }
     log(line);
}
in.close();
}
catch(Exception e7){
     log("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...^^

Sherina
0
Comment
Question by:sherina
  • 2
4 Comments
 
LVL 3

Expert Comment

by:gandalf94305
ID: 7167587
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.

Cheers,
--gandalf.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 7167589
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?
0
 
LVL 3

Accepted Solution

by:
gandalf94305 earned 75 total points
ID: 7167592
Or, instead of writing log("e7"), write log("e7 " + e7);
0
 

Author Comment

by:sherina
ID: 7183069
i find the bug. Thanks a lot. I forgot to add info about this servlet in web.xml in Tomcat.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now