将Servlet错误消息重定向到JSP后保存表单数据 [英] Save Form data after redirecting Servlet error message to JSP

查看:118
本文介绍了将Servlet错误消息重定向到JSP后保存表单数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have a user login form which takes an error message when the password does not match the one in the database. I redirect the error message to the form as shown below, but when it shows the error message on the form it deletes the data on the email/password input fields. What can I do to still keep the data in the input fields after showing the error message? Thank you in advance.

//SERVLET重定向到JSP

//SERVLET REDIRECT TO 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>

推荐答案

我通过添加以下几行来解决此问题:

I solved this by adding these few lines:

//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>

这篇关于将Servlet错误消息重定向到JSP后保存表单数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆