Search code examples
javaservletsurlconnection

Calling servlet from another servlet


I have two servlets which are running on different tomcat servers.

I and trying to call a servlet1 from servlet2 in the following way and wanted to write an object to output stream.

URL url=new URL("http://msyserver/abc/servlet1");
URLConnection con=url.openConnection();
con.setDoOutput(true);
con.setDoInput(true);
OutputStream os=con.getOutputStream();
ObjectOutputStream oos=new ObjectOutputStream(os);
oos.writeObject(pushEmailDTO);
oos.flush();
oos.close();

The problem is that i am unable to hit the servlet? I cannot figure out what i am missing.


Solution

  • I cannot unnderstand but it worked by adding the following line in the code.

    con.getExpiration();
    

    like this

    URL url=new URL("http://msyserver/abc/servlet1");
    URLConnection con=url.openConnection();
    con.setDoOutput(true);
    con.setDoInput(true);
    con.getExpiration();//<----------
    OutputStream os=con.getOutputStream();
    ObjectOutputStream oos=new ObjectOutputStream(os);
    oos.writeObject(pushEmailDTO);
    oos.flush();
    oos.close();