Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/apache/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在apache中使用HttpClient从JavaSwing登录页面调用Servlet?_Java_Apache_Swing_Http_Servlets - Fatal编程技术网

如何在apache中使用HttpClient从JavaSwing登录页面调用Servlet?

如何在apache中使用HttpClient从JavaSwing登录页面调用Servlet?,java,apache,swing,http,servlets,Java,Apache,Swing,Http,Servlets,我的Swing登录页面包含用户名、密码和提交按钮 我需要将用户名和密码传递给LoginAction servlet,我需要通过servlet在控制台中获取用户名和密码 我的Swing代码是 package com.tps.SwingChat.login; import javax.swing.*; import org.apache.http.HttpResponse; import org.apache.http.client.ClientProtocolException; import

我的Swing登录页面包含用户名、密码和提交按钮

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

我的Swing代码是

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是

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

}
请帮助我找到解决办法

如果我提交swing页面,什么都不会发生。我需要用户名和密码才能在控制台中运行


提前感谢…

更新了代码,将数据传递到servlet并从服务器读取响应数据

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();
}
public void actionPerformed(ActionEvent ae){
字符串uname=text1.getText();
字符串pwd=text2.getText();
HttpClient=new DefaultHttpClient();
HttpPost=新的HttpPost(
"http://localhost:8089/SwingChat/LoginAction?uname=“+联塞特派团
+“&pwd=”+pwd);
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“uname”,uname));
添加(新的BasicNameValuePair(“pwd”,pwd));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行POST请求
HttpResponse rsp=client.execute(post);
//读取响应数据
HttpEntity=rsp.getEntity();
InputStream InputStream=entity.getContent();
字符串响应=convertStreamToString(inputStream);
System.out.println(“来自服务器的响应:+Response”);
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
私有静态字符串convertStreamToString(InputStream为){
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
StringBuilder sb=新的StringBuilder();
字符串行=null;
试一试{
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
is.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
使某人返回字符串();
}

请检查

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

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();
}
public void actionPerformed(ActionEvent ae){
字符串uname=text1.getText();
字符串pwd=text2.getText();
HttpClient=new DefaultHttpClient();
HttpPost=新的HttpPost(
"http://localhost:8089/SwingChat/LoginAction?uname=“+联塞特派团
+“&pwd=”+pwd);
试一试{
//添加您的数据
List nameValuePairs=新的ArrayList(2);
添加(新的BasicNameValuePair(“uname”,uname));
添加(新的BasicNameValuePair(“pwd”,pwd));
setEntity(新的UrlEncodedFormEntity(nameValuePairs));
//执行POST请求
HttpResponse rsp=client.execute(post);
//读取响应数据
HttpEntity=rsp.getEntity();
InputStream InputStream=entity.getContent();
字符串响应=convertStreamToString(inputStream);
System.out.println(“来自服务器的响应:+Response”);
}捕获(客户端协议例外e){
e、 printStackTrace();
}捕获(IOE异常){
e、 printStackTrace();
}
}
私有静态字符串convertStreamToString(InputStream为){
BufferedReader reader=新的BufferedReader(新的InputStreamReader(is));
StringBuilder sb=新的StringBuilder();
字符串行=null;
试一试{
而((line=reader.readLine())!=null){
sb.追加(第+行“\n”);
}
}捕获(IOE异常){
e、 printStackTrace();
}最后{
试一试{
is.close();
}捕获(IOE异常){
e、 printStackTrace();
}
}
使某人返回字符串();
}

请查看

谢谢您对我的支持。。但是我需要执行System.out.println(“用户名:+uname”);System.out.println(“密码:+pwd”);在servlet中…如何做???只需使用我提供的新代码覆盖
actionPerformed
函数。然后运行
Login
类以获取UI。然后单击submit按钮将请求放入服务器。在执行此操作之前,请确保您的本地服务器正在运行。如果使用您的代码运行,我将在控制台中获得输出,因为,嘿,我刚刚注意到您正在使用GET方法调用servlet,并且您在servlet的
doPost
方法中进行了编码。尝试将代码复制到
doGet
方法,看看它是否有效。在这种情况下,您可以从我的上述代码中避免使用
nameValuePairs
,不必使用
nameValuePairs
。这用于
POST
模式。通过
URL
参数传递值是
GET
方法。但是您使用的是
HttpPost
。此类用于
POST
模式。如果您特定于使用
GET
方法,那么您可以使用
HttpGet
,例如
HttpGet=newhttpget(“http://localhost:8089/SwingChat/LoginAction?uname=“+uname+”&pwd=“+pwd”)。即使是
HttpPost
也适用于您的情况谢谢您的支持。。但是我需要执行System.out.println(“用户名:+uname”);System.out.println(“密码:+pwd”);在servlet中…如何做???只需使用我提供的新代码覆盖
actionPerformed
函数。然后运行
Login
类以获取UI。然后单击submit按钮将请求放入服务器。在执行此操作之前,请确保您的本地服务器正在运行如果使用您的代码运行,我将在控制台中获得输出,因为,嘿,我刚刚注意到您正在使用GET方法调用servlet,并且您使用您的
doPost
方法进行了编码