java - 扩展页面指令不起作用

标签 java jsp

您好,我正在检查页面指令,但扩展页面指令不起作用。
下面是我的 jsp 页面

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page extends="javapackage.TestClass" %>
<!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>Hai
<% out.println(show()); %>
</body>
</html>

我的类(class)是

package javapackage;

public class TestClass {
  public String show(){
      return "Welcome Prasad";
 }
}


它给了我 500 错误
异常

org.apache.jasper.JasperException: java.lang.ClassCastException: org.apache.jsp.first_jsp cannot be cast to javax.servlet.Servlet
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:177)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
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:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)

root cause 

  java.lang.ClassCastException: org.apache.jsp.first_jsp cannot be cast to javax.servlet.Servlet
org.apache.jasper.servlet.JspServletWrapper.getServlet(JspServletWrapper.java:172)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:369)
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:727)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)


即使我尝试使用扩展 HttpServlet 的类,但没有用。所以请任何人帮助我。我正在使用 Tomcat 7
谢谢

最佳答案

您需要阅读 JSP 规范,特别是 JSP.11.2.4 部分,其中列出了与扩展一起使用的父类(super class)的许多要求,但您问题中的父类(super class)均不满足这些要求。

...the provided superclass:

  • Implements HttpJspPage if the protocol is HTTP, or JspPage otherwise.
  • All of the methods in the Servlet interface are declared final.

Additionally, it is the responsibility of the JSP page author that the provided superclass satisfies:

  • The service method of the servlet API invokes the _jspService method.
  • The init(ServletConfig) method stores the configuration, makes it available via getServletConfig, then invokes jspInit.
  • The destroy method invokes jspDestroy.

关于java - 扩展页面指令不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26114432/

相关文章:

java - 在客户端/服务器应用程序中刷新 JTable

java - 使用 Selenium JAVA 代码读取具有 <SPAN> 标签的列

javascript - 基于javascript变量渲染jsp内容

java - 为什么在jsp上调用session会返回错误?

java - 使用显示标签导出到 Excel 时如何删除单选按钮?

java - ConcurrentModificationException 从迭代器中删除元素

java - 组织.json.JSONException : A JSONArray text must start with '['

java - JAVA中如何知道ConcurrentHashMap上没有运行任何操作或者处于Idle状态?

java - 将 bean 传递给 servlet

java - 如何配置 video.js html5-video 包装器的样式?