日期:2014-05-18 浏览次数:20691 次
//实体类 public class User { String name; String password; public User(){ } public User(String name, String password) { super(); this.name = name; this.password = password; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } } //Dao 类 public class UserDao { public User findUserByName(String username){ return new User(username, "password"); } public boolean addUser(User user){ // do something... return true; } } //Bo 类 public class UserBo { private UserDao dao = new UserDao(); public boolean login(String username,String password){ User u = dao.findUserByName(username); if( u != null && u.getPassword().equals(password)){ return true; } return false; } public boolean addUser(User user){ return dao.addUser(user); } } //Servlet import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class Login extends HttpServlet { private UserBo bo = new UserBo(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { this.doPost(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String username = request.getParameter("username"); String password = request.getParameter("password"); boolean b = bo.login(username, password); if( b ){ request.setAttribute("user", username); response.sendRedirect("loginSuccess.jsp"); }else{ response.sendRedirect("loginError.jsp"); } } }
public User login(String username,String password) throws LoginException{ User u = dao.findUserByName(username); if(u == null) { throw new LoginException("不存在的用户"); } if(!u.getPassword().equals(password)){ throw new LoginException("密码错误"); } return u; }
------解决方案--------------------
业务层(BO)该做什么事?该怎样做事?
业务复杂些的话你就需要在public class Login extends HttpServlet {
调用多个DAO------什么能称为业务就是这种方式
LZ您知道了么,如果调用了多个DAO就知道他的好处了。