为什么我不能在JSP页面中创建一些JAVA类的对象

时间:2013-12-24 14:16:01

标签: java jsp

我有以下JAVA类命名为com包的演示

package com;
public class Demo 
{
public String getMessage()
{
    return "hello";
}
}

现在我想在index.jsp页面中调用Demo类的方法

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-    8859-1"%>
<%@ page import="com.Demo" %>

<!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>Service Call Management</title>
</head>
<body>
<h1>
<% 
Demo d = new Demo();
out.println(d.getMessage());    
%> 
</h1>
</body>
</html>

但是我在Eclipse Kepler的Apache 7.0.35服务器上得到了这个错误

type Exception report

message An exception occurred processing JSP page /index.jsp at line 13

description The server encountered an internal error that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: An exception occurred processing JSP page /index.jsp  at line 13

10: <body>
11: <h1>
12: <% 
13:     Demo d = new Demo();
14:     out.println(d.getMessage());    
15: %>
16: </h1>

所以请帮我解决这个问题

这是堆栈跟踪

Stacktrace:
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:568)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:455)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

这是根本原因

根本原因

javax.servlet.ServletException: java.lang.UnsupportedClassVersionError: com/Demo :   Unsupported major.minor version 51.0 (unable to load class com.Demo)
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.java :912)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:841)
org.apache.jsp.index_jsp._jspService(index_jsp.java:90)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

root cause

java.lang.UnsupportedClassVersionError: com/Demo : Unsupported major.minor version 51.0 (unable to load class com.Demo)
org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:2908)
org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:1173)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1681)
org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1559)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:126)
org.apache.jasper.servlet.JasperLoader.loadClass(JasperLoader.java:63)
java.lang.ClassLoader.loadClassInternal(Unknown Source)
org.apache.jsp.index_jsp._jspService(index_jsp.java:76)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:728)

2 个答案:

答案 0 :(得分:2)

您的代码一见钟情。逻辑上的解释是您没有将Demo.class打包到Web应用程序中的正确位置。您的.war(或展开目录)应如下所示:

application.war/
               index.jsp
               /WEB-INF/web.xml
               /WEB-INF/classes/com/Demo.class
... additional stuff

这是最低限度的必要配置,查看它并查看是否缺少某些内容

在与OP交换一些想法后编辑添加真实答案:

实际上问题是代码是使用java编译器的更新版本编译的,而不是服务器中使用的运行时环境,因此OP报告了java.lang.UnsupportedClassVersionError。 / p>

答案 1 :(得分:1)

不要在JSP中编写scriptlet ,因为scriptlet不应该在JSP中使用超过十年。学习JSP ELJSTL,并使用servlet作为Java代码 见How to avoid Java Code in JSP-Files?

解决方案

您可以使用Java bean,最好解释here is link
根据Java bean标准修改类

package com;
public class Demo 
{
  private String message;
  public String getMessage()
  {
     return message;
  }

  public void setMessage(String message)
  {
     this.message = message;
  }
}

JSP

<jsp:useBean id="demo" class="com.Demo" scope="page">
   <jsp:setProperty name="demo" property="message" value="hello" />
</jsp:useBean>

//to retrieve value
<jsp:getProperty name="demo" property="message"/>