Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

About InputStream() in servlet

Posted on 2002-07-21
4
Medium Priority
?
837 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 300 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

Application Discovery Service in AWS

In the era of the cloud, customers migrating away from their existing on-premise infrastructure. This requires lots of planning, strategies, and effort to identify their existing resources and determine how best to migrate.  Datacenter migrations happen in four phases -

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

688 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