谁能告诉我如何在登录方法中使用会话?这里我的登录代码和它正常工作。需要为此设置会话,不知道如何..
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
// TODO Auto-generated method stub
String operation = request.getParameter("operation");
if(operation!=null && operation.equalsIgnoreCase("login")){
loginDetail(request,response);
}//else if(operation!=null && operation.equalsIgnoreCase("login")){
//logout(request,response);
//}
}
private void loginDetail(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{
User u = new User();
UserService us =new UserServiceImpl() ;
String Uname = request.getParameter("txtUname");
String Pwrd = request.getParameter("txtPwrd");
u.setUname(Uname);
u.setPwrd(Pwrd);
System.out.println(Uname+""+Pwrd);
try {
if(us.Userlogin(u.getUname(),u.getPwrd())){
String message = "Thank you, " + Uname +"..You are now logged into the system";
HttpSession session = request.getSession(true);
session.setAttribute("username", Uname);
session.setAttribute("password", Pwrd);
response.setContentType("text/html");
request.setAttribute("message", message);
request.getRequestDispatcher("/Menu.jsp").forward(request, response);
}else {
String message = "You have to register first or check Your user name password again!";
request.setAttribute("loginMsg", message);
RequestDispatcher rd = getServletContext().getRequestDispatcher("/Login.jsp");
rd.forward(request, response);
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我只是将一些代码行放在“if(us.Userlogin(u.getUname(),u.getPwrd()))”语句中。通过这段代码引导我
答案 0 :(得分:0)
您是不是想通过您提到的代码登录,下次您不想再次登录才能访问这些功能? 因此,您需要在会话对象中放置一个指示登录状态的标志,该标志跨越特定时间的生命周期,例如30分钟。每次请求来确定客户端是否已记录时,您都会使用过滤器来查询标志。 如果您正在进行学习目的的项目,那将非常有用。