如何在JSP scriptlet中使用Java类?错误说类无法解析为类型 [英] How to use a Java class in JSP scriptlet? Error says the class cannot be resolved to a type
问题描述
我已经在Eclipse和Java文件中编写了一个示例JSP文件,并试图调用JSP中的Java类,但它不工作。 JAVA文件的代码如下:
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);
}
}
Javafile位于 src
文件夹。我的JSP文件 test.jsp
如下:
The Javafile is located at src
folder. My JSP file test.jsp
is as follows:
test.jsp p>
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>
它会给出错误 TestJava无法解析为类型 。我已经研究了其他相关的帖子在Stack Overflow,但是这些方法也没有工作。作为新的JSP我不能理解如何解决这个错误。所以我问是否有人可以帮我解决这个问题。
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.
谢谢。
推荐答案
为了在java中使用类对象,你需要先导入类。
与jsp中的scriplet非常相似,您可以通过<%@ page%>
scriplet标签导入。
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 *%>
这篇关于如何在JSP scriptlet中使用Java类?错误说类无法解析为类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!