Receive input stream in Applet
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;
}
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
- Our Servlets wiki page - contains Hello World example
- How to fire and handle HTTP requests with
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
in web.xml
file, then you need to create the URL in applet as follows:
URL url = new URL(getCodeBase(), "myservlet");
// ...
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.
精彩评论