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...
}
I got compilation error while compiling this java servlet code . Where is my error in coding and should I include any other library ?
This is my error log
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, inter开发者_如何转开发face, 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
Try;
Remove "}" before public static void main(String[] args) throws Exception
and put it end of the file.
a)main should be inside class
b)some imports were missing.
Here is how your code should look like
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...
}
}
This is not Servlet. It is a simple Java Program, executable by commandline.
This is a 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();
}
}
Copied from: http://www.caucho.com/resin-3.0/servlet/tutorial/helloworld/index.xtp
精彩评论