2

In my Swing application I need to pass the parameters to the servlets using the login page to authenticate the user.

In the web application we can do this by using Ajax and JavaScript.

But, how can I achieve this in my Swing application? My team leader asked me to go with HTTP client in Apache.

Please help me to do this.

Peter O.
  • 28,965
  • 14
  • 72
  • 87
Human Being
  • 7,671
  • 25
  • 86
  • 134

3 Answers3

2

You have to create HTTP request. The HTTP method depends on your servlet. To create basic HTTP request you should use HttpUrlConnection. If you need something more complex take a look on Apache HttpClient.

AlexR
  • 109,181
  • 14
  • 116
  • 194
  • Can you please give me a example to pass the userName and password to the servlet . This could help me a lot to understand easily ... – Human Being Nov 27 '12 at 15:09
2

Your details are pretty scratchy, but from what I understand and your team lead says the right thing for the job is a HTTP POST, and this is better achieved with httpcomponents from apache.

Some sample code might look like:

 HttpClient client = new DefaultHttpClient();
 HttpPost post = new HttpPost(urlAsString);

 HttpResponse rsp = null;

    try {
        rsp = client.execute(post);
    } catch (IOException e) {
        //ha ha
    } finally {
        //close stuff
    }

and probably you will need to fetch some token from the response in order to make later request as logged in, but this is a implementation detail.

A pretty good example can be found here: http://www.vogella.com/articles/ApacheHttpClient/article.html

This is from the top of my head, hope it helps.

comanitza
  • 1,093
  • 2
  • 9
  • 16
0

As your team leader suggested, you can use the HTTP Client to issue a HTTP request to your servlet and passing in the desired parameters. See http://hc.apache.org/

mbelow
  • 1,073
  • 6
  • 11
  • Thanks for Supporting me..Can you please give me a example to pass the userName and password to the servlet ? This could help me a lot to understand easily.. – Human Being Nov 27 '12 at 15:10