1

我正在编写我的第一个 JSP 页面,并且正在尝试连接到 Google Weather API。所以我希望你能对我有耐心。这是我的 JSP 页面:

<%@ page import="controller.GoogleWeather" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!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>Zipcode - Weather</title>
        <%
            if(request.getParameter("zipcode") != null) {
                GoogleWeather gw = new GoogleWeather(request.getParameter("zipcode"));
                gw.getWeatherFromServer();
            }
        %>
    </head>
    ..... // body goes here.

这是我的控制器类GoogleWeather.java

package controller;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.ProtocolException;
import java.net.URL;
public class GoogleWeather {
    private float temp_f;
    private float humidity;
    private String condition;
    private String zipcode;

    public GoogleWeather(String zipcode) {
        this.zipcode = zipcode;
    }

    public void getWeatherFromServer() {
        HttpURLConnection connection = null;
          BufferedReader rd  = null;
          StringBuilder sb = null;
          String line = null;

          URL serverAddress = null;

          try {
              serverAddress = new URL("http://www.google.com/ig/api");
              //set up out communications stuff
              connection = null;

              //Set up the initial connection
              connection = (HttpURLConnection)serverAddress.openConnection();connection.setRequestProperty("weather", this.zipcode);
              connection.setRequestMethod("GET");
              connection.setDoOutput(true);
              connection.setReadTimeout(10000);

              connection.connect();

              //read the result from the server
              rd  = new BufferedReader(new InputStreamReader(connection.getInputStream()));
              sb = new StringBuilder();

              while ((line = rd.readLine()) != null)
              {
                  sb.append(line + '\n');
              }

              System.out.println(sb.toString());

          } catch (MalformedURLException e) {
              e.printStackTrace();
          } catch (ProtocolException e) {
              e.printStackTrace();
          } catch (IOException e) {
              e.printStackTrace();
          }
          finally
          {
              //close the connection, set all objects to null
              connection.disconnect();
              rd = null;
              sb = null;
              connection = null;
          }
    }

    public float getTemp_f() {
        return temp_f;
    }

    public void setTemp_f(float temp_f) {
        this.temp_f = temp_f;
    }

    public float getHumidity() {
        return humidity;
    }

    public void setHumidity(float humidity) {
        this.humidity = humidity;
    }

    public String getCondition() {
        return condition;
    }

    public void setCondition(String condition) {
        this.condition = condition;
    }
}

我得到的错误和堆栈跟踪如下。

An error occurred at line: 12 in the jsp file: /Weather.jsp
GoogleWeather cannot be resolved to a type
9: <%
10:
11: if(request.getParameter("zipcode") != null) {
12:     GoogleWeather gw = new GoogleWeather(request.getParameter("zipcode"));
13:     gw.getWeatherFromServer();
14: }
15:

Stacktrace:
    at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)
    at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:331)
    at org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:457)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:378)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:353)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:340)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:644)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:358)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:389)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:333)
    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:224)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:169)
    at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:472)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:168)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:100)
    at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:929)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:118)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:405)
    at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:964)
    at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:515)
    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)

我尝试查看一些在线资源,但它们非常混乱。

4

1 回答 1

0

根据上面 Dave Newton 的评论,在 /WEB-INF/classes 文件夹中部署类文件。

于 2012-05-07T18:56:47.013 回答