The If StatementLet's modify the above two pages:
first.JSP:
<html>
<body>
<form action="second.JSP" method="post">
User Name: <input type="text" name="user">
Password: <input type="password" name="pass">
<input type="submit" value="Send Query String">
</form>
</body>
</html>
second.JSP:
<html>
<body>
<%
String u=request.getParameter("user");
String p=request.getParameter("pass");
if ((u.equals("John")) && (p.equals("John123"))) {
out.println "Hi" + u + ", your login is valid.";
}else{
out.println "Sorry, invalid login";
}
%>
</body>
</html>
Open first.JSP on web brower, enter some values as user name and password in
the boxes, then submit. You will see, if you entered "John" for the
user name and "John123" for the password, then the output on second.JSP
is:
Hi John, your login is valid
Otherwise, you will see the message:
Sorry, invalid login
The basic syntax of "If... then..." is:
if (...) {
...
}
You can use "else" or "else if " to make more choices:
if (...) {
...
}else if (...) {
...
}else{
...
}
|