如何在 apache 中使用 HttpClient 从 Java Swing 登录页面调用 Servlet? [英] How to call the Servlet from Java Swing login page using HttpClient in apache?

查看:29
本文介绍了如何在 apache 中使用 HttpClient 从 Java Swing 登录页面调用 Servlet?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 Swing 登录页面包含用户名和密码以及一个提交按钮.

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

我需要将用户名和密码传递给 LoginAction servlet,我需要通过 servlet 在控制台中获取用户名和密码...

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...

我的 Swing 代码是,

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());}
    }
}

我的 Servlet 是,

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..

提前致谢...

推荐答案

更新了您的代码以将数据传递给 servlet 并从服务器读取响应数据.

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();
}

请检查

这篇关于如何在 apache 中使用 HttpClient 从 Java Swing 登录页面调用 Servlet?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆