请教运行JSP显示http status 500
运行jsp,显示http status 500
如下:
HTTP Status 500 -
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: java.lang.NullPointerException
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:491)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
root cause
java.lang.NullPointerException
chp04.DateBase.select(DateBase.java:23)
org.apache.jsp.student_jsp._jspService(student_jsp.java:93)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:377)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:313)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:260)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.26 logs.
代码应该是没有问题的,这是书上的例子(光盘附带的可以排除人为输入错误)。
student.jsp
<%@ page language="java" import="java.util.*,chp04.*"
pageEncoding="UTF-8"%>
<html>
<head>
<center>
<font size="+2" color="#FF1493">JSP的分页形式</font>
</center>
</head>
<body>
<form name="form1" method="GET">
<table align="center" border="0" width="60%" height="23%">
<tr bgcolor="#EED5B7">
<th>
姓名
</th>
<th>
年龄
</th>
<th>
成绩
</th>
<th>
居住地址
</th>
<th>
班级
</th>
<th>
说明
</th>
</tr>
<%
int allpage = 0;//总记录数
int pages = 0;//接收当前页数的变量
int Fenpages = 0;//分页的数量
int pagecount = 4;//每页上存在的记录上
int aa = 1;//当前页数
DateBase date = new DateBase();
ArrayList list = date.select();
allpage = list.size();
Fenpages = (allpage + pagecount - 1) / pagecount;
if (request.getParameter("pages") == null) {
pages = 1;
} else {
pages = Integer.parseInt(request.getParameter("pages"));
}
if (pages > Fenpages || pages < 1) {
pages = 1;
aa = pages;
} else {
aa = pages;
}
ArrayList li = new ArrayList();
int start = (aa - 1) * pagecount;
int end = start + pagecount;
if (aa < Fenpages) {
for (int i = start; i < end; i++) {
li.add(list.get(i));
}
} else {
for (int i = start; i < allpage; i++) {
li.add(list.get(i));
}
}
Student st;
for (int i = 0; i < li.size(); i++) {
st = (Student) li.get(i);
%>
<tr bgcolor="#EAEAEA">
<td>
<%=st.getName()%>
</td>
<td>
<%=st.getAge()%>
</td>
<td>
<%=st.getScore()%>
</td>
<td>
<%=st.getAddress()%>
</td>
<td>
<%=st.getClassroom()%>
</td>
<td>
<%=st.getDescription()%>
</td>
</tr>
<%
}
%>
<tr>
<td colspan="6">
当前第
<%=aa%>
页 共
<%=Fenpages%>
页
<%
if (aa > 1) {
%>
<A href="student.jsp?pages=<%=aa - 1%>">上一页</A>
<%
}
%>
<%
if (aa < Fenpages) {
%>
<A href="student.jsp?pages=<%=aa + 1%>">下一页</A>
<%
}
%>
<A href="student.jsp?pages=1">首页</A>
<A href="student.jsp?pages=<%=Fenpages%>">末页</A> ?
</td>
</tr>
</table>
</body>
</html>
DateBase.java
package chp04;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
public class DateBase {
public ArrayList select() {
ArrayList list = new ArrayList();
String name;
String age;
String score;
String address;
String classroom;
String description;
Connection con = this.getConnection();
try {
Statement st = con.createStatement();
ResultSet rs = st.executeQuery("select * from student ");
while (rs.next()) {
name = getCharset(rs.getString("name"));
age = rs.getInt("age") + "";
score = rs.getDouble("score") + "";
address = getCharset(rs.getString("address"));
classroom = getCharset(rs.getString("classroom"));
description = getCharset(rs.getString("description"));
Student std = new Student();
std.setAddress(address);
std.setAge(age);
std.setClassroom(classroom);
std.setDescription(description);
std.setName(name);
std.setScore(score);
list.add(std);
}
st.close();
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
return list;
}
public Connection getConnection() {
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/myuser", "root", "");
} catch (Exception e) {
e.printStackTrace();
}
return con;
}
public String getCharset(String str) {
String newStr = "";
try {
newStr = new String(str.getBytes("ISO8859-1"), "GB2312");
} catch (UnsupportedEncodingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return newStr;
}
}
Student.java
package chp04;
public class Student {
private String name; // 姓名
private String age; // 年龄
private String score;// 成绩
private String address;// 居住地址
private String classroom;// 班级
private String description;// 说明
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
public String getClassroom() {
return classroom;
}
public void setClassroom(String classroom) {
this.classroom = classroom;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getScore() {
return score;
}
public void setScore(String score) {
this.score = score;
}
}
jsp?分页显示
[解决办法]
chp04.DateBase.select(DateBase.java:23)
org.apache.jsp.student_jsp._jspService(student_jsp.java:93)
这个this 是null
你改成
DateBase db=new DateBase ();
Connection con = db.getConnection();
[解决办法]
Statement st = con.createStatement();
con空指针啊
[解决办法]
问题找到了,就是这行出错了
Statement st = con.createStatement();
Connection con = null;
try {
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/myuser", "root", "");
} catch (Exception e) {
e.printStackTrace();
}