java - Exception org.apache.jasper.JasperException:Unable to compile class for JSP: An error in jsp file:[/index.jsp] conn and DBConnect cannot be resolved
问题描述
I'm using the latest TomCat version and I've recently installed Eclipse IDE for Java Developers - 2021-03
This is my class:this is my class package
package com.DB;
import java.sql.Connection;
import java.sql.DriverManager;
public class DBConnect {
private static Connection conn;
public static Connection getConn()
{
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/ebook-app","root","password");
} catch (Exception e) {
e.printStackTrace();
}
return conn;
}
}
So this is my jsp code for class:
<%@page import="com.DB.DBConnect.* " %>
<% Connection conn=DBConnect.getConn();//My error in this line.why?
out.println(conn);
%>
I'm getting Error like this:
HTTP Status 500 – Internal Server Error
Type Exception Report
Message Unable to compile class for JSP:
Description The server encountered an unexpected condition that prevented it from fulfilling the request.
Exception
org.apache.jasper.JasperException: Unable to compile class for JSP:
An error occurred at line: [33] in the jsp file: [/index.jsp]
Connection cannot be resolved to a type
30: </div>
31:
32: <%@page import="com.DB.DBConnect.* " %>
33: <% Connection conn=DBConnect.getConn();
34: out.println(conn);
35: %>
36:
An error occurred at line: [33] in the jsp file: [/index.jsp]
DBConnect cannot be resolved
30: </div>
31:
32: <%@page import="com.DB.DBConnect.* " %>
33: <% Connection conn=DBConnect.getConn();
34: out.println(conn);
35: %>
36:
Stacktrace:
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:103)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:213)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:482)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:392)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:362)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:346)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:605)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:400)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:378)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:326)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
Note The full stack trace of the root cause is available in the server logs.
Apache Tomcat/9.0.43
Simply, What my problem is ,I'm getting the Error in jsp: line 33; <% Connection conn=DBConnect.getConn(); //My error in this line.why?
I'm beginner for java,so please explain it in simple and clear way...
解决方案
您的 com.DB 包只有一个类,即 DBConnect。您可以导入直到 com.DB.*,而不是在 jsp 中获取连接对象并在 jsp 中打印,而是从您的 java 类中的一种方法返回连接对象的字符串表示或字符串值,然后在此处打印。
推荐阅读
- python - Keras - 一类 CNN - 每一步都有两个输入
- python - 进入第一个纪元后,Tensorflow 抛出 ValueError()
- typescript - 通用映射以及将值与键关联
- javascript - Map 以及如何在 JS 中访问对象键?
- javascript - 正则表达式 JS
- reactjs - 编译错误 Unexpected token '<' my first app React
- python - Django 测试 - ImportError/ModuleNotFoundError
- python - MySQL auto_increment 不为空
- macos - gfortran 编译器给出分段错误
- php - AJAX 加载到另一个具有变量的 div