-12

I have this in the File.java:

 public static void main(String args[]) throws Exception_Exception {
    URL wsdlURL = CallSService.WSDL_LOCATION;
    if (args.length > 0) { 
        File wsdlFile = new File(args[0]);
        try {
            if (wsdlFile.exists()) {
                wsdlURL = wsdlFile.toURI().toURL();
            } else {
                wsdlURL = new URL(args[0]);
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();

and I want to transfer this to a JSP file, so I do like that:

List<String> Search(String keyS){

if(keyS!=null){
QName SERVICE_NAME = new QName("http://ts.search.com/", "callSService");





String arg=??????????????;

URL wsdlURL = CallSService.WSDL_LOCATION;

File wsdlFile = new File(arg);
try {
    if (wsdlFile.exists()) {
        wsdlURL = wsdlFile.toURI().toURL();
    } else {
        wsdlURL = new URL(arg);
    }
} catch (MalformedURLException e) {
    e.printStackTrace();
}

I want to replace the args[0] with arg. What does (String args[]) mean? and how can I replace it ?

4

2 回答 2

8

String args[] is an array of Strings passed in from the command line.

So, if you started your app with java MyApp arg1 arg2

Then args[] would contain => ["arg1", "arg2"]

Java will automatically split up arguments separated by spaces, which is how it knows how many arguments you passed in.

于 2011-11-21T18:45:05.427 回答
4

不要在 JSP 中这样做:(

不要将您的功能放在 a 中main,这会令人困惑:public static void main通常是程序的入口点,而不是通用方法。您可以其用作一个,但 IMO 它具有误导性。

相反,创建一个可以使用所需参数调用的实例方法。它可能是一种static方法,但这会产生一些不灵活性,使测试变得更加困难。将代码嵌入到 JSP 中也会增加测试难度。

ServletContext.getRealPath()除非您提供绝对路径,否则您需要使用获取相对于 Web 应用程序的文件。如果文件“嵌入”在应用程序中(在类路径上),您将需要使用其中一种resourceAsStream变体。

于 2011-11-21T18:46:53.773 回答