Solved

Servlet, change URL parameters

Posted on 2007-11-28
1
2,532 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
groupSum5 challenge 5 68
Java array passed to SQL where clause 23 61
Problem to start Neon 20 83
Java Options in Zimbra Server 2 44
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:

932 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now