2010-12-08 8 views
0
package hall; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import java.net.*; 

public class UrlConnect { 
    public static final String DOCTYPE = 
    "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"; 

    public static String headWithTitle(String title) { 
    return(DOCTYPE + "\n" + 
      "<HTML>\n" + 
      "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n"); 
    } 

    /** Read a parameter with the specified name, convert it to an int, 
     and return it. Return the designated default value if the parameter 
     doesn't exist or if it is an illegal integer format. 
    */ 

} 
public static void main(String[] args) throws Exception { 

    URL url = new URL("http://www.xyz.com"); 

    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
    conn.setConnectTimeout(5000); // 5 seconds 
    conn.setRequestMethod("GET");  
    conn.connect(); 
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 

    String line; 
    StringBuffer bf = new StringBuffer(); 
    while ((line = rd.readLine()) != null) { 
     bf.append(line); 
    } 
    conn.disconnect(); 

    //... pass bf to an XML parser and do your processing... 
} 

このJavaサーブレットコードをコンパイルする際にコンパイルエラーが発生しました。コーディングで私のエラーはどこにありますか?他のライブラリを含めるべきですか?このサーブレットコードのコンパイルエラー

これは私のエラーログ

UrlConnect.java:22: class, interface, or enum expected 
public static void main(String[] args) throws Exception { 
      ^
UrlConnect.java:26: class, interface, or enum expected 
    HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
^
UrlConnect.java:27: class, interface, or enum expected 
    conn.setConnectTimeout(5000); // 5 seconds 
^
UrlConnect.java:28: class, interface, or enum expected 
    conn.setRequestMethod("GET");  
^
UrlConnect.java:29: class, interface, or enum expected 
    conn.connect(); 
^
UrlConnect.java:30: class, interface, or enum expected 
    BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 
^
UrlConnect.java:32: class, interface, or enum expected 
    String line; 
^
UrlConnect.java:33: class, interface, or enum expected 
    StringBuffer bf = new StringBuffer(); 
^
UrlConnect.java:34: class, interface, or enum expected 
    while ((line = rd.readLine()) != null) { 
^
UrlConnect.java:36: class, interface, or enum expected 
    } 
^
UrlConnect.java:40: class, interface, or enum expected 
} 
^ 
11 errors 
+0

は、いくつかのエラーログを投稿、エラーを推測するその困難。 –

+2

これはサーブレットコードではなく、あなたのエラーは何ですか? –

+0

エラーログを掲載しました。 – Hick

答えて

0

てみています。

public static void main(String[] args) throws Exceptionより前に「}」を削除し、ファイルの最後に置きます。

0

a)mainは、クラス内にある必要があります。
b)一部のインポートがありませんでした。ここで

はあなたのコードが

package hall; 

import java.io.BufferedReader; 
import java.io.InputStreamReader; 
import javax.servlet.*; 
import javax.servlet.http.*; 
import java.net.*; 

public class UrlConnect { 

    public static final String DOCTYPE = 
      "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"; 

    public static String headWithTitle(String title) { 
     return (DOCTYPE + "\n" + 
       "<HTML>\n" + 
       "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n"); 
    } 

    /** Read a parameter with the specified name, convert it to an int, 
    and return it. Return the designated default value if the parameter 
    doesn't exist or if it is an illegal integer format. 
    */ 
    public static void main(String[] args) throws Exception { 

     URL url = new URL("http://www.xyz.com"); 

     HttpURLConnection conn = (HttpURLConnection) url.openConnection(); 
     conn.setConnectTimeout(5000); // 5 seconds 
     conn.setRequestMethod("GET"); 
     conn.connect(); 
     BufferedReader rd = new BufferedReader(new InputStreamReader(conn.getInputStream())); 

     String line; 
     StringBuffer bf = new StringBuffer(); 
     while ((line = rd.readLine()) != null) { 
      bf.append(line); 
     } 
     conn.disconnect(); 

    //... pass bf to an XML parser and do your processing... 
    } 
} 
0

これは、サーブレットではないように見えるべきかです。これは、コマンドラインで実行可能な単純なJavaプログラムです。

これはサーブレットですからコピーされ

package test; 

import java.io.*; 

import javax.servlet.http.*; 
import javax.servlet.*; 

public class HelloServlet extends HttpServlet { 
    public void doGet (HttpServletRequest req, 
        HttpServletResponse res) 
    throws ServletException, IOException 
    { 
    PrintWriter out = res.getWriter(); 

    out.println("Hello, world!"); 
    out.close(); 
    } 
} 

http://www.caucho.com/resin-3.0/servlet/tutorial/helloworld/index.xtp