简体   繁体   中英

Error in JSP in calling java class

I have created a sample jsp file and a java class. i am calling a java class in jsp file. it is giving the error. "org.apache.jasper.JasperException: Unable to compile class for JSP: " below is my code.

<html>
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <title>JSP Page</title>
</head>
<body>
    <%
        testClass tc = new testClass();
        tc.testMethod();
    %>
    <h1>Hello World!</h1>
</body>

java class

public class testClass {

public void testMethod(){
    System.out.println("Hello");
}
}

You have to import your java class in your JSP at the beginning. Like this-

<%@ page import="<your package structure here>.testClass"%>

before <html> .

Also close your html tag at the end. It seems unclosed.

Need to mention, your class name starts a with lowercase letter. Its a good practice to give your class, a name starting with upper case letter.

User JSP usebean

<jsp:useBean id="test" class="testClass" /><%
test.testMethod();%>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM