MVC session问题

用户登录进入到LoginServlet LoginServlet的要求如下: 1 接收用户登入数据 2 调用用户的业务类(UsersBo.java),进行什么验证。如果合法,则进入到SelectServlet,否则返回登录页面。 3 在验证通过后,把用户相关信息(或用户对象),保存在session中,... 展开
发布于2010-12-08 08:58 最佳答案
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);
}

}

wangqing2011

采纳率:14% 来自:芝麻团 擅长: 电脑/网络 游戏 医疗健康

为您推荐: