akshay
akshay

Reputation: 295

how to modularize/organize java web application

I am creating a j2ee web appp and I have created the following packages

Now i want to write a functionality. So i have written a index.jsp page which has action = /loginConroller.

Now should I do the folling in loginController?

Authentication authentication = new Authentication();
boolean flag = authentication.chekLoginCredentials(username, passwd)

Will Authentication class consist of only one function? Is this approcach correct?

Upvotes: 1

Views: 664

Answers (2)

BalusC
BalusC

Reputation: 1108642

Normally you would like to get the User model from the DAO and check if it is null or not. If it is null, then display error. If it is not null, then put it in session and proceed.

E.g.

String username = request.getParameter("username");
String password = request.getParameter("password");
User user = userDAO.find(username, password);
if (user != null) {
    request.getSession().setAttribute("user", user);
    response.sendRedirect("home");
} else {
    request.setAttribute("error", "Unknown login, please try again.");
    request.getRequestDispatcher("login").forward(request, response);
}

Or something like that. With only a boolean flag you can't really login the user and you would have to query the DB everytime if you want to get/show details about the logged in user during the session.

Upvotes: 1

gmhk
gmhk

Reputation: 15940

Yes, that would be fine, but still you can add some more features that you can get in future which is related to the database like 1) Closing the connection 2) getting the connection etc

Upvotes: 0

Related Questions