package com.bjsxt.web.servlet;
import com.bjsxt.commons.Constants;
import com.bjsxt.exception.UserNotFoundException;
import com.bjsxt.pojo.Users;
import com.bjsxt.service.UserLoginServive;
import com.bjsxt.service.impl.UserLoginServiceImpl;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
/*
处理用户请求登录
*/
@WebServlet("/login.do")
public class UserLoginServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
this.doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("username");
String pwd =req.getParameter("userpwd");
try{
UserLoginServive userLoginServive = new UserLoginServiceImpl();
Users users =userLoginServive.userLogin(name,pwd);
/*
建立客户端与服务端的会话状态
*/
HttpSession session =req.getSession();
//会话状态建立
session.setAttribute(Constants.USER_SESSION_KEY,users);//这里的 键 调用了Constants类中的常量, 值 是上面的users
//跳转到首页,使用重定向的方式实现跳转
resp.sendRedirect("main.jsp");
}catch (UserNotFoundException u){
req.setAttribute("msg",u.getMessage());
req.getRequestDispatcher("login.jsp").forward(req,resp); //重新跳转到登录页面
}catch (Exception e){
resp.sendRedirect("error.jsp"); //如发生其他异常的时候,跳转到一个错误提示页面
}
}
}
老师,在写servlet层的时候,上面这段代码中的
UserLoginServive userLoginServive = new UserLoginServiceImpl();
Users users =userLoginServive.userLogin(name,pwd);
这两行代码的作用是什么?
用来判断用户信息是否存在的嘛?