开发者

how to modularize/organize java web application

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

  • com.cms.controller (to hold servlets)
  • com.cms.business (to hold busines logic)
  • com.cms.dao (to hold data acce开发者_开发技巧ss objects)
  • com.cms.beans (to hold beans)

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?


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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜