1

I am having the Swing login page contains userName and password and a submit button .

I need to pass the username and password to the LoginAction servlet and I need to get userName and password in the Console through servlet...

My Swing code is ,

package com.tps.SwingChat.login;

import javax.swing.*;

import org.apache.http.HttpResponse;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;

import java.awt.*;
import java.awt.event.*;
import java.io.IOException;

class Login extends JFrame implements ActionListener
{
    private static final long serialVersionUID = 1L;
    JButton SUBMIT;
    JPanel panel;
    JLabel label1,label2;
    final JTextField  text1,text2;
    Login()
    {
        label1 = new JLabel();
        label1.setText("Username:");
        text1 = new JTextField(15);

        label2 = new JLabel();
        label2.setText("Password:");
        text2 = new JPasswordField(15);

        SUBMIT=new JButton("SUBMIT");

        panel=new JPanel(new GridLayout(3,1));
        panel.add(label1);
        panel.add(text1);
        panel.add(label2);
        panel.add(text2);
        panel.add(SUBMIT);
        add(panel,BorderLayout.CENTER);
        SUBMIT.addActionListener(this);
        setTitle("LOGIN FORM");
    }
    public void actionPerformed(ActionEvent ae)
    {
        String uname=text1.getText();
        String pwd=text2.getText();


        HttpClient client = new DefaultHttpClient();
        HttpPost post = new HttpPost("http://localhost:8089/SwingChat/LoginAction?uname="+uname+"&pwd="+pwd);

        try {
            HttpResponse rsp = client.execute(post);
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}
class LoginDemo
{
    public static void main(String arg[])
    {
        try
        {
            Login frame=new Login();
            frame.setSize(300,100);
            frame.setVisible(true);
        }
        catch(Exception e)
        {JOptionPane.showMessageDialog(null, e.getMessage());}
    }
}

And my Servlet is,

public class LoginAction extends HttpServlet {
    private static final long serialVersionUID = 1L;

    String uname = null;
    String pwd = null;

    public LoginAction() {
    super();
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        uname = request.getParameter("uname");
        pwd = request.getParameter("pwd");

        System.out.println("UserName : "+uname);
        System.out.println("Password : "+pwd);
    }

}

Please any help me to find the solution.

If I submit the swing page nothing is happend.I need the userName and password to e in console..

Thanks in advance...

Human Being
  • 8,269
  • 28
  • 93
  • 136

1 Answers1

4

Updated your code to pass data to servlet and read response data from server.

public void actionPerformed(ActionEvent ae) {
    String uname = text1.getText();
    String pwd = text2.getText();

    HttpClient client = new DefaultHttpClient();
    HttpPost post = new HttpPost(
            "http://localhost:8089/SwingChat/LoginAction?uname=" + uname
                    + "&pwd=" + pwd);
    try {
        // Add your data
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(2);
        nameValuePairs.add(new BasicNameValuePair("uname", uname));
        nameValuePairs.add(new BasicNameValuePair("pwd", pwd));
        post.setEntity(new UrlEncodedFormEntity(nameValuePairs));

        // executing the POST request
        HttpResponse rsp = client.execute(post);

        // reading response data
        HttpEntity entity = rsp.getEntity();
        InputStream inputStream = entity.getContent();
        String response = convertStreamToString(inputStream);
        System.out.println("Response from server : " + response);
    } catch (ClientProtocolException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

private static String convertStreamToString(InputStream is) {

    BufferedReader reader = new BufferedReader(new InputStreamReader(is));
    StringBuilder sb = new StringBuilder();

    String line = null;
    try {
        while ((line = reader.readLine()) != null) {
            sb.append(line + "\n");
        }
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    return sb.toString();
}

Please check

sunil
  • 6,444
  • 1
  • 32
  • 44
  • Thanks for supporting me.. But I need to execute the System.out.println("UserName : "+uname); System.out.println("Password : "+pwd); in the servlet...How to do this ??? – Human Being Nov 28 '12 at 09:54
  • just overwrite the `actionPerformed` function with the new code that i have provided. then run the `Login` class to get the UI. then click submit button to place the request to your server. make sure your local server is running before doing this – sunil Nov 28 '12 at 10:06
  • If run with your code , I am getting the output in the console as, – Human Being Nov 28 '12 at 10:21
  • hey i just noticed that you are calling the servlet using GET method and you did coding in `doPost` method of your servlet. Try copying the code to `doGet` method and see whether it works or not. In this case you can avoid using `nameValuePairs` from my above code – sunil Nov 28 '12 at 13:49
  • How can I pass parameters to my servlet using HttpPost ? – Human Being Nov 28 '12 at 15:18
  • As i already given in the code sample, using `nameValuePairs` – sunil Nov 28 '12 at 17:18
  • Thanks for your reply . But I already send the parameters in HttpPost post = new HttpPost( "http://localhost:8089/SwingChat/LoginAction?uname=" + uname + "&pwd=" + pwd); ... But why I need to send the parameters in nameValuePairs and also in HttpPost ... Sorry for disturbing you too much... – Human Being Nov 29 '12 at 06:07
  • 1
    It is not necessary to use `nameValuePairs`. This is used for `POST` mode. Passing values via `URL` parameters is the `GET` method. But you are using `HttpPost`. This class is meant for `POST` mode. If you are specific to use `GET` method, then you can use `HttpGet`, like this `HttpGet get = new HttpGet( "http://localhost:8089/SwingChat/LoginAction?uname=" + uname + "&pwd=" + pwd);`. Even `HttpPost` will work in your case – sunil Nov 29 '12 at 06:15
  • Thanks SUNIL ....Its worked well.... Thanks a lot... Can you please give the website where you referred ? I will update if any issue happens ... – Human Being Nov 29 '12 at 06:26