我正在使用一个java Web应用程序,该应用程序使用jsp登录页面的登录servlet。登录页面操作设置为转到servlet,但它无法正常加载,我收到HTTP 500错误。我最初有一个arraylist,它在java类本地存储了用户名和密码,工作正常,没有错误,直到我改变它以使用JDBC从本地mysql数据库中提取登录数据。我的数据库设置为" user"具有来自用户类的所有字符串数据。
我运行了一个单独的java类来测试数据库,它似乎运行正常但是当我尝试将它实现到我的Web应用程序中时,它在LoginServlet中给出了错误。我设置了我的AuthenicationService类来从数据库中提取用户登录数据并将它们作为对象存储在我的arraylist中。不确定是否有更好的方法可以做到这一点,但我不想弄乱我之前的代码似乎在更改之前正常工作。我确信有更好的方法可以做到这一点,但我有点失去了要改变什么,所以任何帮助都将非常感谢,谢谢。
以下是我设置为业务逻辑层的用户类:
public class User {
private String userName;
private String password;
private String firstName;
private String lastName;
private String email;
private String phoneNumber;
public User(String userName, String pwd, String first, String last, String email, String phone) {
this.password = pwd;
this.userName = userName;
this.firstName = first;
this.lastName = last;
this.email = email;
this.phoneNumber = phone;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
public String getUserName() {
return this.userName;
}
private String getPassword(){
return this.password;
}
private String getEmail(){
return this.email;
}
private String getPhoneNumber(){
return this.phoneNumber;
}
public boolean checkLogin(String userName, String pwd){
return this.getUserName().equals(userName) && this.getPassword().equals(pwd);
}
}
另一个从我的数据库中提取用户登录数据的类设置:
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.PreparedStatement;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.Iterator;
public class AuthenicationService {
ArrayList<User> users;
public AuthenicationService()throws SQLException {
String userName = null;
String Password = null;
String FirstName = null;
String LastName = null;
String Email = null;
String Phone = null;
try {
Connection myConn = DriverManager.getConnection("jdbc:mysql://localhost:3306/titanbank_db", "root", "sesame");
Statement myStmt = myConn.createStatement();
ResultSet result = myStmt.executeQuery("select * from user");
while (result.next()){
userName = result.getString("UserName");
Password = result.getString("Password");
FirstName = result.getString("FirstName");
LastName = result.getString("LastName");
Email = result.getString("Email");
Phone = result.getString("Phone");
users = new ArrayList<User>();
users.add(new User(userName, Password, FirstName, LastName, Email, Phone));
}
}
catch (Exception exc){
exc.printStackTrace();
}
}
public User login(String userName, String pwd){
User actual = null;
for (User me : users) {
if (me.checkLogin(userName, pwd)){
actual = me;
}
}
return actual;
}
}
登录Servlet:
import com.titanBank.bll.*;
import java.io.IOException;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class LoginServlet extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
}
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
try {
String userName = request.getParameter("userName");
String pwd = request.getParameter("password");
AuthenicationService service = new AuthenicationService();
User user = service.login(userName, pwd);
String url = "/accounts/accounts.jsp";
String errorUrl = "/stderror.jsp";
request.setAttribute("login", "true");
if (user != null)
getServletContext().getRequestDispatcher(url).forward(request, response);
else
getServletContext().getRequestDispatcher(errorUrl).forward(request, response);
} catch (SQLException ex) {
Logger.getLogger(LoginServlet.class.getName()).log(Level.SEVERE, null, ex);
}
}
@Override
public String getServletInfo() {
return "Short description";
}// </editor-fold>
}
我得到的错误:
type Exception report
message
description The server encountered an internal error that prevented it from fulfilling this request.
exception
java.lang.NullPointerException
com.titanBank.bll.AuthenicationService.login(AuthenicationService.java:56)
com.titanBank.controllers.LoginServlet.doPost(LoginServlet.java:41)
javax.servlet.http.HttpServlet.service(HttpServlet.java:644)
javax.servlet.http.HttpServlet.service(HttpServlet.java:725)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:393)
note The full stack trace of the root cause is available in the Apache Tomcat/8.0.9 logs.
答案 0 :(得分:0)
public User login(String userName, String pwd){
User actual = null;
if(users!=null){
for (User me : users) {
if (me.checkLogin(userName, pwd)){
actual = me;
}
}
return actual;
}
return null;
}