Laughing girl2021
2022-01-12 15:32
采纳率: 100%
浏览 26
已结题

servlet中参数报错,请问是为什么?逻辑没问题呀!

img


img

//用户servlet代码
@WebServlet(name = "UserServlet",urlPatterns = "/user")      
public class UserServlet extends BaseServlet {

    public void login(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String name = request.getParameter("name");
        String password = request.getParameter("password");

        UserService userService=new UserService();
        User user=null;
        try {
            //调用service中登录方法
            user = userService.login(name, password);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        if (user!=null){
            //登录成功后我们再获取是否保存密码的信息,如果失败了保存密码就没有意义了
            String remember = request.getParameter("remember");
            if (remember!=null&&remember.equals("yes")){
                // 将用户名和密码加入到cookie中
                Cookie nameCookie = new Cookie("name", name);
                Cookie passwordCookie = new Cookie("password", password);
                //设置cookie的有效期 防止销毁
                nameCookie.setMaxAge(60*10);
                passwordCookie.setMaxAge(60*10);
                //将cookie发送给客户端保存
                response.addCookie(nameCookie);
                response.addCookie(passwordCookie);

            }
            //登录成功跳转生鲜种类列表界面
            response.sendRedirect(request.getContextPath()+"/category?method=getCategoryList");
        }else {
            //登录失败提示
            response.setContentType("text/html;charset=utf-8");
            response.getWriter().write("用户登录失败");

        }

    }
    public void register(HttpServletRequest request, HttpServletResponse response) throws IOException {

        try {
            Map<String, String[]> parameterMap = request.getParameterMap();     //通过map获得对应的jsp页面所有参数
            User user=new User();
            BeanUtils.populate(user,parameterMap);

            UserService userService=new UserService();
            boolean register = userService.register(user.getName(),user.getPassword(),user.getEmail());
            if (register) {
                response.sendRedirect(request.getContextPath()+"/login.jsp");
            }else {
                response.setContentType("text/html;charset=utf-8");
                response.getWriter().write("注册失败");
            }
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

}
//用户界面的业务代码
public class UserService {

    public boolean register(User user){

        boolean register = false;
        UserDao UserDao = new UserDao();
        //判断注册的用户是否存在
        boolean checkUser = UserDao.checkUser(user.getName());
        //如果用户名不存在就将注册的信息添加到数据库
        if(checkUser){
            register = UserDao.register(user.getName(),user.getPassword(), user.getEmail());
        }
        return register;
    }

    public User login(String name, String password) throws SQLException {
        UserDao userDao = new UserDao();
        User user = userDao.login(name,password);
        return user;
    }
}
//用户界面数据库操作代码
public class UserDao {

    public boolean checkUser(String name){

        try {
            //数据库链接(匹配src文件夹下的c3p0-config.xml文件,这是固定程式)
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            QueryRunner queryRunner = new QueryRunner(dataSource);
            //传入参数,通过sql语句进入数据库看是否有此参数,如果有则将查询的值赋予user,如果没有则usernull
            String sql="select name from user where name=?";
            User usere = queryRunner.query(sql,new BeanHandler<User>(User.class),name);
            //如果user为空,说明这个用户名没有被注册过
            if(usere==null){
                return true;
            }else{
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public boolean register(User user) {

        //如果用户名不存在就将注册的信息添加到数据库
        try {
            //数据库链接(匹配src文件夹下的c3p0-config.xml文件,这是固定程式)
            ComboPooledDataSource dataSource = new ComboPooledDataSource();
            QueryRunner queryRunner = new QueryRunner(dataSource);
            //传入参数,通过sql语句进入数据库添加参数,如果添加成功,则row>0;
            String sql="insert into user value (null,?,?,?)";
            int row = queryRunner.update(sql,user.getName(),user.getPassword(),user.getEmail());
            //如果row>0,说明添加成功
            if(row>0){
                return true;
            }else{
                return false;
            }
        } catch (SQLException e) {
            e.printStackTrace();
            return false;
        }
    }

    public User login(String name,String password) throws SQLException {

        //数据库链接(匹配src文件夹下的c3p0-config.xml文件,这是固定程式)
        ComboPooledDataSource dataSource = new ComboPooledDataSource();
        QueryRunner queryRunner = new QueryRunner(dataSource);

        String sql="select * from user where name=? and password=?";
        User user = queryRunner.query(sql,new BeanHandler<User>(User.class),name,password);
        return user;
    }
}

3条回答 默认 最新

相关推荐 更多相似问题