0

I have to check the login credentials in my swing application at the client side which is invoked by an executable jar file. Once the details are filled,it check in the servlet in the database. My servlet is working fine.

How to connect a Swing application (client) to a servlet?

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Ninz
  • 349
  • 5
  • 13
  • 1
    Possible Duplicate: http://stackoverflow.com/questions/9090580/sending-data-from-swing-to-servlet – christopher May 13 '13 at 10:48
  • u take the input from textfield , make a HTTPRequestServlet object , populate the information and call the servlet method ! – anshulkatta May 13 '13 at 10:48
  • possible duplicate of [How to call the Servlet from Java Swing login page using HttpClient in apache?](http://stackoverflow.com/questions/13601435/how-to-call-the-servlet-from-java-swing-login-page-using-httpclient-in-apache) – trashgod May 13 '13 at 13:41

2 Answers2

1

Please take a look @ this post

How to call the Servlet from Java Swing login page using HttpClient in apache?

might help resolve..

cheers!!!

Community
  • 1
  • 1
tpsharish
  • 23
  • 1
  • 1
  • 5
1

You can use HttpURLConnection to make an http request from a swing to your server.

Example:

HttpURLConnection connection;


try {

      String urlParameters = "username="+URLEncoder.encode(username,"UTF-8") 
                    +"&password="+URLEncoder.encode(password,"UTF-8");
      //Create connection

      URL url=new URL("your servlet url goes here");
      connection = (HttpURLConnection)url.openConnection();
      connection.setRequestMethod("POST");
      connection.setRequestProperty("Content-Type", 
           "application/x-www-form-urlencoded");

      connection.setRequestProperty("Content-Length", "" + 
               Integer.toString(urlParameters.getBytes().length));
      connection.setRequestProperty("Content-Language", "en-US");  

      connection.setUseCaches (false);
      connection.setDoInput(true);
      connection.setDoOutput(true);

      //Send request
      DataOutputStream wr = new DataOutputStream (
                  connection.getOutputStream ());
      wr.writeBytes (urlParameters);
      wr.flush ();
      wr.close ();

      //Get Response    
      InputStream is = connection.getInputStream();
      BufferedReader rd = new BufferedReader(new InputStreamReader(is));
      String line;
      while((line = rd.readLine()) != null) {
        // read response from your servlet
      }
      rd.close();


    } catch (Exception e) {

      e.printStackTrace();


    } finally {

      if(connection != null) {
        connection.disconnect(); 
      }
    }
Vamsi Abhishek
  • 135
  • 1
  • 12