Link to home
Start Free TrialLog in
Avatar of thushara
thushara

asked on

simulating browser in java application

how can I simulate the client browser event in my servlet? My need is while I'm having a session with the client, sending a http request to a different app server(without knowing to the client) and getting it's response back to my servlet and proceed with the client session. Since response.sendRedirect(url) method uses the request via client browser the response is sent back to the client. I don't want this to happen. example code is much appreciated.
thanx in advance.
ASKER CERTIFIED SOLUTION
Avatar of functionpointer
functionpointer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of msterjev
msterjev

This is simple:

Use HttpURLConnection to the other site.
Set Contet Type of the ServletResponse the same as the HttpURLConnection Content-Type.
Read everything from the HttpURLConnection'r input stream and write it to the ServletResponse's output stream!

I think you can this  yourself!
you want code........

import java.io.*;
import java.net.*;

public class URLConnection
{

     public static void main(String[] args)
     {

          try
          {
               URL url = new URL("https://www.experts-exchange.com");
               HttpURLConnection conn = (HttpURLConnection) url.openConnection();
               conn.connect();

               StringBuffer contentsBuffer = new StringBuffer();
               BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));

               String line;
               while ((line = reader.readLine()) != null)
               {
                    System.out.println(line);
               }
          }
          catch (IOException e)
          {
               System.out.println(e);
          }
     }
}
Avatar of thushara

ASKER

Thank u very much.