How to solve runtime exception in browser while running JSP?
While I try to run the following code
<html>
<head>
<title>JSP Test</title>
</head>
<body>
<form action="KmlSample.jsp">
<input type="submit" name="submit" value="Click Me" />
</form>
</body>
</html>
where KmlSample.jsp is:
<%@ 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 %>开发者_如何学C;
<%@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();
}
}
%>
it gave following error messages on click of button on webbrowser:
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)
How can I resolve it?
<%@ page import java.io.BufferedReader %>
Your import statements are wrong. They should look like
<%@ page import="java.io.BufferedReader" %>
Better yet, don't use JSP for this. Use a Servlet. Then you can write Java code the normal way without hassling with scriptlets and hard-to-debug problems. It's relatively simple, create a class which extends HttpServlet
, put all that Java code inside doGet()
method, map the servlet in web.xml
on an url-pattern
of /KmlSample
and change your form action to
<form action="KmlSample">
See also:
- Servlets info page
This is the problem:
static String inputFile = "kmlexamples.txt";
static String outputFile = "output.kml";
The static
keyword makes no sense here, take it out.
精彩评论