java JSP 编译失败,并显示“未为类型 [ClassName] 定义方法 [MethodSignature]”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3566574/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 02:26:45  来源:igfitidea点击:

JSP compilation fails with "The method [MethodSignature] is undefined for the type [ClassName]"

javajsp

提问by LGAP

 An error occurred at line: 9 in the jsp file: /Test1.jsp
The method addURL(String, String) is undefined for the type SearchLink
6: 
7:  if(url1!=null && url1.trim().length()!=0){
8:      myfirst.SearchLink p=new myfirst.SearchLink();
9:      String result=p.addURL(url1,source1);
10:         out.println(result);
11:         System.out.println(result);
12:     }else{

Please let me know if you need any further details for answering the error cause. Thanks in advance

如果您需要更多详细信息来回答错误原因,请告诉我。提前致谢

The complete jsp coding is as below.

完整的jsp编码如下。

<%@ page import="myfirst.*" %>
<%

String url1=request.getParameter("url");
String source1=request.getParameter("source");

if(url1!=null && url1.trim().length()!=0){
    myfirst.SearchLink p=new myfirst.SearchLink();
    String result=p.addURL(url1,source1);
    out.println(result);
    System.out.println(result);
}else{
    System.out.println("Not a valid url");
    out.println("Not a valid url");
}
%>

And this is my Java code named SearchLink and I have compiled this version too..

这是我的名为 SearchLink 的 Java 代码,我也编译了这个版本..

 package myfirst;

 import java.net.URL;
 import java.net.URLConnection;
 import java.sql.*;
 public class SearchLink{
public static void main(String args[]) throws Exception {
    //String link="http://hosted.ap.org";
}

public String checkURL(String link,String source)throws SQLException{
    Connection con=null;
    Statement stmt=null;
    Statement stmtR=null;
    //link="http://www.topix.com/rss/city/ellensburgwa";
    //String source="Sample";
    if(con==null){
            SQLConnection.setURL("jdbc:sqlserver://192.168.2.53\SQL2005;user=sa;password=365media;DatabaseName=LN_ADWEEK");
            con=SQLConnection.getNewConnection();
            stmt=con.createStatement();
            stmtR=con.createStatement();
    }
    try{
        ResultSet rs;
        boolean hasRows=false;
        rs=stmt.executeQuery("select url from urlbckup where url='"+link+"'");
        while(rs.next()){
            hasRows=true;
            //String mem=rs.getString(1);
            rs.close();
            return "This URL already exists in DB";
        }
        rs.close();
        if (!hasRows)
        {

        }
        return "This URL does not exist in DB";
    }catch(Exception e){
        e.printStackTrace();
        return e.getMessage();
    }finally{
        if(stmtR!=null){
            stmtR.close();
        }
        if(stmt!=null){
            stmt.close();
        }
        if(con!= null){
            con.close();
        }
    }
}


public String addURL(String link,String source)throws SQLException{
        Connection con=null;
        Statement stmt=null;
        Statement stmtR=null;
        if(con==null){
                SQLConnection.setURL("jdbc:sqlserver://192.168.2.53\SQL2005;user=sa;password=365media;DatabaseName=LN_ADWEEK");
                con=SQLConnection.getNewConnection();
                stmt=con.createStatement();
                stmtR=con.createStatement();
    }
        try{
            PreparedStatement insertUrlStatement = con.prepareStatement("INSERT INTO urlbckup VALUES(?, ?, ?, ?, ?)");
            insertUrlStatement.setInt(1, 21211);
            insertUrlStatement.setString(2, link);
            insertUrlStatement.setString(3, source);
            insertUrlStatement.setInt(4, 1);
            insertUrlStatement.setInt(5, 0);
            insertUrlStatement.executeUpdate();
            insertUrlStatement.close();
            return "The URL has been added to the Database";}
        catch(Exception e){
            e.printStackTrace();
            return e.getMessage();
        }finally{
            if(stmtR!=null){
                stmtR.close();
            }
            if(stmt!=null){
                stmt.close();
            }
            if(con!= null){
                con.close();
            }
    }


}

}

}

回答by Colin Hebert

You should redeploy your application.

您应该重新部署您的应用程序。

In most IDEs, when you deploy web applications jsp pages can still be edited and "redeployed" on the fly onto the web container. But with classes, you need to recompile your code and redeploy your application.

在大多数 IDE 中,当您部署 Web 应用程序时,jsp 页面仍然可以被编辑并动态“重新部署”到 Web 容器上。但是对于类,您需要重新编译代码并重新部署应用程序。

Now some IDE and auto redeploy your application on save. And there are projects like jrebelwhich allow you to change classes into your application without redeploying it.

现在一些 IDE 并在保存时自动重新部署您的应用程序。还有像jrebel这样的项目,它允许您将类更改到您的应用程序中,而无需重新部署它。

回答by Stephen C

Assuming that myfirst.SearchLinkis a fully qualified class name, the compiler is telling you that the SearchLinkclass does not define a method called addURLthat takes two Stringarguments.

假设这myfirst.SearchLink是一个完全限定的类名,编译器会告诉您SearchLink该类没有定义一个addURL接受两个String参数的方法。

Possible causes:

可能的原因:

  • you have misspelled the name of the method
  • you have forgotten to declare the method
  • you have declared the method with arguments with the wrong types
  • you have called the method with arguments of the wrong types.
  • 您拼错了方法的名称
  • 你忘记声明方法
  • 您已经使用错误类型的参数声明了该方法
  • 您使用错误类型的参数调用了该方法。

EDIT

编辑

None of the above seem to apply, so my next guess is that you have neglected to compile and deploy the latest version of the SearchLinkclass to your web server.

以上似乎都不适用,所以我的下一个猜测是您忽略了将SearchLink类的最新版本编译和部署到您的 Web 服务器。

回答by Jeroen Rosenberg

it means that the SearchLink class doesn't have a method with the signature addURL(String, String).

这意味着 SearchLink 类没有带有签名的方法 addURL(String, String)。