不要使用 JSP 重置公式字段中的输入
我正在练习 JSP,并且想要创建一个简单的公式字段。下面的代码工作正常。现在我希望最后的输入保留在公式字段中。因此,当我输入值“密码”和“名称”时,无论 if-else 语句的结果是什么,这两个值都应保留在公式字段中。
例如,我输入“用户”和“1234”并按提交,公式字段被清除,我不希望这样。提交后这两个值将保留在那里。真诚地,我不知道如何解决这个问题。
我的建议是使用 application.setAttribute("",) 和 application.getAttribute(""),但我不知道如何。我很乐意提供任何建议。谢谢你!
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
<head>
<title>Practice</title>
</head>
<body>
<h2>Practice</h2>
<h3>Please enter your name and the password.</h3>
<form method="post" action="">
<table>
<tr><td style="text-align:center">Name</td>
<td><input type="text" name="name" size="80" /></td>
</tr>
<tr><td style="text-align:center">Password</td>
<td><input type="text" name="password" size="80" /></td>
</tr>
<tr><td><input type="submit" value="Send" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
</table>
</form>
<%-- Testing name and password. --%>
<% String name = request.getParameter("name");
String password = request.getParameter("password");
if (name != null && name.equals("user") && password != null && password.equals("1234"))
{
%>
<p>Your Input is correct!</p>
<% }
else
{
%>
<p>Your input is not correct!</p>
<% }
%>
</body>
</html>
I'm practising a bit on JSP and want to create a simple formular field. The code below is working properly. Now i want that the last input stays in the formular fields. So when i type in a value "password" and a "name" both values should stay in the formular field no matter what the result of the if-else statement is.
For example i type in "user" and "1234" and press submit the formular fields get cleared and i don't want that. The two values shall stay there after submitting. Sincerly i have no good idea how to solve this problem.
My suggestion would be to use application.setAttribute("",) and application.getAttribute(""), but i don't know how. I would be glad for any advise. Thank you!
<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
<head>
<title>Practice</title>
</head>
<body>
<h2>Practice</h2>
<h3>Please enter your name and the password.</h3>
<form method="post" action="">
<table>
<tr><td style="text-align:center">Name</td>
<td><input type="text" name="name" size="80" /></td>
</tr>
<tr><td style="text-align:center">Password</td>
<td><input type="text" name="password" size="80" /></td>
</tr>
<tr><td><input type="submit" value="Send" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
</table>
</form>
<%-- Testing name and password. --%>
<% String name = request.getParameter("name");
String password = request.getParameter("password");
if (name != null && name.equals("user") && password != null && password.equals("1234"))
{
%>
<p>Your Input is correct!</p>
<% }
else
{
%>
<p>Your input is not correct!</p>
<% }
%>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
只需使用提交的值填充其
value
属性即可。在正常 JSP EL 中,这将是:${param.username}
的作用与下面丑陋的 scriptlet 基本相同% if (request.getParameter("用户名") != null) { out.print(request.getParameter("用户名")); } %>,只是以一种更加干净和简洁的方式。JSTL
fn:escapeXml()
函数可以防止您XSS< /a> 攻击。否则,用户将能够输入">
作为名称并执行它(当然,使用更恶意的 javascript,它会发送例如,将 cookies 发送到另一台服务器,而不是简单的警报)。Just fill their
value
attribute with the submitted value. In normal JSP EL that would be:The
${param.username}
does basically the same as the following ugly scriptlet<% if (request.getParameter("username") != null) { out.print(request.getParameter("username")); } %>
, only in a much more clean and consice way.The JSTL
fn:escapeXml()
function is there to prevent you from XSS attacks. Otherwise users would be able to enter"><script>alert('xss');</script>
as name and get it executed (of course with a more malicious javascript which sends the cookies to another server for example instead of a simple alert).