MVC session问题
用户登录进入到LoginServletLoginServlet的要求如下:1接收用户登入数据2调用用户的业务类(UsersBo.java),进行什么验证。如果合法,则进入...
用户登录进入到LoginServlet LoginServlet的要求如下:
1 接收用户登入数据 2 调用用户的业务类(UsersBo.java),进行什么验证。如果合法,则进入到SelectServlet,否则返回登录页面。
3 在验证通过后,把用户相关信息(或用户对象),保存在session中,以方便在会话中读取和进行判断用户是否为登录用户。
这里的LoginServlet要怎么写?? 展开
1 接收用户登入数据 2 调用用户的业务类(UsersBo.java),进行什么验证。如果合法,则进入到SelectServlet,否则返回登录页面。
3 在验证通过后,把用户相关信息(或用户对象),保存在session中,以方便在会话中读取和进行判断用户是否为登录用户。
这里的LoginServlet要怎么写?? 展开
1个回答
展开全部
public class LoginServlet extends HttpServlet {
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException{
//....
LoginService ls = null;
User user = null;
RequestDispatcher rd = null;
String us =null;
String pwd = null;
String rold = null;
PrintWriter pw = null;
try {
resp.setCharacterEncoding("GBK");
pw = resp.getWriter();
ls = new LoginService(new UserDAOImpl());
us = req.getParameter("ename");
pwd = req.getParameter("password");
String rol = req.getParameter("role");
if(rol.equals("0"))
rold="管理员";
else if(rol.equals("1"))
rold="老师";
else rold="学生";
//按照用户名、密码查询用户合法性
user = ls.queryUser(us, pwd,rold);
} catch (UserAuthException e) {
//当发生异常时把请求转发到错误页面
rd = req.getRequestDispatcher("/message.jsp");
req.setAttribute("message", "用户名或密码错误");
req.setAttribute("link","<a href='login.jsp'>请重试!</a>");
rd.forward(req, resp);
return;
}
//若登录成功则把用户作为session范围的属性存放
HttpSession ss = req.getSession();
ss.setAttribute(Constant.LOGIN_USER, user);
if(rold.equals("管理员")){
rd = req.getRequestDispatcher("/manager.jsp");
rd.forward(req,resp);
}else{
rd = req.getRequestDispatcher("/welcome.jsp");
rd.forward(req, resp);
return;
}
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException{
doGet(req, resp);
}
}
public void doGet(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException{
//....
LoginService ls = null;
User user = null;
RequestDispatcher rd = null;
String us =null;
String pwd = null;
String rold = null;
PrintWriter pw = null;
try {
resp.setCharacterEncoding("GBK");
pw = resp.getWriter();
ls = new LoginService(new UserDAOImpl());
us = req.getParameter("ename");
pwd = req.getParameter("password");
String rol = req.getParameter("role");
if(rol.equals("0"))
rold="管理员";
else if(rol.equals("1"))
rold="老师";
else rold="学生";
//按照用户名、密码查询用户合法性
user = ls.queryUser(us, pwd,rold);
} catch (UserAuthException e) {
//当发生异常时把请求转发到错误页面
rd = req.getRequestDispatcher("/message.jsp");
req.setAttribute("message", "用户名或密码错误");
req.setAttribute("link","<a href='login.jsp'>请重试!</a>");
rd.forward(req, resp);
return;
}
//若登录成功则把用户作为session范围的属性存放
HttpSession ss = req.getSession();
ss.setAttribute(Constant.LOGIN_USER, user);
if(rold.equals("管理员")){
rd = req.getRequestDispatcher("/manager.jsp");
rd.forward(req,resp);
}else{
rd = req.getRequestDispatcher("/welcome.jsp");
rd.forward(req, resp);
return;
}
}
public void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException,IOException{
doGet(req, resp);
}
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询