Reputation: 129
I have following JAVA class named as Demo of com package
package com;
public class Demo
{
public String getMessage()
{
return "hello";
}
}
now i want to call a method of Demo class in my index.jsp page
<%@ 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>
but i get this error by my Apache 7.0.35 server in Eclipse Kepler
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>
so please help me to resolve this issue
Here is the Stack Trace
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)
here is the root cause
root cause
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)
Upvotes: 0
Views: 6804
Reputation: 9872
Your code looks right at first sight. The logical explanation would be that you have not packaged your Demo.class in the right place in the web application. Your .war (or exploded directory) should look like this:
application.war/
index.jsp
/WEB-INF/web.xml
/WEB-INF/classes/com/Demo.class
... additional stuff
That's the minimum necessary configuration, have a look at it and look if there is something missing
Edited to add the real answer after exchanging some ideas with the OP:
Actually the problem was that the code was compiled with a more recent version of the java compiler than the runtime environment used in the server, thus the java.lang.UnsupportedClassVersionError
reported by the OP.
Upvotes: 1
Reputation: 12993
Do not write scriptlets in JSP, because scriptlets shouldn't be used in JSPs for more than a decade. Learn the JSP EL, the JSTL, and use servlet for the Java code.
See How to avoid Java Code in JSP-Files?
You can use Java bean, best explained here is link
Modify the class as per Java bean standard
package com;
public class Demo
{
private String message;
public String getMessage()
{
return message;
}
public void setMessage(String message)
{
this.message = message;
}
}
<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"/>
Upvotes: 1