1.编写前端页面
2.设置欢迎页面(首页)
<welcome-file-list>
<welcome-file>login.jsp</welcome-file>
</welcome-file-list>
</web-app>
3.编写dao层用户登录的接口
package com.lsj.dao.user;
import com.lsj.pojo.user;
import java.sql.Connection;
import java.sql.SQLException;
public interface UserDao {
//得到登录的用户
public user getLoginUser(Connection connection,String userCode) throws SQLException;
}
4.编写dao接口的实现类
package com.lsj.dao.user;
import com.lsj.dao.BaseDao;
import com.lsj.pojo.user;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public class UserDaoImpl implements UserDao{
@Override
public user getLoginUser(Connection connection, String userCode) throws SQLException {
PreparedStatement pstm=null;
ResultSet rs=null;
user user =null;
if(connection!=null){
String sql="select * from smbms_user where userCode=?";
Object[] params={userCode};
user=new user();
rs = BaseDao.execute(connection, pstm, rs, sql, params);
if(rs.next()){
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("createBy"));
user.setCreationDate(rs.getDate("creationDate"));
user.setModifyBy(rs.getInt("modifgyBy"));
user.setModifyDate(rs.getDate("modifyDate"));
}
BaseDao.closeResource(null,pstm,rs);
}
return user;
}
}
5.业务层接口
User login(String userCode, String password);
6.业务层实现类
public class UserServiceImpl implements UserService{
//业务层都会调用dao层,所以要引入dao层
private UserDao userDao;
public UserServiceImpl(){
userDao=new UserDaoImpl();
}
@Override
public User login(String userCode, String password) {
Connection connection=null;
User user=null;
connection= BaseDao.getConnection();
try {
//通过业务层调用对应的具体数据库操作
user = userDao.getLoginUser(connection, "zhaoyan");
} catch (SQLException throwables) {
throwables.printStackTrace();
}finally {
BaseDao.closeResource(connection,null,null);
}
return user;
}
}
7.编写Servlet
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
System.out.println("start...");
String userCode = req.getParameter("userCode");
String userPassword = req.getParameter("userPassword");
//和数据库中的密码进行对比,调用业务层;
UserServiceImpl userService = new UserServiceImpl();
User user = userService.login(userCode, userPassword);//已经把登录的人查出来了
if(user!=null){//查有此人,将用户的信息存到session中
req.getSession().setAttribute(Constants.USER_SESSION,user);
//跳转到内部主页
resp.sendRedirect("jsp/frame.jsp");
}else {//查无此人,无法登录,转发回登录页面,顺带提示用户名或者密码错误
req.setAttribute("error","用户名或者密码不正确");
req.getRequestDispatcher("login.jsp").forward(req,resp);
}
}
8.注册Servlet
9.测试访问要确保以上功能成功