简体   繁体   中英

requestDispatcher.forward() not open the new html page

I'm working on web app ,using JAVA Servlet .I'm trying to make a login form i just want the user to set username and password and if valid open the mainPage.html the code run without errors or exception ,but when this function runs requestDispatcher.forward() the page reload without forwarding to the mainPage.html

this is the validation function:

    public static boolean validate(String name,String pass){ 

    boolean status=false;  
    try{  
    Class.forName("com.mysql.jdbc.Driver");  
    Connection con=(Connection) DriverManager.getConnection(  
    "jdbc:mysql://localhost:3309/project","root","123456");  

    java.sql.PreparedStatement ps=con.prepareStatement(  
    "select * from user where username=? and password=?");  
    ps.setString(1,name);  
    ps.setString(2,pass);  

    ResultSet rs=ps.executeQuery();  
    status=rs.next();  

    }catch(Exception e){System.out.println(e);}  
    return status;  
    }

this is the login function:

public static void Login(HttpServletRequest request, HttpServletResponse response) {
    String userName= request.getParameter("username");
    String password =request.getParameter("password");
    if(UserDao.validate(userName, password)){  
        RequestDispatcher requestDispatcher=request.getRequestDispatcher("mainPage.html");  
        try {
            requestDispatcher.forward(request,response);
         /*I already debug the code , it run requestDispatcher.forward(request,response); without any exceptions but nothing happens the html page only refreshing ,not opening mainPage.html*/
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
    }  
    else{  
        System.out.print("Sorry username or password error");  
        RequestDispatcher rd=request.getRequestDispatcher("http://localhost:8080/project");  
        try {
            rd.include(request,response);
        } catch (ServletException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }  
    }  
}

getting the request from ajax in controller(servlet):

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException {
   if (action.contentEquals("Login")) 
        UserBusiness.Login(request, response);
    }

ajax call :

function Login() {
var username = $('#username').val();
var password = $('#password').val();
$.ajax({
    url: 'Controller',
    data : {
        action: 'Login',
        username: username,
        password: password,
    },
    type: 'post',
    success: function(response) {

    },
    error: function(e) {
        alert('error');
    }
});
}

login html page:

        <script>   
    $(document).ready(function () {
        //  loadJsFile("scripts/user-page.js");
        $('#login_btn').on('click', function(){



    Login();
        });
    });
</script>
    <form class="sign-in-htm" method="GET" >
      <div class="group">
        <label for="user" class="label">Username</label>
        <input id="username" name="username" type="text" class="input">
      </div>
      <div class="group">
        <label for="pass" class="label">Password</label>
        <input id="password" name="password" type="password" class="input" data-type="password">
      </div>
      <div class="group">
        <input id="check" type="checkbox" class="check" checked>
        <label for="check"><span class="icon"></span> Keep me Signed in</label>
      </div>
      <div class="group">
        <input type="submit" id="login_btn" class="button" value="Sign In">
      </div>
      <div class="hr"></div>
      <div class="foot-lnk">
        <a href="#forgot">Forgot Password?</a>
      </div>
    </form>

You are doing an AJAX request, but you are doing nothing with the response to the AJAX request.

Why don't you try removing the "click" action for login_btn, and test if a simple submit of the form works?

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