在Applet中接收输入流
我正在尝试使用 Java 小程序和Servlet 和 Servlet 之间的通信一个小程序。当我从 URLConnection 对象检索输入流时,我遇到 HTTP 501 错误。
我知道 501 错误意味着我正在尝试执行未配置连接的操作(在我的例子中) GET 操作),但我认为下面的代码确实接受输入,因为我说 conn.setDoInput( true ); 。
有人可以就为什么我无法获取输入流和输入流向我提供任何建议吗?如何修复它?
错误:
在 messageServlet() 中:失败:java.io.IOException:服务器返回 HTTP 响应代码:501 对于 URL:http://localhost:8000/TestServlet.class
我的代码如下(我已经注释了抛出异常的位置),也如果你想知道 http://localhost:8000 是我用 python 脚本设置的 moch 服务器这是有效的,因为连接 &获取输出流不会引发异常。
//this code occurs in my class TextApplet::messageServlet()
//I am attempting to send a string to my servlet class TestServlet,
//then read a string that the servlet sends back
try
{
URL servletUrl = new URL( "http://localhost:8000/TestServlet.class" );
URLConnection conn = servletUrl.openConnection();
conn.setDoInput( true );
conn.setDoOutput( true );
conn.setUseCaches( false );
conn.setDefaultUseCaches (false);
conn.setRequestProperty ("Content-Type", "application/octet-stream");
OutputStream out = conn.getOutputStream();
out.write( message.getBytes() );
out.flush();
out.close();
// receive result from servlet
InputStream in = conn.getInputStream(); // Exception Occurs HERE
BufferedReader reader = new BufferedReader(new InputStreamReader( in ));
String result = reader.readLine();
in.close();
return result;
}
catch ( IOException e )
{
System.out.println( "In messageServlet(): " + e );
msgBox.setText( msgBox.getText() + "\nIn messageServlet(): Failure: " + e );
}
catch ( Exception e )
{
System.out.println( "In messageServlet(): " + e );
msgBox.setText( msgBox.getText() + "\nIn messageServlet(): Failure: " + e );
}
return null;
}
I am experimenting with Java applets & communicating between a Servlet & an applet. I am experiencing a HTTP 501 error when I go to retrieve the input stream from my URLConnection object.
I know that the 501 error means that I am attempting to perform an action that the connection was not configured for (in my case the GET operation) but I thought my code below DOES accept input because I say conn.setDoInput( true ); .
Can someone provide me with any advice on why I cant obtain an input stream & how to fix it?
Error:
In messageServlet(): Failure: java.io.IOException: Server returned HTTP response code: 501 for URL: http: //localhost:8000/TestServlet.class
My code is below (I have commented where the exception is thrown), also if you are wondering the http: //localhost:8000 is a moch server I set up with a python script & that works because connecting & obtaining the output stream doesn't throw an exception.
//this code occurs in my class TextApplet::messageServlet()
//I am attempting to send a string to my servlet class TestServlet,
//then read a string that the servlet sends back
try
{
URL servletUrl = new URL( "http://localhost:8000/TestServlet.class" );
URLConnection conn = servletUrl.openConnection();
conn.setDoInput( true );
conn.setDoOutput( true );
conn.setUseCaches( false );
conn.setDefaultUseCaches (false);
conn.setRequestProperty ("Content-Type", "application/octet-stream");
OutputStream out = conn.getOutputStream();
out.write( message.getBytes() );
out.flush();
out.close();
// receive result from servlet
InputStream in = conn.getInputStream(); // Exception Occurs HERE
BufferedReader reader = new BufferedReader(new InputStreamReader( in ));
String result = reader.readLine();
in.close();
return result;
}
catch ( IOException e )
{
System.out.println( "In messageServlet(): " + e );
msgBox.setText( msgBox.getText() + "\nIn messageServlet(): Failure: " + e );
}
catch ( Exception e )
{
System.out.println( "In messageServlet(): " + e );
msgBox.setText( msgBox.getText() + "\nIn messageServlet(): Failure: " + e );
}
return null;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
HTTP 501 错误意味着服务器(至少是 servletcontainer 内置默认 servlet)无法理解您创建的 HTTP 请求。它可能是由不正确的标头和/或正文引起的。
我不确定发生了什么,但仅 URL
http://localhost:8000/TestServlet.class
到目前为止看起来并不正确。这看起来很像您将原始.class
文件放入公共 webcontent 文件夹中并尝试访问它。这毫无意义。 Servlet 类应放置在/WEB-INF/classes
文件夹中(在包内!),并且调用 servlet 的 URL 应在
中定义/WEB-INF/web.xml
文件的。另请参阅
URLConnection
触发和处理 HTTP 请求?与具体问题,我宁愿使用
Applet#getCodeBase()
获取代码库的 URL(下载小程序的位置),而不是将其硬编码在 URL 中。假设 servlet 映射到web.xml
文件中的/myservlet
URL 模式,那么您需要在 applet 中创建 URL,如下所示:A HTTP 501 error means that the server (at least, the servletcontainer builtin default servlet) doesn't understand the HTTP request as you've created. It can be caused by incorrect headers and/or body.
I'm not sure what's going on, but the URL alone
http://localhost:8000/TestServlet.class
does by far not look right. This look much like that you've dropped the raw.class
file in the public webcontent folder and are attempting to access it. This makes no sense. Servlet classes should be placed in/WEB-INF/classes
folder (inside a package!) and the URL to invoke the servlet should be definined in<url-pattern>
of the/WEB-INF/web.xml
file.See also
URLConnection
?Unrelated to the concrete problem, I'd rather use
Applet#getCodeBase()
to obtain the URL of the code base (there where the applet is been downloaded from) instead of hardcoding it in URLs. Assuming that the servlet is mapped on an URL pattern of/myservlet
inweb.xml
file, then you need to create the URL in applet as follows:如果您收到 5XX 错误,则原因在服务器端。检查您的应用程序的日志。服务器 - 您将看到 servlet 抛出异常,这将解释问题所在。
If you get 5XX error the reason is on server side. Check logs of your app. server - you will see exception thrown from your servlet that will explain where the problem is.