使用jsp检查用户名和密码

发布于 2024-10-02 18:31:45 字数 1985 浏览 2 评论 0原文

你好,我正在学习一些 JSP,我对下面的代码有疑问。我创建一个index.jsp 文件并尝试使用eclipse 或直接使用Tomcat 6.0 运行它。我总是收到“if (name.equals("user") && password.equals("1234"))”行的错误。错误是 java.lang.NullPointerException。我很乐意提供任何建议。

<?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" size="80" /></td>
</tr>
<tr><td style="text-align:center">Password</td>
<td><input type="text" name="password" size="80" /></td>
</tr>
<tr><td><input type="submit" value="Send" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
</table>
</form>

<%-- Testing name and password. --%>
<% String name = request.getParameter("name");
   String password = request.getParameter("password");
   if (name.equals("user") && password.equals("1234"))
     {
%>
       <p>Your Input is correct!</p>
<%   }
   else if (!name.equals(""))
     {
%>
       <p>Please enter your name!</p>
<%   }

   else if (!password.equals(""))
     {
%>
       <p>Please enter your password!</p>
<%   }

   else if (!name.equals("") && !password.equals(""))
     {
%>
       <p>Please enter your name and your password!</p>

<%   }

   else
     {
%>
       <p>Your input is not correct!</p>
<%   }
%>

</body>


</html>

Hello i'm learning a bit JSP and i have a problem with the code below. I create one index.jsp file and try to run it with eclipse or directly with Tomcat 6.0. I always get a error for the line "if (name.equals("user") && password.equals("1234"))". The error is
java.lang.NullPointerException. I would be glad for any advise.

<?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" size="80" /></td>
</tr>
<tr><td style="text-align:center">Password</td>
<td><input type="text" name="password" size="80" /></td>
</tr>
<tr><td><input type="submit" value="Send" /></td>
<td><input type="reset" value="Reset" /></td>
</tr>
</table>
</form>

<%-- Testing name and password. --%>
<% String name = request.getParameter("name");
   String password = request.getParameter("password");
   if (name.equals("user") && password.equals("1234"))
     {
%>
       <p>Your Input is correct!</p>
<%   }
   else if (!name.equals(""))
     {
%>
       <p>Please enter your name!</p>
<%   }

   else if (!password.equals(""))
     {
%>
       <p>Please enter your password!</p>
<%   }

   else if (!name.equals("") && !password.equals(""))
     {
%>
       <p>Please enter your name and your password!</p>

<%   }

   else
     {
%>
       <p>Your input is not correct!</p>
<%   }
%>

</body>


</html>

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

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

发布评论

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

评论(2

⊕婉儿 2024-10-09 18:31:45

这是错误的做法。

JSP scriptlet 代码(<% %> things 中的代码)总是会在页面被请求时立即执行,无论它是否是由某个页面请求的。正常的 GET 请求(链接、书签等)或 POST 表单提交。

在应该显示空表单的初始请求中,代码也会被执行,并且在尝试获取请求参数时,它得到的只是 null。由于您无法调用 null 上的任何方法,因此您将收到 NullPointerException

让它只在表单提交时执行的最简单的解决方法是检查请求方法是否为 POST。

<%
    if (request.getMethod().equalsIgnoreCase("POST")) {
        // The form is been submitted. Put your code here.
    }
%>

通常,提交表单时,未填写的输入字段最终将显示为空字符串,而不是 null。但是,为了使代码更加健壮,建议无论如何检查 null

防止 NullPointerException 的正确解决方案是在尝试访问引用之前检查引用是否不为 null。

    if (name != null && !name.equals("user")) {
        // ...
    }

或者,您也可以包装引用,因为 "user" 保证不为 null

    if (!"user".equals(name)) {
        // ...
    }

但是,scriptlet(JSP 文件内的原始 Java 代码)是 不好的做法。通常的做法是让表单提交到 Servlet。然后,您可以重写 doPost() 方法并在那里编写代码。您可以在[servlets]标签信息页面中了解有关 servlet 的任何信息。

This is the wrong approach.

The JSP scriptlet code (the code in <% %> things) will always be executed immediately whenever the page is been requested, regardless of if it's been requested by a normal GET request (link, bookmark, etc) or a POST form submit.

On the initial request which should display the empty form, the code get executed as well and in an attempt to grab the request parameters, all it gets is null. Since you cannot invoke any methods on null, you will get a NullPointerException.

The easiest workaround to let it execute only on a form submit is to check if the request method is POST.

<%
    if (request.getMethod().equalsIgnoreCase("POST")) {
        // The form is been submitted. Put your code here.
    }
%>

Normally, when the form is submitted, the input fields which are not filled in will end up as empty strings, not as null. However, to make the code more robust, it's recommended to check for null anyway.

The correct solution to prevent NullPointerException is to check if the reference is not null before attempting to access it.

    if (name != null && !name.equals("user")) {
        // ...
    }

Alternatively, you can also wrap the references since "user" is guaranteed not null.

    if (!"user".equals(name)) {
        // ...
    }

However, scriptlets (raw Java code inside JSP files) are a bad practice. The normal practice is to let the form submit to a Servlet. You can then just override the doPost() method and write code there. You can learn anything about servlets in the [servlets] tag info page here.

活雷疯 2024-10-09 18:31:45

这并不是对为什么你的jsp给你带来空指针异常的直接答案,而是在对字符串文字进行字符串比较时可以在代码中避免它们的一种方法(这不会解决你的情况从长远来看,要做这样的事情:

if("1234".equals(password)){
...
}

这样,如果密码为空,您会得到一个 false 而不是 NPE。

This is not a direct answer to why your jsp is giving you null pointer exceptions, but one way you can avoid them in your code when doing string comparisons against string literals (which won't solve your situation in the long run is to do something like this:

if("1234".equals(password)){
...
}

This way if password is null you get a false instead of a NPE.

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