• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 923
  • Last Modified:

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.
1 Solution
why dont you just proxy the request for the client? just connect to the resource and write back directly to the ServetOutputStream.
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)

               URL url = new URL("http://www.experts-exchange.com");
               HttpURLConnection conn = (HttpURLConnection) url.openConnection();

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

               String line;
               while ((line = reader.readLine()) != null)
          catch (IOException e)
thusharaAuthor Commented:
Thank u very much.

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now