以下代码存在于
http://docs.oracle.com/javase/tutorial/networking/urls/readingWriting.html
哪个是oracle教程网站。
我的问题是理解 servlet。正如您在页面底部的文档中看到的那样:
如果您的 ReverseServlet 位于http://example.com/servlet/ReverseServlet,那么当您使用
http://example.com/servlet/ReverseServlet “逆转我”
为了运行这个例子,我用这个链接测试了我的程序
http://docs.oracle.com/javase/tutorial/networking/urls/examples/ReverseServlet.java “逆转我”
我得到了这个错误:
Exception in thread "main" java.io.IOException: Server returned HTTP response code: 403 for URL: http://docs.oracle.com/javase/tutorial/networking/urls/examples/ReverseServlet.java
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:1436)
at DemoURL.main(DemoURL.java:28)
是我的 ReverseServlet 所在的地方还是我完全错了。如果这不是正确的方法,我该如何运行这个程序来检查教程中的这个例子?
这是我更改类名的代码:
import java.io.*;
import java.net.*;
public class DemoURL {
public static void main(String[] args) throws Exception {
if (args.length != 2) {
System.err.println("Usage: java Reverse "
+ "http://<location of your servlet/script>"
+ " string_to_reverse");
System.exit(1);
}
String stringToReverse = URLEncoder.encode(args[1], "UTF-8");
URL url = new URL(args[0]);
URLConnection connection = url.openConnection();
connection.setDoOutput(true);
OutputStreamWriter out = new OutputStreamWriter(connection.getOutputStream());
out.write("string=" + stringToReverse);
out.close();
BufferedReader in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
String decodedString;
while ((decodedString = in.readLine()) != null) {
System.out.println(decodedString);
}
in.close();
}
}