How to use a Java class in JSP scriptlet? Error says the class cannot be resolved to a type

Joy picture Joy · Mar 30, 2013 · Viewed 68.9k times · Source

I have written a sample JSP file in Eclipse and a Java file and was trying to call the Java class inside my JSP but it is not working. The code of the JAVA file is as follows:

TestJava.jva

public class TestJava {
     public void test(String msg)
      {
          System.out.println("My name is "+msg);
      }
}

The Javafile is located at src folder. My JSP file test.jsp is as follows:

test.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

 <html>
 <head>
     <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
     <title>My First JSP with JAVA</title>
 </head>
 <body>
 <jsp:useBean id="link" scope="application" class = "TestJava" />   
  <% TestJava t=new TestJava();
  t.test("Joy");
 %>
 </body>
 </html>

It is giving error as "TestJava cannot be resolved to a type". I have studied other related posts in Stack Overflow but those approaches also did not work. Being new to JSP I cannot understand how to fix that error. So I am asking if anyone can help me to fix that problem.

Thank you.

Answer

Erik Kaju picture Erik Kaju · Mar 30, 2013

In order to use class objects in java, you need to import classes first. Pretty much the same with scriplets in jsp, here you import it via <%@ page %> scriplet tags.

<%@ page import="your.class*" %>