插入非法元素

时间:2013-09-05 11:53:32

标签: java html jsp

这是我的JSP页面。单击按钮,我将调用此JSP页面,该页面将HTML文档转换为PDF文档。

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1" import="java.io.*,com.itextpdf.text.*,com.itextpdf.tool.xml.*,com.itextpdf.text.pdf.PdfWriter"%>

<!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>
<%
//step 1

Document document = new Document();

// step 2

PdfWriter writer = PdfWriter.getInstance(document, new FileOutputStream("pdf.pdf"));
// step 3

document.open();

// step 4

XMLWorkerHelper.getInstance().parseXHtml(writer, document,new FileInputStream("C:/Documents and Settings/preetish/Desktop/practice/Buttons.html"));

//step 5

 document.close();

%>

</body>

</html>

但是当我在Apache Tomcat服务器上运行index.jsp时,它会给我以下错误日志:

SEVERE: Servlet.service() for servlet [jsp] in context with path [/paginanation] threw exception [An exception occurred processing JSP page /pfd.jsp at line 19

16: // step 3
17: document.open();
18: // step 4
19: XMLWorkerHelper.getInstance().parseXHtml(writer, document,new FileInputStream("C:/Documents and Settings/preetish/Desktop/practice/Buttons.html"));
20: //step 5
21:  document.close();
22: 


Stacktrace:] with root cause
java.lang.ClassCastException: Insertion of illegal Element: 30
    at com.itextpdf.text.Phrase.add(Phrase.java:367)
    at com.itextpdf.text.Paragraph.add(Paragraph.java:345)
    at com.itextpdf.tool.xml.html.Div.end(Div.java:117)
    at com.itextpdf.tool.xml.html.AbstractTagProcessor.endElement(AbstractTagProcessor.java:189)
    at com.itextpdf.tool.xml.pipeline.html.HtmlPipeline.close(HtmlPipeline.java:206)
    at com.itextpdf.tool.xml.XMLWorker.endElement(XMLWorker.java:141)
    at com.itextpdf.tool.xml.parser.XMLParser.endElement(XMLParser.java:395)
    at com.itextpdf.tool.xml.parser.state.ClosingTagState.process(ClosingTagState.java:70)
    at com.itextpdf.tool.xml.parser.XMLParser.parseWithReader(XMLParser.java:235)
    at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:213)
    at com.itextpdf.tool.xml.parser.XMLParser.parse(XMLParser.java:174)
    at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:223)
    at com.itextpdf.tool.xml.XMLWorkerHelper.parseXHtml(XMLWorkerHelper.java:185)
    at org.apache.jsp.pfd_jsp._jspService(pfd_jsp.java:76)
    at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:419)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:391)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:304)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:210)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:240)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:164)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:462)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:164)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:562)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:395)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:250)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:188)
    at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:302)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
    at java.lang.Thread.run(Unknown Source)

2 个答案:

答案 0 :(得分:1)

您忘记导入Document类。

<% page import="class">

这是在jsp页面中导入的方法

答案 1 :(得分:0)

你自己正在使用松散的HTML4(至少在JSP中),并且正在尝试转换XHTML。

  • 一般情况下:制作一个独立的应用程序(库)以便更容易开发。
  • 将原始HTML4页面转换为XHTML,并验证页面(Tiny Validator,任何浏览器插件。)

错误可能是非法元素,例如

<b><p>lorem</p><p>ipsum</p></b>

b标签内不允许使用p标签。

如您所知,XHTML需要将单身人士写成<img .../>。对于HTML,您可以在/>之前留出空格。打开的代码<p>需要一个clöosing标记</p>