Solved

Servlet, change URL parameters

Posted on 2007-11-28
1
2,534 Views
Last Modified: 2013-11-24
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

0
Comment
Question by:kmapper
1 Comment
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 500 total points
ID: 20415404
HTTP Protocol is a stateless protocol. so once the communication is ended. it is ended. if you try to write again on the same streams, you will get illegalstateexception.

if you want to send something else on a second run, you will have to start from making a new connection.
0

Featured Post

How Do You Stack Up Against Your Peers?

With today’s modern enterprise so dependent on digital infrastructures, the impact of major incidents has increased dramatically. Grab the report now to gain insight into how your organization ranks against your peers and learn best-in-class strategies to resolve incidents.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
numbers ascending pyramid 101 212
hibernate insert example 13 34
iterator/ListIterator approach 17 39
junit as external jar or library 7 22
An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question