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

周靖峰 周靖峰 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我的文件目录为:

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这个类吧,请问各位高手们应该怎么做才能解决这个问题
...全文

相关推荐

评论 6
收藏 已收藏
分享
返回顶部
周靖峰
Web 开发 81,107 成员 341,720 内容