Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

Servlet, change URL parameters

Posted on 2007-11-28
1
Medium Priority
?
2,542 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 2000 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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
Suggested Courses

824 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