2014-12-01 127 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

相关问题