日期:2014-05-18  浏览次数:20673 次

看的电子工业出版社的《jsp宝典》,有一例程照书上的方法做始终运行出错,求助各位了
运行环境是tomcat+mysql
开发环境是eclipse+lomboz

下面是照书上的例程改过的代码,为了让大家更容易看明白,其中//部门就是造成错误的地方:

<%@   page   contentType= "text/html;   charset=gb2312 "   language= "java "   import= "java.sql.* "   errorPage= " "   %>
<!DOCTYPE   HTML   PUBLIC   "-//W3C//DTD   HTML   4.01   Transitional//EN "   "http://www.w3.org/TR/html4/loose.dtd ">
<html>
<head>
<meta   http-equiv= "Content-Type "   content= "text/html;   charset=gb2312 ">
<title> 数据库操作 </title>
</head>
//连接数据库
<%
String   product_name= " ";

Connection   con=null;
Statement   sm=null;
ResultSet   rs=null;
try{
Class.forName( "com.mysql.jdbc.Driver ").newInstance();
String   url= "jdbc:mysql://localhost/mydb ";
con=DriverManager.getConnection(url, "root ", "12345 ");
sm=con.createStatement();
rs=sm.executeQuery( "select   *   from   products ");
}catch(Exception   e){
e.printStackTrace();
}
%>
//读取数据库记录
// <%
//while(rs.next()){
//product_name=rs.getString( "PRODUCT_NAME ");
//}
//%>
<%=product_name%>
<body>

</body>
</html>


如果去掉//
//读取数据库记录
// <%
//while(rs.next()){
//product_name=rs.getString( "PRODUCT_NAME ");
//}
//%>
此部分造成的错误提示如下:

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
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:372)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


root   cause  

java.lang.NullPointerException
org.apache.jsp.index_jsp._jspService(index_jsp.java:70)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:94)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:324)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:292)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:236)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)


note   The   full   stack   trace   of   the   root   cause   is   available   in   the   Apache   Tomcat/5.0.30   logs.


--------------------------------------------

Apache   Tomcat/5.0.30

------解决方案--------------------