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

6

10

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.

Judas answered 30/3, 2013 at 10:18 Comment(5)
I think TestJava should be a managedbean. I am not 100% sure.Eiser
You should import your java class in jsp file!Carnahan
@KorayTugay managed bean concept is for JSF AFAIK, but it's not applied to plain JSP/Servlet development.Kandi
Good to know you have an answer, but you must have in mind that scriptlets are discouraged to use. Refer to How to avoid Java Code in JSP-Files? for more info.Kandi
@LuiggiMendoza Thank you, and sorry for the wrong information!Eiser
L
7

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*" %>

Lithoid answered 30/3, 2013 at 10:20 Comment(7)
I have put the name of the class as you said "<%@ page import="TestJava" %>". But still it is showing that "TestJava cannot be resolved".Judas
Specify the package relation as well. Provide some more info about where you hold your class and we will solve it. If you work with eclipse: right click on your java classfile -> properties -> "path" (You can copy the path here)Lithoid
@Joy: Are you sure that the TestJava is in default package?Carnahan
I have put my TestJava class in HelloWorld/src folder.Judas
A bit unclear, do you have any packages or just src folder? Create another temporary java class and add TestJava t=new TestJava(); there. Eclipse will propose to import a class or will do it automatically. Then you will see how it should look like in jsp.Lithoid
Actually I have created the class file in a default package. Is it may be the reason of the error?Judas
Thank you all for your reply :) Yes I have fixed it. I think accessing class from the default package was the main reason behind the error.Judas
R
5

You have to write fully qualified name of your class in page directive

<%@ page import="fully qualified name of the class" %>
Rifle answered 30/3, 2013 at 10:26 Comment(2)
Yes.. I hv imported <%@ page import="com.sampleServlet.sampleClass" %>Minerva
so whats is your question?Rifle
S
2

You need to import your class using <%@ page %>

In your case, import Test in your jsp page like this.

<%@ page import="yourpackagename.Test" %>

if you want to import multiple classes that are in different packages declare them like this.

<%@ page import="yourpackagename.Test,yourpackagename2.Test2" %>

Also, I highly suggest you put your Test class outside the default package, and put it in another package.

Shelly answered 30/3, 2013 at 11:24 Comment(0)
F
1

<%@ page import="TestJava" %> Make sure that your TestJava is in the classpath

Flagelliform answered 30/3, 2013 at 10:30 Comment(0)
G
1

Make sure about the @import as others said

and then The "class" attribute specifies the actual class of the bean instance.

 <jsp:useBean id="link" scope="application" class = "fullpackagename.TestJava" />
      <% TestJava t=new TestJava();
      t.test("Joy");
     %>
Grove answered 30/3, 2013 at 11:1 Comment(0)
V
0

Which IDE are you using? I recommend you use something like Eclipse with the JSP plugin. It will underline with a red objects that you attempt to declare that have not been compiled and imported.

You don't have to use beans by the way.. you can just create java objects and import them

Vue answered 12/2, 2016 at 19:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.