首页 > 解决方案 > 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

1st Error page 2nd Error page

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...

标签: javajsp

解决方案


您的 com.DB 包只有一个类,即 DBConnect。您可以导入直到 com.DB.*,而不是在 jsp 中获取连接对象并在 jsp 中打印,而是从您的 java 类中的一种方法返回连接对象的字符串表示或字符串值,然后在此处打印。


推荐阅读