How do I output to a Servlet?
1 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Tuesday, August 7, 2001 03:13 PM

Hi,



Ok, here's my question. I have read a couple articles on communicating Applet to Servlet. But, they all use GET. I want to POST information to a servlet. How can I give multiple parameters using POST?

Can I have example code please?



THANK YOU SO MUCH!

Re: How do I output to a Servlet?

Posted By:   Christopher_Pickslay  
Posted On:   Tuesday, August 7, 2001 10:31 PM

1) Get a HttpURLConneciton to the servlet using:


String urlString = "http://your-host/your-servlet";
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) (url.openConnection());


2) Set the request method to POST:


conn.setRequestMethod("POST");


3) Create your POST data (POST data looks just like GET data):


String postData;
postData += "field1";
postData += "=";
postData += URLEncoder.encode("value for field1");
postData += "&";
postData += "field2";
postData += "=";
postData += URLEncoder.encode("value for field2");


3) Write your POST data to your HttpURLConnection's OutputStream:


PrintWriter out = new PrintWriter(conn.getOutputStream);
out.print(postData);
out.close();


4) Get your HttpURLConnection's InputStream to read the result:


BufferedReader in = new BufferedReader(new InputStreamReader(conn.getInputStream));
About | Sitemap | Contact