Java Web编程

时间:2014-04-01 22:15:27

标签: java sql jsp

我创建了一个登录页面并将登录信息处理到数据库。该类将检查用户并返回其ID。一切顺利,直到我通过初始化变量UID的点。之后我有一个SQL查询打破了程序。我的错是什么?
错误

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

1 个答案:

答案 0 :(得分:0)

我认为你需要添加你的&#34; DbBroker&#34;类在JSP页面中的类:

&lt;%@ page import =&#34; packagename.DbBroker&#34; %GT;

<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>