Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8906839
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:45:20+00:00 2026-06-15T02:45:20+00:00

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

  • 0

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…

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T02:45:21+00:00Added an answer on June 15, 2026 at 2:45 am

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having just added a new button in my web application, I get an error
In my Swing chat application i am having the send button, one text area,
I am building a Text Editor in Java(Swing) having an EditorPane to type the
I'm using java.awt.Robot for integration tests of my Swing application, but I'm having trouble
I need help setting up a simple C++/C# SWIG project. I am having a
I'm having a problem that when my frame is shown (after a login dialog)
I'm developing a Desktop Application in java(jdk 1.6) with swing but having a problem
I'm creating a Java swing app and I'm having a real hard time getting
Im having trouble getting Swing layouts to do what I want. I want the
I'm fairly new to Java and I've been having some difficulties with Swing. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.