Code in the Servlet
...
// HTTP-Error-Code 200
response.sendError(response.SC_OK);
...
// HTTP-Error-Code 404
response.sendError(response.SC_NOT_FOUND);
...
This Servlet is called by an application, which sends an xml-file per
post-Method to this servlet:
public static void main(String[] args) {
try {
String location = "http://localhost:8080/server";
// Connection to the servlet
URL testServlet = new URL( location );
URLConnection servletConnection = testServlet.openConnection();
servletConnection.setDoInput(true);
servletConnection.setDoOutput(true);
//make request
BufferedWriter out = new BufferedWriter(new
OutputStreamWriter(servletConnection.getOutputStream()));
out.write("<?xml version=\"1.0\"?>");
out.write("<Request>This is my sample request</Request>");
out.flush();
out.close();
//Ausgabe auslesen
BufferedReader in = new BufferedReader(new
InputStreamReader(servletConnection.getInputStream()));
String inputLine;
while ((inputLine = in.readLine()) != null){
System.out.println(inputLine);
}
in.close();
...
}
catch (MalformedURLException mae) {
System.out.println("MalformedURLException ::
APPLET_AppletToServlet ");
}
catch (java.io.FileNotFoundException ex) {
System.out.println(">>>>>>>>> TestApp: FileNotFoundException!");
ex.printStackTrace();
}
catch (IOException ex) {
System.out.println(">>>>>>>>> TestApp: IOException!");
ex.printStackTrace();
}
catch (Exception ex) {
System.out.println(">>>>>>>>> TestApp: Exception!");
ex.printStackTrace();
}
If the response of the servlet is SC_OK (HTTP-Error 200) then
everything is ok, but if the response is SC_NOT_FOUND (HTTP-Error 404)
then I get the following error:
java.io.FileNotFoundException: http://localhost:8080/server
at sun.net.www.protocol.http.HttpURLConnection.getInputStream(HttpURLConnection.java:574)
at sun.net.www.protocol.http.HttpURLConnection.getHeaderField(HttpURLConnection.java:817)
at java.net.URLConnection.getContentType(URLConnection.java:377)
at testdateiuebertragung.TestApp.main(TestApp.java:36)
>>>>>>>>> TestApp: FileNotFoundException!
Is that ok? If the errors are thrown as exceptions, which error throws
which exception?
Thanks for your help!
> Thanks for your help!