3

I know this question has been asked a lot of times, but so far, none of the answers previously given have worked for me, here is my code:

<html>
  <head>
    <title>Programa</title>
  </head>
  <body>
    <div>
      Introduzca una palabra a buscar:
      <input type="text" id="INPUT">

      <%@ page import="java.net.URL" %>
      <%@ page import="com.google.gson.*" %>
      <%@ page import="java.net.URLEncoder" %>
      <%@ page import="java.io.InputStreamReader" %>
      <%@ page import="java.io.InputStream" %>
      <%@ page import="java.io.Reader" %>
      <%@ page import="javax.swing.*" %>
      <%@ page import="java.awt.event.*;" %>

      <%!
      int min(int a,int b) {
          return (a>b)? b:a;
      }
      int edit_distance(String a,String b) {
          int n = a.length(), m = b.length(),costo;
          int[][] mat = new int[n+1][m+1];
          for(int i=0;i<=n;++i) mat[i][0] = i;
          for(int j=0;j<=m;++j) mat[0][j] = j;

          for(int i=1;i<=n;++i) {
            for(int j=1;j<=m;++j) {
              costo = a.charAt(i-1) == b.charAt(j-1)? 1 : 0;
              mat[i][j] = min(min(mat[i-1][j] + 1,mat[i-1][j-1] + costo),mat[i][j-1] + 1);
            }
          }
          return mat[n][m];
      }
      String resultados_de_la_busqueda(String search) { //Básicamente lo que hace esta función es devolverte una cadena con los resultados de la búsqueda
          StringBuffer RES = new StringBuffer("<html>");
          String google = "http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="; //El enlace para buscar en google
          String charset = "UTF-8";
          URL url;
          Reader reader;
          try {
            url = new URL(google + URLEncoder.encode(search, charset));
            try {
              reader = new InputStreamReader(url.openStream(), charset);
              GoogleResults results = new Gson().fromJson(reader, GoogleResults.class);
              for(int i=0;i<3;++i) {
                RES.append(results.getResponseData().getResults().get(i).getTitle());
                RES.append("<br/>");
                RES.append("<a href=\"");
                RES.append(results.getResponseData().getResults().get(i).getUrl());
                RES.append("\">" + results.getResponseData().getResults().get(i).getUrl() + "</a>");
                RES.append("<br/><br/>");
              }
            } catch(Exception e) {}
          } catch(Exception e) {}
          return RES.toString();
      }
      %>

      <%
      //Se supone que acá debo mostrar el resultado de la búsqueda...
      out.println(resultados_de_la_busqueda("taringa"));
      %>
    </div>
  </body>
</html>

And here is the error I'm getting:

org.apache.jasper.JasperException: No se puede compilar la clase para JSP: 

Ha tenido lugar un error en la línea: 47 en el archivo jsp: /Programa.jsp
GoogleResults cannot be resolved to a type
Gson cannot be resolved to a type

Any idea on what is causing the problem?

Edit 1:

This is the GoogleResults class:

import java.util.List;

public class GoogleResults {

  private ResponseData responseData;
  public ResponseData getResponseData() { return responseData; }
  public void setResponseData(ResponseData responseData) { this.responseData = responseData; }
  public String toString() { return "ResponseData[" + responseData + "]"; }

  static class ResponseData {
    private List<Result> results;
    public List<Result> getResults() { return results; }
    public void setResults(List<Result> results) { this.results = results; }
    public String toString() { return "Results[" + results + "]"; }
  }

  static class Result {
    private String url;
    private String title;
    public String getUrl() { return url; }
    public String getTitle() { return title; }
    public void setUrl(String url) { this.url = url; }
    public void setTitle(String title) { this.title = title; }
    public String toString() { return "Result[url:" + url +",title:" + title + "]"; }
  }
}
hinafu
  • 689
  • 2
  • 5
  • 13
  • 1
    What's the package name of your `GoogleResults` and `Gson` classes? From the posted code, I think you haven't imported. – Luiggi Mendoza Jul 18 '12 at 04:14
  • Also, it looks like you're in the learning process. I'll recommend you to read [How to avoid Java Code in JSP-Files?](http://stackoverflow.com/q/3177733/1065197), the answer written by BalusC will show you good practices to stop having these kind of problems in your JSPs. Buena suerte en tu proyecto! – Luiggi Mendoza Jul 18 '12 at 04:19
  • Gson is from the gson package: http://code.google.com/p/google-gson/ which I imported (hopefully) here: <%@ page import="com.google.gson.*" %> GoogleResults doesn't have a package. It's not that I don't want to learn, but I don't have time to do it, I already had the project done and the teacher told us to not to use swing, and instead use jsp, and well, that's pretty bad. – hinafu Jul 18 '12 at 04:20
  • Check that you have downloaded the jar and added it into your buildpath. Rebuild your proyect and redeploy it to update the changes. – Luiggi Mendoza Jul 18 '12 at 04:22
  • I have downloaded all the jar files, but honestly, I don't know where to put them, so I put them in every folder I could, here's a pic of what my project explorer looks like: http://pasteshack.net/images/313760001342585673.png – hinafu Jul 18 '12 at 04:28
  • Please join to this chat room http://chat.stackoverflow.com/rooms/14037/lima-peru to have a better talking about your issues. – Luiggi Mendoza Jul 18 '12 at 04:38

6 Answers6

7

Seems like its not picking up your class...

Do a Project>Clean and then Refresh it (F5).

Hoped this helped, here is where I got it from:

Click here

chandhooguy
  • 452
  • 3
  • 14
1

Sometimes if you are importing your project into a new workspace, this problem is encountered if JRE was different of the previous workspace frm the new workspace, try changing the jre system library in build path. It works out

1

This Happen to me after my java was automatically updated. Check The JRE is correctly set: RClick on the project , choose Build Path ->configure Build Path , Go to libraries Path and make sure JRE System library is properly configured.

Meltzer
  • 127
  • 2
  • 10
0

Always put your classes in packages.

nitind
  • 19,089
  • 4
  • 34
  • 43
0

Make sure your GoogleResults.class is being compiled under WEB-INF/classes/

Also it seems to me like you should import this file at the beginning of your .jsp.

Hope this help

Alex Beaupré
  • 2,670
  • 2
  • 14
  • 8
0

Create new project. Copy all files in this new project from old project. Delete old one and rename new project.

Thats it. It will work fine

vitralyoz
  • 570
  • 7
  • 14