0

谁能告诉我如何在登录方法中使用会话?在这里我的日志代码及其正常工作。需要为此设置会话并且不知道如何..

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()))”语句中放了几行代码..引导我完成这段代码

4

1 回答 1

0

您的意思是您想通过您提到的代码登录,而下次您不想再次登录以访问这些功能?因此,您需要在会话对象中放置一个指示登录状态的标志,该标志跨越特定时间的生命周期,例如 30 分钟。并且每次请求来确定客户端是否已登录时,您都会放置一个过滤器来查询标志。如果您正在为学习目的进行项目,这将很有用。

于 2013-07-04T08:18:33.753 回答