requestDispatcher.forward()无法打开新的html页面

问题描述 投票:0回答:1

我正在使用JAVA Servlet进行网络应用开发。我正在尝试制作一个登录表单,我只希望用户设置用户名和密码,如果有效,请打开mainPage.html。代码运行时没有错误或异常,但是当此函数运行requestDispatcher.forward()时,页面重新加载而不转发到mainPage.html

这是验证功能:

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

这是登录功能:

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

从controller(servlet)中的ajax获取请求:

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

ajax电话:

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

登录html页面:

        <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>
java html ajax servlets
1个回答
0
投票

您正在执行AJAX请求,但是您对AJAX请求的响应没有任何反应。

为什么不尝试删除login_btn的“ click”操作,并测试简单的表单提交是否有效?

© www.soinside.com 2019 - 2024. All rights reserved.