gpt4 book ai didi

java - JSP运行时浏览器运行异常如何解决?

转载 作者:行者123 更新时间:2023-12-01 15:55:22 24 4
gpt4 key购买 nike

当我尝试运行以下代码时

<html>
<head>
<title>JSP Test</title>
</head>
<body>
<form action="KmlSample.jsp">
<input type="submit" name="submit" value="Click Me" />
</form>
</body>
</html>

其中 KmlSample.jsp 是:

<%@ page language="java" %>
<%@ page import java.io.BufferedReader %>
<%@ page import java.io.File%>
<%@ page import java.io.FileNotFoundException %>
<%@ page import java.io.FileOutputStream %>
<%@ page import java.io.FileReader%>
<%@ page import java.io.IOException%>

<%@page import org.jdom.Document %>
<%@page import org.jdom.Element %>
<%@page import org.jdom.Namespace %>
<%@page import org.jdom.output.Format %>
<%@page import org.jdom.output.XMLOutputter %>
<%

static String inputFile = "kmlexamples.txt";
static String outputFile = "output.kml";

Namespace ns = Namespace.getNamespace("", "http://earth.google.com/kml/2.2");

Element kml = new Element("kml", ns);
Document kmlDocument = new Document(kml);

Element document = new Element("Document", ns);
kml.addContent(document);

Element name = new Element("name", ns);
name.setText("Java Generated KML Document");
document.addContent(name);

Element style = new Element("Style", ns);
style.setAttribute("id", "redIcon");
document.addContent(style);

Element iconStyle = new Element("IconStyle", ns);
style.addContent(iconStyle);

Element color = new Element("color", ns);
color.setText("990000ff");
iconStyle.addContent(color);

Element icon = new Element("Icon", ns);
iconStyle.addContent(icon);

Element href = new Element("href", ns);
href.setText("http://www.cs.mun.ca/~hoeber/teaching/cs4767/notes/02.1-kml/circle.png");
icon.addContent(href);


File file = new File(inputFile);
BufferedReader reader;
try {
reader = new BufferedReader(new FileReader(file));
try {
String line = reader.readLine();
while (line != null) {
String[] lineParts = line.split(";");
if (lineParts.length == 3) {

Element placemark = new Element("Placemark", ns);
document.addContent(placemark);

Element pmName = new Element("name", ns);
pmName.setText(lineParts[0].trim());
placemark.addContent(pmName);

Element pmDescription = new Element("description", ns);
pmDescription.setText(lineParts[1].trim());
placemark.addContent(pmDescription);

Element pmStyleUrl = new Element("styleUrl", ns);
pmStyleUrl.setText("#redIcon");
placemark.addContent(pmStyleUrl);

Element pmPoint = new Element("Point", ns);
placemark.addContent(pmPoint);

Element pmCoordinates = new Element("coordinates", ns);
pmCoordinates.setText(lineParts[2].trim());
pmPoint.addContent(pmCoordinates);

}

line = reader.readLine();
}

} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}

try {
XMLOutputter outputter = new XMLOutputter(Format.getPrettyFormat());
FileOutputStream writer = new FileOutputStream(outputFile);
outputter.output(kmlDocument, writer);
writer.close();
} catch (IOException e) {
e.printStackTrace();
}

}

%>

单击网络浏览器上的按钮时出现以下错误消息:

org.apache.jasper.JasperException: /KmlSample.jsp(2,16) equal symbol expected
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:88)
org.apache.jasper.compiler.Parser.parseAttribute(Parser.java:195)
org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:150)
org.apache.jasper.compiler.Parser.parseAttributes(Parser.java:162)
org.apache.jasper.compiler.ParserController.getPageEncodingForJspSyntax(ParserController.java:451)
org.apache.jasper.compiler.ParserController.determineSyntaxAndEncoding(ParserController.java:392)
org.apache.jasper.compiler.ParserController.doParse(ParserController.java:173)
org.apache.jasper.compiler.ParserController.parse(ParserController.java:103)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:167)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:306)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:316)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:336)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:265)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

如何解决?

最佳答案

<%@ page import java.io.BufferedReader %>

您的导入语句是错误的。它们应该看起来像

<%@ page import="java.io.BufferedReader" %>

更好的是,don't为此使用 JSP。使用 Servlet。然后您就可以以正常方式编写 Java 代码,而不必担心 scriptlet 和难以调试的问题。相对简单,创建一个扩展 HttpServlet 的类,将所有 Java 代码放入 doGet() 方法中,在 web.xml 中映射 servlet在 /KmlSampleurl-pattern 上并将表单操作更改为

<form action="KmlSample">

另请参阅:

关于java - JSP运行时浏览器运行异常如何解决?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5179702/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com