如何在JSP中调用自己定义的类

周靖峰 2010-10-16 06:50:11
我的文件目录为:

test/
----WEB-INF/
--------lib/
------------mysql-connector-java-5.1.7-bin.jar
--------classes/
------------ConnMysql.class
----index.jsp

ConnMysql.java

import java.sql.*;

public class ConnMysql {
public static Connection getConnection(){
try {
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/test?user=root&password=root&useUnicode=true&characterEncoding=GBK";
return DriverManager.getConnection(url);
} catch (Exception e) {
System.out.print(e.getMessage());
return null;
}
}
}


index.jsp

<%@page import="java.sql.*" %>
<%@page import="ConnMysql" %>
<%
Connection conn = null;
Statement stat = null;
ResultSet rs = null ;

try{
conn = ConnMysql.getConnection();
stat = conn.createStatement();
rs = stat.executeQuery("SELECT * FROM test");
while (rs.next()){
out.print(rs.getString("id") + ":");
out.println(rs.getString("name"));
}
}catch(Exception e){
out.println(e.getMessage());
}
%>


但是打开index.jsp时显示错误

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: Unable to compile class for JSP:

An error occurred at line: 7 in the generated java file
The import ConnMysql cannot be resolved

An error occurred at line: 9 in the jsp file: /index.jsp
ConnMysql cannot be resolved
6: ResultSet rs = null ;
7:
8: try{
9: conn = ConnMysql.getConnection();
10: stat = conn.createStatement();
11: rs = stat.executeQuery("SELECT * FROM test");
12: while (rs.next()){


Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:349)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
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.29 logs.


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

Apache Tomcat/6.0.29


应该是说找不到ConnMysql这个类吧,请问各位高手们应该怎么做才能解决这个问题
...全文
155 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
周靖峰 2010-10-16
  • 打赏
  • 举报
回复
算了,我还是自己看书的了,情况太复杂,不好讲了
叮咚客 2010-10-16
  • 打赏
  • 举报
回复
试试新建一目录,将你的类考到目录下进行调用
周靖峰 2010-10-16
  • 打赏
  • 举报
回复
我照着1楼的说法改写了两个源文件

<%@page import="java.sql.*" %>
<%@page import="ConnMysql" %>
<%
Connection conn = null;
Statement stat = null;
ResultSet rs = null ;

try{
stat = ConnMysql.getStatement();
rs = stat.executeQuery("SELECT * FROM test");
while (rs.next()){
out.print(rs.getString("id") + ":");
out.println(rs.getString("name"));
}
rs.close();
stat.close();
conn.close();
}catch(Exception e){
out.println(e.getMessage());
}
%>


import java.sql.*;

public class ConnMysql {
public static Statement getStatement(){
try {
Class.forName("com.mysql.jdbc.Driver");
String url ="jdbc:mysql://localhost:3306/test?user=root&password=root&useUnicode=true&characterEncoding=GBK";
Connection conn = DriverManager.getConnection(url);
return conn.createStatement();
} catch (Exception e) {
System.out.print(e.getMessage());
return null;
}
}
}


对了,jsp的错误好像有两种
第一种:
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: An exception occurred processing JSP page /index.jsp at line 7

4: Connection conn = null;
5: Statement stat = null;
6: ResultSet rs = null ;
7:
8: try{
9: stat = ConnMysql.getStatement();
10: rs = stat.executeQuery("SELECT * FROM test");


Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
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
org.apache.jsp.index_jsp._jspService(index_jsp.java:61)
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.29 logs.


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

Apache Tomcat/6.0.29

第二种:
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: Unable to compile class for JSP:

An error occurred at line: 7 in the generated java file
The import ConnMysql cannot be resolved

An error occurred at line: 9 in the jsp file: /index.jsp
ConnMysql cannot be resolved
6: ResultSet rs = null ;
7:
8: try{
9: stat = ConnMysql.getStatement();
10: rs = stat.executeQuery("SELECT * FROM test");
11: while (rs.next()){
12: out.print(rs.getString("id") + ":");


Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:92)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:330)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:439)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:349)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:327)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:314)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:592)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:317)
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.29 logs.


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

Apache Tomcat/6.0.29


怎么错误还会变来变去?
周靖峰 2010-10-16
  • 打赏
  • 举报
回复
[Quote=引用 2 楼 coolbamboo2008 的回复:]
ConnMysql初始化了么?
[/Quote]
怎么初始化?
coolbamboo2008 2010-10-16
  • 打赏
  • 举报
回复
ConnMysql初始化了么?
不止鱼 2010-10-16
  • 打赏
  • 举报
回复
stat = conn.createStatement();
这句话出错,不应该这么写
1.既然你用封装,应该在ConnMysql里定义好自己的conn方法,然后直接用ConnMysql.getStatement(conn)里调用,
2.而且你在index里没有关闭相关的资源,这些关闭的方法应该放在ConnMysql类里,然后直接传参调用之
3.catch里最好把各个Exception写明,如SQLException,等等,不应图省事只写个Exception,否则程序执行效率很低,特别在大型的程序之下,跟别人把Exception写清楚的那些程序相比,这种差距就会体现出来了

81,094

社区成员

发帖
与我相关
我的任务
社区描述
Java Web 开发
社区管理员
  • Web 开发社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧