2014-04-01 44 views
2

我做了一個登錄頁面並處理登錄信息到數據庫。班級將檢查用戶並返回他的ID。一切都很順利,直到我通過初始化變量UID的點。之後,我有一個SQL查詢打破了程序。我的錯誤是什麼?
錯誤Java Web編程

HTTP Status 500 - Internal Server Error 

type Exception report 

messageInternal Server Error 

descriptionThe server encountered an internal error that prevented it from fulfilling this request. 

exception 

org.apache.jasper.JasperException: java.lang.NullPointerException 
root cause 

java.lang.NullPointerException 

的login.jsp

</head> 
<body> 
    <% 
    if(request.getParameter("submit")!=null) 
    { 
     String ime = request.getParameter("name"); 
     String pass = request.getParameter("pass"); 
     int uid = DbBroker.checkUser(name,pass); 

     if (uid>0) 
     { 
      session.setAttribute("uid", name); 
      response.sendRedirect("prodaja.jsp"); 
     } 
     else if (uid==0) 
     { 
      session.setAttribute("uid", ime); 
      response.sendRedirect("administrator.jsp"); 
     }  
     else out.print("Korisnik nepostoji"); 

    } 
    %> 

</body> 

dbBroker.java

public class DbBroker { 

    public static Connection conn; 

    public static void conn() 
    { 
     try 
     { 
       Class.forName("com.mysql.jdbc.Driver").newInstance(); 
       conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/prodavnica", "root", ""); 
     } 
     catch(Exception e) 
     { 
      System.out.println(e); 
     } 
    } 

    private void close() 
    { 
     try 
     { 
      conn.close(); 
     } 
     catch(Exception e) 
     { 
      System.out.println(e); 
     } 
    } 

    public static int checkUser(String name, String pass) throws SQLException 
    { 
     conn(); 
     name=name.trim().replace("'", ""); 
     pass=pass.trim().replace("'",""); 
     int uid = -1; 
     Statement st=conn.createStatement(); 
     ResultSet rs=st.executeQuery("SELECT id from user WHERE name='"+name+ 
            "' and pass='"+pass+"';"); 
     if (rs.next()) 
     { 
      uid=rs.getInt("id"); 
     } 
     conn.close(); 
     return uid; 
    } 
} 
+0

提供錯誤爲好,日Thnx。 –

+0

請顯示你正在發送的名字並通過。另外,conn()是做什麼的? –

+3

而且,當你在那裏時,friggin'_hash your passwords_。 – Carsten

回答

0

我認爲你需要在添加 「DbBroker」 類JSP頁面一樣:

<%@頁面進口= 「packagename.DbBroker」 %>

<html> 
<%@ page import="packagename.DbBroker" %> 
    <head> 
    </head> 
<body> 
    <% 
    if(request.getParameter("submit")!=null) 
    { 
     String ime = request.getParameter("name"); 
     String pass = request.getParameter("pass"); 
     int uid = DbBroker.checkUser(name,pass); 

     if (uid>0) 
     { 
      session.setAttribute("uid", name); 
      response.sendRedirect("prodaja.jsp"); 
     } 
     else if (uid==0) 
     { 
      session.setAttribute("uid", ime); 
      response.sendRedirect("administrator.jsp"); 
     }  
     else out.print("Korisnik nepostoji"); 

    } 
    %> 

</body> 
</html>