使用 String.toLowerCase() 而不出现解析错误,JSP
我对我的代码还有最后一个问题。最后,我想构建一个忽略大小写敏感性的函数。因此,“user”和“USER”和“UserEr”应该以相同的方式对待。我第一个想法是如何实现这样的功能。这就是我尝试过的:
<% if (name != null) { namecheck = name.toLowerCase(); }
else { namecheck = request.getParameter("name"); }
%>
<% if (password != null) { passwordcheck = password.toLowerCase(); }
else { passwordcheck = request.getParameter("password"); }
%>
由于我必须检查它是否为 NULL 以避免错误,因此实现起来并不容易。 现在我发现我的“namecheck”和“passwordcheck”仅在卷曲的 if 括号中可用。所以编译器给了我一个“未解决”错误。我不知道如何在不编写大量 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>
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>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只需使用 <代码>String#equalsIgnoreCase()。
我不断重复:JSP 不适合存放这些东西。我希望这“只是”家庭作业。
Just use
String#equalsIgnoreCase()
.I keep repeating: JSP is the wrong place for this stuff. I hope it's "just" homework.
也许我没有看到其他东西,但我没有看到您曾经将 namecheck 和 passwordCheck 声明为字符串。如果您想让它们保持可用,只需在您“获取”姓名和通行证的位置旁边声明它们即可
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
您必须声明您正在使用的变量。
尝试声明
namecheck
和passwordcheck
,例如:You have to declare variables you're using.
Try declaring
namecheck
andpasswordcheck
, for example:您可以使用 jstl core tag libary 更改代码。
API
You can change your code using jstl core tag libary.
API