2014-12-01 71 views
0

我正在開發一個使用登錄servlet作爲jsp登錄頁面的java web應用程序。登錄頁面動作設置爲轉到servlet,但無法正確加載,並且出現HTTP 500錯誤。我原本有一個arraylist,它在java類中本地存儲用戶名和密碼,在沒有錯誤的情況下工作正常,直到我將其更改爲使用JDBC從本地mysql數據庫中提取登錄數據。我的數據庫設置了「用戶」表,其中包含來自用戶類的所有字符串數據。Java登錄Servlet獲取內部錯誤請求HTTP 500 java.lang.NullPointerException

我跑了一個單獨的java類來測試數據庫,它似乎運行良好,但是當我試圖將它實現到我的web應用程序時,它給了它在LoginServlet中的錯誤。我設置了我的AuthenicationService類來從數據庫中提取用戶登錄數據,並將它們作爲對象存儲在我的數組列表中。不知道是否有更好的方法來做到這一點,但我不想混淆我以前的代碼,這些代碼在更改之前似乎可以正常工作。我確信有一個更好的方法來做到這一點,但我有點失落,所以任何幫助將不勝感激謝謝。

下面是用戶類我已經設置爲我的商業邏輯層:

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

在AuthenicationService方法中可能發生異常,因此用戶ArrayList爲null,並且登錄方法會拋出NPE。 – enrique7mc 2014-12-01 22:42:47

回答

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; 
    } 
+0

感謝您的回覆我試過你的建議和錯誤消失:) – MrLevel81 2014-12-01 23:07:15

+0

歡迎你的朋友:) – Pravin 2014-12-01 23:29:50

相關問題