Link to home
Start Free TrialLog in
Avatar of kmapper
kmapper

asked on

Servlet, change URL parameters

Hi,
I'm connection to a URL in a servlet (check the code attached).

I wonder if there is any way to make multiple connection to the same URL and to pass different request parameters ... without creating a new connection.

In my example, I want to change what I write in osw.write ....

thanks
private static void connect (boolean GET_METHOD) throws Exception
    {
        String parameters="firstName=Paz";
        String urlString="http://localhost:8080/Research/SimpleServlet?";
 
        if (GET_METHOD)
            urlString += parameters;
 
 
        URL url = new URL (urlString);
        HttpURLConnection conn = (HttpURLConnection) url.openConnection();
        conn.setAllowUserInteraction (true);
        conn.setDoOutput(true);
 
        if (!GET_METHOD)
        {
            OutputStream osw = (OutputStream) conn.getOutputStream();
            osw.write (parameters.getBytes());
        }
        BufferedReader respContent = new BufferedReader(new InputStreamReader(conn.getInputStream()));
        PrintWriter display = new PrintWriter(System.out);
        String respLine;
        while( (respLine = respContent.readLine()) != null)
        {
            display.println(respLine);
        }
        display.flush();
        respContent.close();
        conn.disconnect();
    }

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Kuldeepchaturvedi
Kuldeepchaturvedi
Flag of United States of America image

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