我制作了一个登录页面并将登录信息处理到数据库中。该类用于检查用户并返回他的 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
登录.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>
数据库代理.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;
}
}
最佳答案
我认为您需要将“DbBroker”类添加到 JSP 页面中,例如:
<%@ page import="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>
关于Java网络编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22797980/