- 整体思路
- 设置欢迎页:设置完欢迎页之后,tomcat启动后会自动跳转到欢迎页而不是index.jsp
-
登录的整体步骤:
- 已经拥有前端页面:login.jsp,登录成功,跳转到业务主页面,登录失败输出提示信息,回到登录页面
- 编写Dao层:接口和实现类,它的作用是从数据库中获取登录的用户
//通过userCode从数据库中查询数据,并将其赋值给Java实体类user public User getLoginUser(Connection connection, String userCode) throws Exception { PreparedStatement pstm = null; ResultSet rs = null; User user = null; if(null != connection){ String sql = "select * from smbms_user where userCode=?"; Object[] params = {userCode}; rs = BaseDao.execute(connection, pstm, rs, sql, params); if(rs.next()){ user = new User(); user.setId(rs.getInt("id")); user.setUserCode(rs.getString("userCode")); user.setUserName(rs.getString("userName")); user.setUserPassword(rs.getString("userPassword")); user.setGender(rs.getInt("gender")); user.setBirthday(rs.getDate("birthday")); user.setPhone(rs.getString("phone")); user.setAddress(rs.getString("address")); user.setUserRole(rs.getInt("userRole")); user.setCreatedBy(rs.getInt("createdBy")); user.setCreationDate(rs.getTimestamp("creationDate")); user.setModifyBy(rs.getInt("modifyBy")); user.setModifyDate(rs.getTimestamp("modifyDate")); } //connection设成null不让其关闭,是因为后面业务层还需要数据库连接。 BaseDao.closeResource(null, pstm, rs); } return user; }
- 编写Service层:调用Dao层,获取业务所需要的数据,登录的接口及其实现类
public class UserServiceImpl implements UserService{ //创建UserServiceImpl就会实例化userDao private UserDao userDao; public UserServiceImpl(){ userDao = new UserDaoImpl(); } //通过前端传过来的姓名和密码对比数据库中查询到的 public User login(String userCode, String userPassword) { Connection connection = null; User user = null; try { connection = BaseDao.getConnection(); user = userDao.getLoginUser(connection, userCode); } catch (Exception e) { e.printStackTrace(); }finally{ BaseDao.closeResource(connection, null, null); } //匹配密码 if(null != user){ if(!user.getUserPassword().equals(userPassword)) user = null; } return user; } }
- 编写servlet:接受用户的请求交给业务层去做,这里用户的请求是登录,业务层要做的是在数据库中匹配输入的用户名和密码。
public class LoginServlet extends HttpServlet { //servlet:控制层,接收用户的请求,调用业务层代码。 @Override protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { //从前端获取用户名和密码(接收用户的请求) String userCode = req.getParameter("userCode"); String userPassword = req.getParameter("userPassword"); //接收请求后需处理业务,业务是:和数据库中的数据进行对比,所以需调用业务层 UserServiceImpl userService = new UserServiceImpl(); User user = userService.login(userCode, userPassword);//这里已经把登录的人给查出来了 //service层返回的就是user对象 if(user != null){//查有此人,可以登录 //将用户的信息放入Session中 req.getSession().setAttribute(Constant.USER_SESSION , user); //跳转主页(跳转到另一个页面,地址变了,所以用重定向) resp.sendRedirect("jsp/frame.jsp"); }else{//查无此人,无法登录 //转发会登录页面,顺带提示它,用户名或密码错误。((跳转到本页面,只是在本页面加了些信息(用户名或密码错误),地址没变,所以用请求转发)) req.setAttribute("error" , "用户名或密码错误");//请求可以携带数据 req.getRequestDispatcher("login.jsp").forward(req , resp); } } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { doGet(req, resp); } }
- 注册servlet
<!--servlet--> <servlet> <servlet-name>LoginServlet</servlet-name> <servlet-class>com.tong.servlet.user.LoginServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>LoginServlet</servlet-name> <url-pattern>/login.do</url-pattern> </servlet-mapping>