[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

How to call a servlet from a separate Java Application?

Posted on 2005-04-07
3
Medium Priority
?
316 Views
Last Modified: 2013-11-24
Hi, Dear all:

I have a stand-alone Java program that needs to call a servlet from a remote URL. The java program need to pass a string parameter during the call and the servlet need return a string back to the Java program.

The part of Java program calling the servlet is shown below:
......
               try{
                URL urlToCall=new URL("http://urladdress/servlet/MyServlet");
                URL url=new URL(urlToCall.toExternalForm() + "?" + URLEncoder.encode("para") +
                "=" + URLEncoder.encode(paraStr));
                 URLConnection con =url.openConnection();
                 con.setDoInput(true);
                  con.setDoOutput(true);
                  con.setRequestProperty("Content-Type", "text/html");
                  con.setUseCaches(false);
                 InputStream in=con.getInputStream();
                 ObjectInputStream objInputStm= new ObjectInputStream(in);
                  String servletData= (String)objInputStm.readObject();
 }catch(Exception e){
                System.out.println("e=" + e);
                System.out.println("e message: " + e.getMessage());
                e.printStackTrace();
                }

during the run, I get error message as below:
e=java.io.StreamCorruptedException: invalid stream header
e message: invalid stream header

Do you know what is the problem here? I test myServlet through brower with the URL and paraStr, which works. So I think it is not the servlet problem. What is wrong with the stream header? Or can you provide sample codes to do this task please?

thanks a lot!

yi02


0
Comment
Question by:yi02
  • 2
3 Comments
 
LVL 92

Accepted Solution

by:
objects earned 1000 total points
ID: 13730525
> ObjectInputStream objInputStm= new ObjectInputStream(in);
> String servletData= (String)objInputStm.readObject();

you need to use a Reader instead of an ObjectInputStream to read the response

BufferedReader rin = new BufferedReader(new InputStreamReader(in));
String servletDate = rin.readLine();

0
 

Author Comment

by:yi02
ID: 13730772
dear objects:

Thanks a lot! It works! Did not realize just this simple!

best regards,

yi02
0
 
LVL 92

Expert Comment

by:objects
ID: 13731821
glad I could help :)
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

Question has a verified solution.

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

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…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
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…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
Suggested Courses
Course of the Month19 days, 8 hours left to enroll

872 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