gretty 0 Junior Poster

Hello

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;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.