Output streaming of a servlet

Discussion in 'Java' started by anu, Mar 14, 2007.

  1. anu

    anu Guest

    Hi all,
    I am doing a web application.in that i have comnnected from a java
    class to the servlet using URLConnection APIs.The problem now i am
    facing is i need to return some values from the servlet to the calling
    javaclass.how can i do this?
    I have used the following code.
    In the javaclass...

    String location = "http://localhost:8080/Scroll_Sample/ScrollServlet?
    param1="+username+"&param2="+password;

    URL testServlet = new URL(location);

    URLConnection servletConnection =
    testServlet.openConnection();
    InputStream instr = servletConnection.getInputStream();

    ObjectInputStream inputFromServlet = new ObjectInputStream(instr);

    String name1 = (String)inputFromServlet.readObject();

    System.out.println("name is "+name1);

    System.out.println("contenttype is
    "+servletConnection.getContentType());

    System.out.println("within end of try");
    And in the servlet............................

    response.setContentType("application/x-java-serialized-object");
    OutputStream outstr = response.getOutputStream();
    ObjectOutputStream oos = new ObjectOutputStream(outstr);
    String name1="anu";
    oos.writeObject(name1);

    The exception i have got is
    Within try
    Exception isjava.io.IOException: Server returned HTTP response code:
    500 for URL: http://localhost:8080/Scroll_Sample/ScrollServlet?param1=anu&param2=anu
    java.io.IOException: Server returned HTTP response code: 500 for URL:
    http://localhost:8080/Scroll_Sample/ScrollServlet?param1=anu&param2=anu
    at
    sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:
    1149)
    at ServletConnect.main(ServletConnect.java:61)
    can anyone suggest any idea pls?
    Thanks in Advance.....
    anu, Mar 14, 2007
    #1
    1. Advertising

  2. anu

    ck Guest

    Re: Output streaming of a servlet

    On Mar 14, 12:11 pm, "anu" <> wrote:
    > Hi all,
    > I am doing a web application.in that i have comnnected from a java
    > class to the servlet using URLConnection APIs.The problem now i am
    > facing is i need to return some values from the servlet to the calling
    > javaclass.how can i do this?
    > I have used the following code.
    > In the javaclass...
    >
    > String location = "http://localhost:8080/Scroll_Sample/ScrollServlet?
    > param1="+username+"&param2="+password;
    >
    > URL testServlet = new URL(location);
    >
    > URLConnection servletConnection =
    > testServlet.openConnection();
    > InputStream instr = servletConnection.getInputStream();
    >
    > ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
    >
    > String name1 = (String)inputFromServlet.readObject();
    >
    > System.out.println("name is "+name1);
    >
    > System.out.println("contenttype is
    > "+servletConnection.getContentType());
    >
    > System.out.println("within end of try");
    > And in the servlet............................
    >
    > response.setContentType("application/x-java-serialized-object");
    > OutputStream outstr = response.getOutputStream();
    > ObjectOutputStream oos = new ObjectOutputStream(outstr);
    > String name1="anu";
    > oos.writeObject(name1);
    >
    > The exception i have got is
    > Within try
    > Exception isjava.io.IOException: Server returned HTTP response code:
    > 500 for URL:http://localhost:8080/Scroll_Sample/ScrollServlet?param1=anu&param2=anu
    > java.io.IOException: Server returned HTTP response code: 500 for URL:http://localhost:8080/Scroll_Sample/ScrollServlet?param1=anu&param2=anu
    > at
    > sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection....
    > 1149)
    > at ServletConnect.main(ServletConnect.java:61)
    > can anyone suggest any idea pls?
    > Thanks in Advance.....



    What happens when you simply paste
    http://localhost:8080/Scroll_Sample/ScrollServlet?param1=anu&param2=anu
    in browser?

    Instead of using URL object, in my opinion Commons HTTP client would
    be a better option.

    --
    Ck
    http://www.gfour.net
    ck, Mar 14, 2007
    #2
    1. Advertising

  3. anu wrote:
    > I am doing a web application.in that i have comnnected from a java
    > class to the servlet using URLConnection APIs.The problem now i am
    > facing is i need to return some values from the servlet to the calling
    > javaclass.how can i do this?
    > I have used the following code.
    > In the javaclass...
    >
    > String location = "http://localhost:8080/Scroll_Sample/ScrollServlet?
    > param1="+username+"&param2="+password;
    >
    > URL testServlet = new URL(location);
    >
    > URLConnection servletConnection =
    > testServlet.openConnection();
    > InputStream instr = servletConnection.getInputStream();
    >
    > ObjectInputStream inputFromServlet = new ObjectInputStream(instr);
    >
    > String name1 = (String)inputFromServlet.readObject();
    >
    > System.out.println("name is "+name1);
    >
    > System.out.println("contenttype is
    > "+servletConnection.getContentType());
    >
    > System.out.println("within end of try");
    > And in the servlet............................
    >
    > response.setContentType("application/x-java-serialized-object");
    > OutputStream outstr = response.getOutputStream();
    > ObjectOutputStream oos = new ObjectOutputStream(outstr);
    > String name1="anu";
    > oos.writeObject(name1);
    >
    > The exception i have got is
    > Within try
    > Exception isjava.io.IOException: Server returned HTTP response code:
    > 500 for URL: http://localhost:8080/Scroll_Sample/ScrollServlet?param1=anu&param2=anu
    > java.io.IOException: Server returned HTTP response code: 500 for URL:
    > http://localhost:8080/Scroll_Sample/ScrollServlet?param1=anu&param2=anu
    > at
    > sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:
    > 1149)
    > at ServletConnect.main(ServletConnect.java:61)
    > can anyone suggest any idea pls?


    1) Check the log files and console window for errors that can
    give a hint about why you get a code 500.

    2) I am a bit skeptical about the concept of sending serialized
    Java objects a body of HTTP requests. It should work, but does
    seem a good choice.

    Arne
    =?ISO-8859-1?Q?Arne_Vajh=F8j?=, Mar 14, 2007
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Andrew
    Replies:
    1
    Views:
    5,614
    Andrew
    Oct 5, 2005
  2. Eoin
    Replies:
    0
    Views:
    592
  3. Eoin
    Replies:
    0
    Views:
    385
  4. circuit_breaker
    Replies:
    2
    Views:
    1,989
    Jack Jia
    Apr 4, 2004
  5. Vasil Slavov
    Replies:
    1
    Views:
    763
    Kent Johnson
    Apr 19, 2005
Loading...

Share This Page