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>
Just fill their
valueattribute 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).