如何在 JSP scriptlet 中使用 Java 类?错误表示该类无法解析为类型 [英] How to use a Java class in JSP scriptlet? Error says the class cannot be resolved to a type

查看:38
本文介绍了如何在 JSP scriptlet 中使用 Java 类?错误表示该类无法解析为类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在 Eclipse 中编写了一个示例 JSP 文件和一个 Java 文件,并试图在我的 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);
      }
}

Java 文件位于 src 文件夹中.我的JSP文件test.jsp如下:

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>

它给出的错误是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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆