我在 jsp 中编写了一个简单的登录模块(那里有很多脚本 :))。但是,我不断收到 NullPointer 异常错误。我找不到,我尝试了很多。
JSP登录代码
<form action="loginAction.jsp" method="post" name="config-form">
<table align="center">
<tr>
<td> UserName:</td>
<td><input type="text" name="username" required parameter=*"></td>
</tr>
<tr>
<td> Password:</td>
<td><input type="password" name="username" required parameter=*"></td>
</tr>
<tr><td><input type="submit" value="LogIn"></td></tr>
</table>
登录操作.jsp
<body>
<%
String username=request.getParameter("username");
String password=request.getParameter("password");
LoginModel login = new LoginModel();
login.setUsername(username);
login.setPassword(password);
LoginValidator validator = new LoginValidator();
boolean validate=validator.validateLogin(login);
if(validate==false)
{
response.sendRedirect("login.jsp");
}
else
{
response.sendRedirect("index.jsp");
}
%>
登录模型.java
public class LoginModel {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
登录验证器.java
public boolean validateLogin(LoginModel login)
{
String uname="vivekaltruist";
String passwd="somePassword";
System.out.println(login.getUsername());
if((login.getUsername().equals(uname)) && login.getPassword().equals(passwd))
{
return true;
}
return false;
}
堆栈跟踪:
java.lang.NullPointerException 在 LoginValidator.validateLogin(LoginValidator.java:12)
我敢肯定,这将是一个愚蠢的理由,但我无法找到它。同样的事情,我试过没有jsp,它工作正常。帮帮我