登录功能_实现_Servlet与登录功能_实现_Service&Dao

登录功能_实现_Servlet

  

LoginServlet:

package cn.itcast.travel.web.servlet;

import cn.itcast.travel.domain.ResultInfo;
import cn.itcast.travel.domain.User;
import cn.itcast.travel.service.UserService;
import cn.itcast.travel.service.impl.UserServiceImpl;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet("/loginServlet")
public class LoginServlet extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                //1.获取用户名和密码是数据
        Map<String, String[]> map = request.getParameterMap();
                //2.封装User对象
        User user = new User();
        try {
            BeanUtils.populate(user,map);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
                //3.调用Service查询
        UserService service = new UserServiceImpl();
        User s = service.login(user);

        ResultInfo info = new ResultInfo();

        //4.判断用户对象是否为null
        if (s ==null){
            //用户名密码错误
            info.setFlag(false);
            info.setErrorMsg("用户名密码错误");
        }
        //5.判断用户是否激活
        if (s!=null && ! "Y".equals(s.getStatus())){
            //用户尚未激活
            info.setFlag(false);
            info.setErrorMsg("您尚未激活,请激活");
        }
        //6.判断登录成功
        if (s != null && "Y".equals(s.getStatus())){
            //登录成功
            info.setFlag(true);
        }
        //响应数据
        ObjectMapper mapper = new ObjectMapper();
        response.setContentType("application/join; charset=utf-8");
        mapper.writeValue(response.getOutputStream(),info);

    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.doPost(request, response);
    }
}

 

登录功能_实现_Service&Dao

  

    User login(User user);
}

 

 /**
     * 登录方法
     * @param user
     * @return
     */
    @Override
    public User login(User user) {
        return userDao.findByUSernameAndpassword(user.getUsername(),user.getPassword());
    }


}

 

    User findByUSernameAndpassword(String username, String password);
}

 

    /**
     * 根据用户名查询的方法
     * @param username
     * @param password
     * @return
     */

    @Override
    public User findByUSernameAndpassword(String username, String password) {
        User user = null;
        try {
            //1.定义sql
            String sql = "select * from tab_user where username = ? and password = ? ";
            //2.执行sql
            user = template.queryForObject(sql, new BeanPropertyRowMapper<>(User.class), username,password);
        } catch (Exception e) {

        }

        return user;
    }

 

posted @ 2023-03-01 09:54  zj勇敢飞,xx永相随  阅读(12)  评论(0编辑  收藏  举报