Solved

About InputStream() in servlet

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
login form jsp example 2 74
ejb on wildfly 5 65
Eclipse with various Java releases 7 92
web project error add remove 1 85
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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
Suggested Courses

710 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