0

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;
}       
user593747
  • 1,393
  • 3
  • 30
  • 49
  • if you navigate to http://localhost:8000/TestServlet.class do you also get the error? Could you also paste your web.xml, as the servlet mapping might be wrong. And last, if you're not mapping the servlet, the feature to access a servlet by name was removed from the servlet specs a few years ago, as it was deemed unsecure. – Augusto Feb 12 '11 at 09:58

2 Answers2

2

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


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");
// ...
Community
  • 1
  • 1
BalusC
  • 992,635
  • 352
  • 3,478
  • 3,452
0

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.

AlexR
  • 109,181
  • 14
  • 116
  • 194