How to call a servlet from a separate Java Application?

Posted on 2005-04-07
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:
                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.setRequestProperty("Content-Type", "text/html");
                 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());

during the run, I get error message as below: 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!


Question by:yi02
    LVL 92

    Accepted Solution

    > 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();


    Author Comment

    dear objects:

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

    best regards,

    LVL 92

    Expert Comment

    glad I could help :)

    Featured Post

    What Should I Do With This Threat Intelligence?

    Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

    Join & Write a Comment

    Suggested Solutions

    Title # Comments Views Activity
    wordsWithout 49 64
    sumHeights  challenge 17 52
    ArrayLists in ArrayLists/HashMaps? 4 30
    wordappend challenge 8 56
    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…
    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 will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
    This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

    746 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

    19 Experts available now in Live!

    Get 1:1 Help Now