1

well I have a jsp page with a login form, i'm using a servlet, if the username and password are correct the servlet redirects the user to another page else it redirects him to the login page again

when i log in with a correct username and password i'm redirected perfectly to reservation.jsp but when i put a wrong username or password in the form when i click on the submit button the page became blank

here is the servlet

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.*;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.mysql.jdbc.PreparedStatement;

/**
 * Servlet implementation class LogServlet
 */
@WebServlet("/LogServlet")
public class LogServlet extends HttpServlet {
private static final long serialVersionUID = 1L;




public LogServlet() {
    super();

}



protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    response.setContentType("text/html");


    String name=request.getParameter("name");
    String password=request.getParameter("password");

    PreparedStatement ps = null;
    ResultSet rs = null;

    try {
        Class.forName("com.mysql.jdbc.Driver");
        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/log",
                "root","");
        ps = (PreparedStatement) conn.prepareStatement("select nom_client,username,password from client where username = ? and password = ?");

        ps.setString(1, name);
        ps.setString(2, password);
        rs=ps.executeQuery();



    try {
        while(rs.next()){
        if(password.equals(rs.getString("password")) && name.equals(rs.getString("username"))){

        HttpSession session=request.getSession();
        session.setAttribute("name",name);
        PrintWriter out=response.getWriter();
        request.getRequestDispatcher("reservation.jsp").include(request, response);
        }
        else{
            response.sendRedirect("/login.jsp");

        }}
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();

    }
    System.out.close();


    }
    catch(Exception e){e.printStackTrace();}


}
}
hichamx
  • 794
  • 3
  • 7
  • 18

8 Answers8

2

use request.getContextPath(), It will redirect you to login page.

response.sendRedirect(request.getContextPath()+"/login.jsp");

and if you are reaching to your login page in that case, pass request and response objects to your jsp page.

request.getRequestDispatcher(request.getContextPath()+"/login.jsp").forward(request,response);
atish shimpi
  • 4,873
  • 2
  • 32
  • 50
1

use this.

request.getRequestDispatcher("/login.jsp").forward(request,response);

As you have used

request.getRequestDispatcher("reservation.jsp").include(request, response); 

to forward after successful login and as it is working, why not forwarding after failed also in this way .?

Which mean to use

request.getRequestDispatcher("/login.jsp").include(request, response); 
Saif
  • 6,804
  • 8
  • 40
  • 61
  • please be careful about the location of your login.jsp location. see if it is in the right location where in should be. – Saif May 13 '15 at 11:03
  • i have all the jsp files in the same location..i have tested your suggestion in another projet and it works perfectly, but not with that one – hichamx May 13 '15 at 11:17
  • try removing the slash `/` use `request.getRequestDispatcher("login.jsp").include(request, response); ` or `request.getRequestDispatcher("login.jsp").forward(request,response);` – Saif May 13 '15 at 11:25
  • the problem still persists – hichamx May 13 '15 at 11:51
1

The problem could be here:

response.sendRedirect("/login.jsp");

This means that in the location header of the redirect response you will have:

http://localhost:8080/login.jsp

If you change it to:

response.sendRedirect("login.jsp");

than its relative to your webabb context path:

http://localhost:8080/app/login.jsp

Try with firebug or similar and trace down request/response you should find out quickly. And be sure jsp files are were expected in the hierarchy.

Hope this helps

jsfviky
  • 183
  • 1
  • 11
0
request.getRequestDispatcher("login.jsp").include(request, response);

Have u tried above code in else part ? I think it should work

singhakash
  • 7,891
  • 6
  • 31
  • 65
Hiren patel
  • 971
  • 8
  • 25
0

Problem is response.sendRedirect(...) does not prepend the context path where the resource (jsp) is bundled. Try using request.getRequestDispatcher(...) in both cases.

Fran Montero
  • 1,679
  • 12
  • 24
0

Rather than giving response.sendRedirect("\login.jsp"); use response.sendRedirect("/yourprojectname/"); and in welcome file in web.xml set login.jsp as welcome file.

-1

Add this

response.sendRedirect("/yourprojectname/");

and set login.jsp as welcome file in web.xml

Jean-Rémy Revy
  • 5,607
  • 3
  • 39
  • 65
-1

The response from the database was empty, so I had to add that case to the class (via if/else condition).

CubeJockey
  • 2,209
  • 8
  • 24
  • 31
hichamx
  • 794
  • 3
  • 7
  • 18