简体   繁体   中英

page not redirecting in servlet

Hi I am doing small login application using servlet , my response am redirecting a another jsp but its getting redirected, Below is my code is there

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);              
            }

Thanks in advance, Suganth.A

In if condition use response.sendRedirect(request.getContextPath() + "/Login.jsp");

In else condition you are not forwarding RequestDispatcher.

use below code in else method

rd.forward(request, response);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM