Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 462
Joy Avatar asked Mar 30 '13 10:03

Joy


People also ask

Can not be resolved to a type JSP?

You need a type declaration there.. something like Movie movie = (Movie) movies. get(i). The better thing to do is use Generics and define a type to the ArrayList (like ArrayList<Movie>()) or ...the best thing to do is to use EL and forget scriplets.

What is correct scriptlet?

In JavaServer Pages (JSP) technology, a scriptlet is a piece of Java-code embedded in the HTML-like JSP code. The scriptlet is everything inside the <% %> tags. Between these the user can add any valid Scriptlet i.e. any valid Java Code. In AppleScript, a scriptlet is a small script.

What is <%= some Java code %> in JSP?

A scriptlet tag is used to execute java source code in JSP. Syntax is as follows: <% java source code %>


3 Answers

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

like image 112
Erik Kaju Avatar answered Oct 02 '22 12:10

Erik Kaju


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

<%@ page import="fully qualified name of the class" %>
like image 20
Neha Choudhary Avatar answered Oct 02 '22 14:10

Neha Choudhary


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.

like image 38
KyelJmD Avatar answered Oct 02 '22 14:10

KyelJmD