页面不在servlet中重定向

时间:2014-09-15 13:37:19

标签: java jsp servlets

您好我正在使用servlet进行小型登录应用程序,我的回复是重定向另一个jsp但是它被重定向,下面是我的代码是

PreparedStatement ps = con.prepareStatement("select * from sahi_users where username = ? and password = ?");
        ps.setString(1, request.getParameter("userName"));
        ps.setString(2, request.getParameter("pass"));
        ResultSet rs = ps.executeQuery();   
        while (rs.next()) {
            check = true;
        }           
        if(check){
            HttpSession session = request.getSession();
            session.setAttribute("user", request.getParameter("userName"));
            //setting session to expiry in 30 mins
            session.setMaxInactiveInterval(30*60);
            Cookie userName = new Cookie("user", request.getParameter("userName"));
            userName.setMaxAge(30*60);
            response.addCookie(userName);
            response.sendRedirect("./Login.jsp");
        }else{
            RequestDispatcher rd = getServletContext().getRequestDispatcher("/Login.jsp");
            PrintWriter out= response.getWriter();
            //out.println("<font color=red>Either user name or password is wrong.</font>");
                rd.include(request, response);              
            }

提前致谢, Suganth.A

1 个答案:

答案 0 :(得分:0)

如果条件使用response.sendRedirect(request.getContextPath() + "/Login.jsp");

在其他情况下,您不转发RequestDispatcher。

在else方法中使用下面的代码

rd.forward(request, response);