使用 String.toLowerCase() 而不出现解析错误,JSP

发布于 2024-10-03 00:39:17 字数 2743 浏览 2 评论 0原文

我对我的代码还有最后一个问题。最后,我想构建一个忽略大小写敏感性的函数。因此,“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

狼性发作 2024-10-10 00:39:17

只需使用 <代码>String#equalsIgnoreCase()

String name = request.getParameter("name");
String password = request.getParameter("password");
if (name != null && name.equalsIgnoreCase("user")
    && password != null && password.equalsIgnoreCase("1234")) 
{
    // Pass.
}

我不断重复:JSP 不适合存放这些东西。我希望这“只是”家庭作业。

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.

雨落□心尘 2024-10-10 00:39:17

也许我没有看到其他东西,但我没有看到您曾经将 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

长发绾君心 2024-10-10 00:39:17

您必须声明您正在使用的变量。

尝试声明 namecheckpasswordcheck,例如:

 <% String name = request.getParameter("name");
   String password = request.getParameter("password");
   String namecheck = "";
   String passwordcheck = "";
 %>  

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 = "";
 %>  
荒人说梦 2024-10-10 00:39:17

您可以使用 jstl core tag libary 更改代码。

API

You can change your code using jstl core tag libary.

API

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文