在JSP中使用类对象

时间:2015-11-24 10:43:50

标签: jsp java-ee jsp-tags

我在java中创建了一个对象(类),并希望在jsp页面中使用它

我的jsp是

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="com.anshu.obj" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <%
        obj fooBar = new obj();
        System.out.println("Name is "+fooBar.nameOwener());
    %>
</body>
</html>

我的班级是

package com.anshu;
public class obj {
    public String nameOwener(){
        return "Anshu";
    }
}

但是它给出了错误

生成的java文件中line:6处发生错误 只能导入一种类型。 com.anshu.obj解析为一个包

jsp文件中的第12行:/index.jsp

发生错误

obj无法解析为类型

提前致谢:) 我对jsp很假 我在Windows 64位中使用jboss 7.1.1和eclipse luna

This is my folder structure image

1 个答案:

答案 0 :(得分:1)

测试了共享代码。它工作正常。

做一个干净的构建并尝试运行。没有问题。

我案例中的代码

<强> obj.java

package com.test;

public class obj {
    public String nameOwener(){
        return "mani";
    }

}

<强> test.jsp的

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ page import="com.test.obj" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
    <%
        obj fooBar = new obj();
        System.out.println("Name is "+fooBar.nameOwener());
    %>
</body>
</html>

<强>输出

Name is mani