0

given that I'm not really practical with Servelts, I'm trying to create a stand alone Java application based on an architecture Client-Server. Suppose I'm handling with the login of a user. The client is a simple java application (.jar) standing on the user's pc, that invoke, via an Action Listener a Servlet (standing on a server, in my case the client and server reside on the same machine), which handles the login of the user. If the user is present into the database, then the Response of the Servlet is an object User, and starts an Http Session.

Here the diagram:

enter image description here

Here the code of the application so far:

public class LoginForm extends JFrame {

    private JTextField username;
    private JTextField password;
    private JButton btnOk;
    private JButton btnAnnulla;

    private Container c;
    private JPanel loginForm;


    public LoginForm() {
        super();
        initComponent();
    }

    private void initComponent() {

        username = new JTextField("Username");
        password = new JTextField("Password");
        btnOk = new JButton("OK");
        btnAnnulla = new JButton("Annulla");

        //.....

        btnOk.addActionListener(new btnOkListener());
    }
}

Here the listener:

public class btnOkListener implements ActionListener {

    @Override
    public void actionPerformed(ActionEvent e) {

        //TODO: invoke LoginServlet
        throw new UnsupportedOperationException("Not supported yet."); 
    }

}

What I have to do is to invoke the Servlet. How can I do that? May I have to create a new web application (instead a Java application) that allow me to create a Sevlet and put the jar in it or there is another way?

s.dallapalma
  • 893
  • 8
  • 28
  • Use `UrlConnection` or Apache Common's [`HttpClient`](https://hc.apache.org/httpcomponents-client-ga/). – Thomas Mar 10 '16 at 15:48
  • Ok, but do I have to create the Servlet on a separate project, right? (i'm working with NetBeans IDE) – s.dallapalma Mar 10 '16 at 15:57
  • Ok, so the server doesn't exist yet? In that case, yes, you'd have to create a new project to build the web application with the servlet. Whether the application contains an embedded web server or is deployed in an external webserver depends on you but assuming the client runs on a different machine you need at least two applications. – Thomas Mar 10 '16 at 16:01
  • @BalusC I would import the servlet class and invoke its method: doPost(Request, Response) passing it two parameter: the username and the password the user inserted in the LoginForm. Then, do I have to put the Servlet in the WEB-INF package and the suorce code of the client application (that is, LoginForm and btnOkListener) into the "Source Packages"? – s.dallapalma Mar 10 '16 at 16:16
  • Not possible, no, when *"standing on the user's pc"*. – BalusC Mar 10 '16 at 16:17
  • So I have to create a new separate Project for the Servlet and user UrlConnection or HttpClient like Thomas said, right? – s.dallapalma Mar 10 '16 at 16:19

0 Answers0