Using String.toLowerCase() without getting a resolve error, JSP
I have a final question on my piece of code. As a last thing i want to build in a function which will ignore the case sensitivity. So "user" and "USER" and "UsEr" should be treated the same way. I have a first idea how to implement such a function. Thats what i tried out:
<% if (name != null) { namecheck = name.toLowerCase(); }
else { namecheck = request.getParameter("name"); }
%>
<% if (password != null) { passwordcheck = password.toLowerCase(); }
else { passwordcheck = request.getParameter("password"); }
%>
Since i must check if it's NULL to avoid an error, it is not so easy to implement.
Now i could found out that my "namecheck" and "passwordcheck" are only avaible in the curled if-brackets. So the compiler gives me a "not resolved" error. I don't know how to fix this problem without writing a huge number of if-else.
<?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" value="${fn:escapeXml(param.name)}" /></td>
</tr>
<tr><td style="text-align:center">Password</td>
<td><input type="password" name="password" value="${fn:escapeXml(param.password)}" /></td>
</tr>
<tr><td><input type="submit" value="Send" /></td>
</tr>
</table>
</form>
<%-- Testing name and password. --%>
<% String name = request.getParameter("name");
String password = request.getParameter("password");
%>
<%-- Trying to use the .toLowerCase(). --%>
<% if (name != null) { namecheck = name.toLowerCase(); }
else { namecheck = request.getParameter("name"); }
%>
<% if (password != null) { passwordcheck = password.toLowerCase(); }
else { passwordcheck = request.getParameter("password"); }
%>
<% if (namecheck != null && name.equals("user") && passwordcheck != null && password.equals("1234"))
{
%>
<p>Your Input is correct!</p>
<% }
else
{
%>
<p>Your input is not correct!</p>
<% }
%>
</body>
</html>
Just use String#equalsIgnoreCase()
.
String name = request.getParameter("name");
String password = request.getParameter("password");
if (name != null && name.equalsIgnoreCase("user")
&& password != null && password.equalsIgnoreCase("1234"))
{
// Pass.
}
I keep repeating: JSP is the wrong place for this stuff. I hope it's "just" homework.
Maybe I'm not seeing something else, but I don't see that you ever declared namecheck and passwordCheck as Strings. If you want to keep them available just declare them next to where you 'get' name and pas
You have to declare variables you're using.
Try declaring namecheck
and passwordcheck
, for example:
<% String name = request.getParameter("name");
String password = request.getParameter("password");
String namecheck = "";
String passwordcheck = "";
%>
You can change your code using jstl core tag libary.
API
精彩评论