Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何调用另一个html页面java servlet_Java_Jquery_Html_Servlets - Fatal编程技术网

如何调用另一个html页面java servlet

如何调用另一个html页面java servlet,java,jquery,html,servlets,Java,Jquery,Html,Servlets,您好,我有一个使用Javaservlet的简单登录项目。我有index.html主页,两个servlet,第一个Login.java servlet服务index.html,第二个UserInfo.java servlet,当按下提交按钮时,我想重定向到这个servlet,还有jquery文件,其中包含将输入值发布到Login.java servlet的方法。当我按下登录按钮时,什么也没发生。这里是源代码 index.html Username:<input type="text" id=

您好,我有一个使用Javaservlet的简单登录项目。我有index.html主页,两个servlet,第一个Login.java servlet服务index.html,第二个UserInfo.java servlet,当按下提交按钮时,我想重定向到这个servlet,还有jquery文件,其中包含将输入值发布到Login.java servlet的方法。当我按下登录按钮时,什么也没发生。这里是源代码

index.html

Username:<input type="text" id="username_field" value="" class="round_border"/>
Password:<input type="password" id="password_field" value="" class="round_border"/>
<input onclick="login()" type="submit" value="Submit" id="login_button" class="btn" />
Login.java

    RequestDispatcher requestDispatcher  = request.getRequestDispatcher("UserInfo");
    requestDispatcher.forward(request, response);
try (PrintWriter out = response.getWriter()) {

            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet UserInfo</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet UserInfo at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
if(!password.equals("123456") //check if password is wrong
    out.println("{\"error\": \"wrong password\"}"); //write error to response. notice, we don't need server-redirect, since we will redirect by the client if the error exists
UserInfo.java

    RequestDispatcher requestDispatcher  = request.getRequestDispatcher("UserInfo");
    requestDispatcher.forward(request, response);
try (PrintWriter out = response.getWriter()) {

            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet UserInfo</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet UserInfo at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
if(!password.equals("123456") //check if password is wrong
    out.println("{\"error\": \"wrong password\"}"); //write error to response. notice, we don't need server-redirect, since we will redirect by the client if the error exists
try(PrintWriter out=response.getWriter()){
out.println(“”);
out.println(“”);
out.println(“”);
out.println(“Servlet用户信息”);
out.println(“”);
out.println(“”);
println(“位于“+request.getContextPath()+”的Servlet用户信息);
out.println(“”);
out.println(“”);
}

好的,问题是,您通过以下线路向服务器发送呼叫:

$.post("Login", params, function(data){alert(data);}
        );
如果执行ajax调用,浏览器将不会转到新页面,服务器只会将ajax调用重定向到另一个servlet。因此,如果要在ajax调用后重定向,必须通过javascript重定向

如果您的loginServlet将发送如下错误:

$.post("Login", params, function(data){
            if(!data.error)
                window.location.href = "http://example.com/UserInfo/";
            else
                alert(data.error);
        });
Login.java

    RequestDispatcher requestDispatcher  = request.getRequestDispatcher("UserInfo");
    requestDispatcher.forward(request, response);
try (PrintWriter out = response.getWriter()) {

            out.println("<!DOCTYPE html>");
            out.println("<html>");
            out.println("<head>");
            out.println("<title>Servlet UserInfo</title>");            
            out.println("</head>");
            out.println("<body>");
            out.println("<h1>Servlet UserInfo at " + request.getContextPath() + "</h1>");
            out.println("</body>");
            out.println("</html>");
        }
if(!password.equals("123456") //check if password is wrong
    out.println("{\"error\": \"wrong password\"}"); //write error to response. notice, we don't need server-redirect, since we will redirect by the client if the error exists
在您的客户机中,您可以执行以下操作:

$.post("Login", params, function(data){
            if(!data.error)
                window.location.href = "http://example.com/UserInfo/";
            else
                alert(data.error);
        });
如果ajax调用成功,将执行该函数,并将浏览器重定向到UserInfo servlet

编辑:添加了成功检查


Edit2:在响应中添加了打印错误

是否收到
警报(数据)?确切地说,我在alertbox中获得了源代码。顺便说一句,我删除了警报(数据),但什么也没有发生。警报框中到底有什么?UserInfoit的源代码会重定向我,即使密码或用户名不正确或未填写。我希望servlet在用户名和密码正确时重定向我。当然,如果用户名错误,它将重定向您。您可以发送某种错误作为结果,并在重定向之前进行检查