Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/86.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
Java 将Servlet错误消息重定向到JSP后保存表单数据_Java_Html_Jsp_Servlets_Forms - Fatal编程技术网

Java 将Servlet错误消息重定向到JSP后保存表单数据

Java 将Servlet错误消息重定向到JSP后保存表单数据,java,html,jsp,servlets,forms,Java,Html,Jsp,Servlets,Forms,我有一个用户登录表单,当密码与数据库中的密码不匹配时,它会显示一条错误消息。我将错误消息重定向到如下所示的表单,但当它在表单上显示错误消息时,它会删除电子邮件/密码输入字段中的数据。在显示错误消息后,我可以做什么来保持输入字段中的数据?先谢谢你 //SERVLET重定向到JSP request.setAttribute("errorMessage", "Wrong credentials!"); request.getRequestDispatcher("

我有一个用户登录表单,当密码与数据库中的密码不匹配时,它会显示一条错误消息。我将错误消息重定向到如下所示的表单,但当它在表单上显示错误消息时,它会删除电子邮件/密码输入字段中的数据。在显示错误消息后,我可以做什么来保持输入字段中的数据?先谢谢你

//SERVLET重定向到JSP

request.setAttribute("errorMessage", "Wrong credentials!");
                    request.getRequestDispatcher("/Login.jsp").forward(request, response);
//JSP表单页

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>User Log in</title>
</head>
<body>
<form action = "LoginServlet" method="POST">
<h3>Sign In</h3><br>

Email: <input type="text" name="email" required><br>
Password: <input type="password" name="pass" required><br>
<div style="color: #FF0000;">${errorMessage}</div><br>
<input type="submit" name="submit" value="Sign in">

</form>
</body>
</html>

用户登录
登录
电子邮件:
密码:
${errorMessage}

我通过添加以下几行解决了这个问题:

//SERVLET

request.setAttribute("email",request.getParameter("email"));
request.setAttribute("pass", request.getParameter("pass"));
request.setAttribute("errorMessage", "Wrong credentials!");
request.getRequestDispatcher("/Login.jsp").forward(request, response);
//JSP页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>User Log in</title>
<%@ include file="PageHeader.html" %>
</head>
<body>
<form action = "LoginServlet" method="POST">
<h3>Sign In</h3><br>
Email: <input type="text" name="email" required value="${email}"><br>
Password: <input type="password" name="pass" required value="${pass}"><br>
<div style="color: #FF0000;">${errorMessage}</div><br>
<input type="submit" name="submit" value="Sign in" id="sButton">

</form>
</body>
</html>

用户登录
登录
电子邮件:
密码:
${errorMessage}

下面是一个关于如何使用AJAX实现这一点的示例

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html>
<html>
<head>
<title>User Log in</title>
<%@ include file="PageHeader.html" %>
<!-- you need to include jquery for this example -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>

</head>
<body>
<form action = "LoginServlet" method="POST" id="someform">
<h3>Sign In</h3><br>
Email: <input type="text" name="email" required><br>
Password: <input type="password" name="pass" required><br>
<div style="color: #FF0000;display:none" id="someMsg"></div><br>
<input type="submit" name="submit" value="Sign in" id="sButton">

</form>


<script>  
    //the following will ajaxify your form, all you have to do is add 'id="someform" to it
    //reference here: https://stackoverflow.com/questions/4112686/how-to-use-servlets-and-ajax
    $(document).on("submit", "#someform", function(event) {
        var $form = $(this); 
        $.post($form.attr("action"), $form.serialize(), function(response) {
            //response is not empty (because it contains the error string message)
            if(response){
               //here we are using the response from the servlet to set the text of the div. .show() makes the element visible, .delay(1000) is a 1000ms delay for .fadeOut() which makes the element invisible after some time 
               $('#someMsg').text(response).show().delay(1000).fadeOut("slow");
            }else{
               //response here is empty (String error = "";), so we just redirect the user (because details match)
               window.location = "Welcome.jsp"; 
            }
        });
        event.preventDefault(); // Important! Prevents submitting the form.
    });
</script>

</body>
</html>

让我知道它是否适用于您

您需要从jsp中的reguest参数中检索电子邮件和密码,或者让servlet将它们放在请求属性中(就像处理错误一样),并在转发后在jsp中检索它们。Nice。当计时器允许时,标记为答案。你试过ajax了吗?没有,实际上我还没有试过任何ajax代码,但我愿意接受任何建议是的,它工作得非常好。谢谢你的建议。
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
   protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


            String pass1 = request.getParameter("pass");
            String email = request.getParameter("email");
            String error = "";

            //do whatever you need to do here, just don't do any forwarding 

            //here we check if the details are incorrect (!), javascript on the front end will decide whether to stay on the same page or redirect based on the value we send in error string.
            if(!validate(email,pass1)){
                 //if details are wrong, lets send the following string:
                 error = "Your password or email is wrong and you should feel bad.";
            }

             response.setContentType("text/plain");  // Set content type of the response so that jQuery knows what it can expect.
             response.setCharacterEncoding("UTF-8"); // You want world domination, huh?
             response.getWriter().write(error);       // Write response body.


    }