Question: Create a Java client code to submit the username/password to a server code. This is done when the client user clicks on the login button.
Create a Java client code to submit the username/password to a server code. This is done when the client user clicks on the login button. On the server side, the code will check if the username and password received from the client are valid. Let the valid username/password pairs be stored in an array of strings in the server code. The array should consist of 11 pairs and having the following structure:
String [] logins=new String[]{user1,pass1,user2,pass2, , user11,pass11}
The server should check if the username/password received from the client is part of its array. If this is true, it sends a yes response to the client, and if not it sends a no response. When the client receives the servers response, it will inform the user via a showMessageDialog message if the authentication is successful or not.
Consider the code down below to help you.
package number2;
import javax.swing.*;
public class Number2 extends JFrame {
public static void main(String[] args) { JTextField username = new JTextField(); JTextField password = new JPasswordField(); Object[] userDetails = { "Username:", username, "Password:", password };
int attempts = 3; while (true) { username.setText(null); password.setText(null); int option = JOptionPane.showConfirmDialog(null, userDetails, "Login", JOptionPane.OK_CANCEL_OPTION); if (option == JOptionPane.OK_OPTION) { if (lookup(username.getText(), password.getText())) { JOptionPane.showMessageDialog(null, "Login successful"); return; } else { JOptionPane.showMessageDialog(null, "Incorrect username or password " + (--attempts) + " left", "Alert", JOptionPane.WARNING_MESSAGE); if (attempts == 0) System.exit(0); } } else { System.out.println("Login canceled"); return; } } }
private static boolean lookup(String name, String password) { return name.equals("joinmy123") && password.equals("rawr82!!"); } }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
