将输入的文本转换为小写

发布于 2024-08-11 09:08:41 字数 2486 浏览 8 评论 0 原文

我有一个带有

[snip]

<% 
  String name = request.getParameter("name");
  String pass = request.getParameter("pass");
  String globalname = "webeng";
  String globalpass = "2009";
  if (name !=null && pass!=null && name.equals(globalname) && pass.equals(globalpass))
   {
   %>
    <hr />
    <p><b>Howdy, <%= request.getParameter("name") %></b></p>
    <hr />
<% }
  else if (name !=null | pass!=null && name.equals("") | pass.equals(""))
  {
  %>
    <hr />
    <p><b>Ooops, one or more fields are empty. Please fill everything out!!</b></p>
    <hr />
<% }
  else if (name !=null | pass!=null && !name.equals(globalname) | !pass.equals(globalpass))
  {
  %>
    <hr />
    <p><b>Incorrect Userdata!</b></p>
    <hr />
<% }
  else{
  }
%>

[snip]

的 index.jsp现在,例如全局名称是小写的“webeng”。人们可以输入“WebEng”、“webENG”、“WEBENG”及其变体。

我需要将字符串中输入的内容转换为小写。不知何故

String newname = name.toLowerCase();
String newpass = pass.toLowerCase();

不工作。有人知道吗?

这是 Eclipse 在使用

<% 
      String name = request.getParameter("name");
      String pass = request.getParameter("pass");
      String globalname = "webeng";
      String globalpass = "2009";
      String newname = name.toLowerCase();
      String newpass = pass.toLowerCase();

       if (name !=null && pass!=null && name.equals(globalname) && pass.equals(globalpass))
       {
       %>
        <hr />
        <p><b>Howdy, <%= request.getParameter("name") %></b></p>
        <hr />
    <% }
      else if (name !=null | pass!=null && name.equals("") | pass.equals(""))
      {
      %>
        <hr />
        <p><b>One or more fields are empty!</b></p>
        <hr />
    <% }
      else if (name !=null && pass!=null && !name.equals(globalname) | !pass.equals(globalpass))
      {
      %>
        <hr />
        <p><b>Incorrect Userdata!</b></p>
        <hr />
    <% }
      else{
      }
    %>

Eclipse 时告诉我的内容: http://i.imagehost .org/0277/2009-11-15_19_34_00.png

I've got an index.jsp with

[snip]

<% 
  String name = request.getParameter("name");
  String pass = request.getParameter("pass");
  String globalname = "webeng";
  String globalpass = "2009";
  if (name !=null && pass!=null && name.equals(globalname) && pass.equals(globalpass))
   {
   %>
    <hr />
    <p><b>Howdy, <%= request.getParameter("name") %></b></p>
    <hr />
<% }
  else if (name !=null | pass!=null && name.equals("") | pass.equals(""))
  {
  %>
    <hr />
    <p><b>Ooops, one or more fields are empty. Please fill everything out!!</b></p>
    <hr />
<% }
  else if (name !=null | pass!=null && !name.equals(globalname) | !pass.equals(globalpass))
  {
  %>
    <hr />
    <p><b>Incorrect Userdata!</b></p>
    <hr />
<% }
  else{
  }
%>

[snip]

Now, the globalname for example is in lowercase "webeng". Folks may type in "WebEng", "webENG", "WEBENG" and variations thereof.

I need those typed in Strings converted to lowercase. Somehow

String newname = name.toLowerCase();
String newpass = pass.toLowerCase();

is not working. Anybody got any idea?

This is what Eclipse tells me when I use

<% 
      String name = request.getParameter("name");
      String pass = request.getParameter("pass");
      String globalname = "webeng";
      String globalpass = "2009";
      String newname = name.toLowerCase();
      String newpass = pass.toLowerCase();

       if (name !=null && pass!=null && name.equals(globalname) && pass.equals(globalpass))
       {
       %>
        <hr />
        <p><b>Howdy, <%= request.getParameter("name") %></b></p>
        <hr />
    <% }
      else if (name !=null | pass!=null && name.equals("") | pass.equals(""))
      {
      %>
        <hr />
        <p><b>One or more fields are empty!</b></p>
        <hr />
    <% }
      else if (name !=null && pass!=null && !name.equals(globalname) | !pass.equals(globalpass))
      {
      %>
        <hr />
        <p><b>Incorrect Userdata!</b></p>
        <hr />
    <% }
      else{
      }
    %>

Eclipse: http://i.imagehost.org/0277/2009-11-15_19_34_00.png

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(4

奢欲 2024-08-18 09:08:41

你的代码逻辑很奇怪。 Scriptlet 也不会让测试变得更加容易。这是一个具有真实 Java 类风格的 SSCCE ,用于启动和简化测试:

public class Test {

    public static void main(String[] args) throws Exception {
        String user = "WeBeNg"; // Change this as if it is user input.
        String pass = "2009"; // Change this as if it is user input.

        String expectedUser = "webeng";
        String expectedPass = "2009";

        if (user == null || pass == null || user.isEmpty() || pass.isEmpty()) {
            System.out.println("Please enter both username and password.");
        } else if (user.equalsIgnoreCase(expectedUser) && pass.equals(expectedPass)) {
            System.out.println("Welcome " + user);
        } else {
            System.out.println("Unknown login.");
        }
    }

}

希望这会有所帮助。

编辑#1:请不要发布屏幕截图。将异常和堆栈跟踪复制粘贴到代码块中。顺便说一下,例外情况不是来自 Eclipse。它来自 Tomcat。此外,所讨论的异常(NullPointerException)是相当不言自明的。您访问了一个实际上为空的对象引用。

SomeObject someObject = null;
someObject.doSomething(); // Fails with NPE.
someObject = new SomeObject();
someObject.doSomething(); // Succes.

您需要先进行空检查,例如

if (someObject != null) {
    someObject.doSomething(); // Succes.
} 

Edit #2 我还建议您了解 Java 中的运算符、运算符优先级和表达式分组。看,以下内容不符合“逻辑”,

if (name !=null | pass!=null && name.equals("") | pass.equals(""))

这是一个很好的教程,可以从这里开始了解这一点: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html 祝你好运。

Your code logic is odd. Scriptlets also doesn't make testing more easy. Here's an SSCCE in flavor of a real Java class to kickoff and ease testing:

public class Test {

    public static void main(String[] args) throws Exception {
        String user = "WeBeNg"; // Change this as if it is user input.
        String pass = "2009"; // Change this as if it is user input.

        String expectedUser = "webeng";
        String expectedPass = "2009";

        if (user == null || pass == null || user.isEmpty() || pass.isEmpty()) {
            System.out.println("Please enter both username and password.");
        } else if (user.equalsIgnoreCase(expectedUser) && pass.equals(expectedPass)) {
            System.out.println("Welcome " + user);
        } else {
            System.out.println("Unknown login.");
        }
    }

}

Hope this helps.

Edit #1: please do not post screenshots. Copypaste the exception and stacktrace in code blocks. The exception is by the way not coming from Eclipse. It's coming from Tomcat. Also, the exception in question (NullPointerException) is fairly self-explaining. You accessed an object reference which is actually null.

SomeObject someObject = null;
someObject.doSomething(); // Fails with NPE.
someObject = new SomeObject();
someObject.doSomething(); // Succes.

You need to do a null-check first, e.g.

if (someObject != null) {
    someObject.doSomething(); // Succes.
} 

Edit #2 I also recommend you to learn about operators, operator precedence and expression grouping in Java. See, the following isn't "logical"

if (name !=null | pass!=null && name.equals("") | pass.equals(""))

Here's a good tutorial to start with to learn about this: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/operators.html Good luck.

杀お生予夺 2024-08-18 09:08:41

您可以使用 compareToIgnoreCase 并且您不必自己进行转换。

You can use compareToIgnoreCase and you don't have to do the conversion yourself.

念﹏祤嫣 2024-08-18 09:08:41

您似乎使用 |而不是|| 。这是故意的吗?

You seem to use | in stead of || . Is that intentional?

我早已燃尽 2024-08-18 09:08:41

这里是 equalsIgnoreCase。除非您确实不想使用 | 运算符来简化 or 的快捷方式,否则使用 || 通常更安全。我也不想像 BalusC 的回答那样失败,默认条件是“登录”用户,这有些令人不安。

<% 
String name = request.getParameter("name");
String pass = request.getParameter("pass");
String globalname = "webeng";
String globalpass = "2009";
if (name ==null || pass==null || name.equals("") || pass.equals("")) {
// You can use StringUtils.isBlank here instead if you have it available to you
 %>
<hr />
 <p><b>Ooops, one or more fields are empty. Please fill everything out!!</b></p>
 <hr />
<% } else if (name.equalsIgnoreCase(globalname) && pass.equalsIgnoreCase(globalpass))
 {
 %>
  <hr />
   <p><b>Howdy, <%= request.getParameter("name") %></b></p>
  <hr />
<% } else {
 %>
  <hr />
   <p><b>Incorrect Userdata!</b></p>
  <hr />
 }
%>

Here it is with equalsIgnoreCase. Unless you really don't want to shortcut the or with the | operator it's generally safer to go with ||. I also prefer not to fall through to success as in BalusC's answer, there is something unsettling about the default condition being to "login" the user.

<% 
String name = request.getParameter("name");
String pass = request.getParameter("pass");
String globalname = "webeng";
String globalpass = "2009";
if (name ==null || pass==null || name.equals("") || pass.equals("")) {
// You can use StringUtils.isBlank here instead if you have it available to you
 %>
<hr />
 <p><b>Ooops, one or more fields are empty. Please fill everything out!!</b></p>
 <hr />
<% } else if (name.equalsIgnoreCase(globalname) && pass.equalsIgnoreCase(globalpass))
 {
 %>
  <hr />
   <p><b>Howdy, <%= request.getParameter("name") %></b></p>
  <hr />
<% } else {
 %>
  <hr />
   <p><b>Incorrect Userdata!</b></p>
  <hr />
 }
%>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文