How do I capture a request and dispatch the exact request (with all the parameters received) to another URL?

David Garcia



As far as i know it depends on the location of the next target url.

  • If the next servlet url is in the same host, then you can use the forward method.
    Here is an example code about using forward:
       RequestDispatcher rd = null;
       String targetURL = "target_servlet_name";
       ServletContext ctx = this.getServletContext();
       rd = ctx.getRequestDispatcher(targetURL);
       rd.forward(request, response);

  • The other possibility is that the target servlet location is in a different host. Then you will need to use Streams,Sockets... and the code could be something like this:
        URL url=null;
        URLConnection conn=null;
          url = new URL
          // the requestParam contains all the attributes (name=value pairs) you want to send taken from the incomming request
          conn = url.openConnection();
        } catch(MalformedURLException e) {
           // whatever you want
        BufferedReader inStream=new BufferedReader(new InputStreamReader(conn.getInputStream()));
        String respuesta=inStream.readLine();

You can read more about the first approach in the Java Developer Journal (Number of May 2000 page 102).

Test Conditions
I have test this approach over webLogic 451 with the Service Pack 8 or higher. I can asure you that with Service Pack 7 or lower, the forward approach doesn't work.

[That's all well and good, but you've dodged an important part of the answer: How do we grab the values of the parameters? I have source code that does this, but unfortunately my hard drive just crashed -- can someone else fill this in? -Alex]