Solved

About InputStream() in servlet

Posted on 2002-07-21
4
831 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

The New “Normal” in Modern Enterprise Operations

DevOps for the modern enterprise offers many benefits — increased agility, productivity, and more, but digital transformation isn’t easy, especially if you’re not addressing the right issues. Register for the webinar to dive into the “new normal” for enterprise modern ops.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 17 53
Tomcat: Unable to run tomcat service. 2 24
What's wrong with this code? 4 26
ejb entity bean example 2 17
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…

830 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