Compilation error in compiling this servlet code 
<pre><code>package hall;
import javax.servlet.*;
import javax.servlet.http.*;
import java.net.*;

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

  public static String headWithTitle(String title) {
    return(DOCTYPE + "\n" +
           "&lt;HTML&gt;\n" +
           "&lt;HEAD&gt;&lt;TITLE&gt;" + title + "&lt;/TITLE&gt;&lt;/HEAD&gt;\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...
}
</code></pre>

<p>I got compilation error while compiling this java servlet code . Where is my error in coding and should I include any other library ?</p>

<p>This is my error log </p>

<pre><code>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
</code></pre>
