使用jsp检查用户名和密码
你好,我正在学习一些 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这是错误的做法。
JSP scriptlet 代码(
<% %>
things 中的代码)总是会在页面被请求时立即执行,无论它是否是由某个页面请求的。正常的 GET 请求(链接、书签等)或 POST 表单提交。在应该显示空表单的初始请求中,代码也会被执行,并且在尝试获取请求参数时,它得到的只是
null
。由于您无法调用null
上的任何方法,因此您将收到NullPointerException
。让它只在表单提交时执行的最简单的解决方法是检查请求方法是否为 POST。
通常,提交表单时,未填写的输入字段最终将显示为空字符串,而不是
null
。但是,为了使代码更加健壮,建议无论如何检查null
。防止 NullPointerException 的正确解决方案是在尝试访问引用之前检查引用是否不为 null。
或者,您也可以包装引用,因为
"user"
保证不为null
。但是,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 onnull
, you will get aNullPointerException
.The easiest workaround to let it execute only on a form submit is to check if the request method is POST.
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 fornull
anyway.The correct solution to prevent
NullPointerException
is to check if the reference is notnull
before attempting to access it.Alternatively, you can also wrap the references since
"user"
is guaranteed notnull
.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 thedoPost()
method and write code there. You can learn anything about servlets in the[servlets]
tag info page here.这并不是对为什么你的jsp给你带来空指针异常的直接答案,而是在对字符串文字进行字符串比较时可以在代码中避免它们的一种方法(这不会解决你的情况从长远来看,要做这样的事情:
这样,如果密码为空,您会得到一个 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:
This way if password is null you get a false instead of a NPE.