如何在运行JSP时解决浏览器中的运行时异常?

时间:2011-03-03 10:48:54

标签: java jsp

我尝试运行以下代码

<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();
    }

  }

%>

它在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)

我该如何解决?

2 个答案:

答案 0 :(得分:1)

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

您的导入声明错误。它们应该看起来像

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

更好的是,don't使用JSP。使用Servlet。然后,您可以正常方式编写Java代码,而无需麻烦 scriptlet 和难以调试的问题。它相对简单,创建一个extends HttpServlet的类,将所有Java代码放在doGet()方法中,将web.xml中的servlet映射到url-pattern的{​​{1}}和将表单操作更改为

/KmlSample

另见:

答案 1 :(得分:0)

这是问题所在:

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

static关键字在这里没有任何意义,请将其删除。