Solved

Servlet, change URL parameters

Posted on 2007-11-28
1
2,536 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

[Webinar] How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

Question has a verified solution.

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

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
In this post we will learn different types of Android Layout and some basics of an Android App.
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This video teaches viewers about errors in exception handling.

724 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