3

How to send data (form data) from a Swing to the servlet. Is it as simple as specifying in action attribute as in html(form)?

I am passing data to Servlet using this code:

URL url = new URL(targetURL); 
connection = (HttpURLConnection)url.openConnection();
connection.setRequestMethod("POST");

What is that I have to pass to url i.e in place of targetURL (do I need to mention Servlet location) so that I can retrieve values in the servlet?

yatskevich
  • 2,021
  • 16
  • 25
user1168647
  • 247
  • 1
  • 3
  • 11

2 Answers2

1

You need to use HttpUrlConnection in order to communicate using HttpRequest

See

Community
  • 1
  • 1
jmj
  • 225,392
  • 41
  • 383
  • 426
  • url = new URL(targetURL); connection = (HttpURLConnection)url.openConnection(); connection.setRequestMethod("POST"); connection.setRequestProperty("Content-Type", "application/x-www-form-urlencoded");I have seen this code in some link,targeturl=target o the servlet-is it correct?? Do we need to set all the connection parameters like setreqmethod,set req property??? – user1168647 Feb 01 '12 at 04:24
1

It works just the same as Java Applets, you can find more information here: http://www.coderanch.com/how-to/java/AppletsFaq#servlet

I'd highly reccommend BalusC's introduction to Servlet's and DAO's here as well.

blong
  • 2,700
  • 6
  • 35
  • 97