编译此servlet代码时出现编译错误

时间:2010-12-08 06:29:27

标签: java

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代码时出现编译错误。我的编码错误在哪里?我应该包含任何其他库吗?

这是我的错误日志

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

3 个答案:

答案 0 :(得分:0)

尝试;

public static void main(String[] args) throws Exception之前删除“}”并将其放在文件的末尾。

答案 1 :(得分: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...
    }
}

答案 2 :(得分: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