簡體   English   中英

GoogleWeather無法解析為一種類型

[英]GoogleWeather cannot be resolved to a type

我正在編寫我的第一個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)

我嘗試查看一些在線資源,但它們非常令人困惑。

根據上面Dave Newton的評論,在/ WEB-INF / classes文件夾中部署類文件是可行的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM