2010-12-08 189 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 servlet代碼時出現編譯錯誤。我的編碼錯誤在哪裏,我應該包含任何其他庫嗎?編譯此servlet代碼時出現編譯錯誤

這是我的錯誤日誌

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

發佈一些錯誤日誌,它很難猜測錯誤。 – 2010-12-08 06:35:41

回答

0

嘗試;

public static void main(String[] args) throws Exception之前刪除「}」並將其放到文件結尾。

0

a)主要應該在類內
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

這不是Servlet的。它是一個簡單的Java程序,可以由命令行執行。

這是一個Servlet:從複製

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