Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to receive HTTP-Responses from Servlet

0 views
Skip to first unread message

nick

unread,
Oct 21, 2002, 7:40:40 AM10/21/02
to
I try to make a servlet, which responds with the sendError-Method of
the response, wether everything was ok or if there was an error:

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!

Sergio Juan

unread,
Oct 21, 2002, 7:58:18 AM10/21/02
to

"nick" <Nikola...@web.de> escribió en el mensaje
news:d9fb210e.02102...@posting.google.com...
It's correct. By setting the error code to 404, you are telling the client
that the data requested is not found, so it throws an error. And, as the
connection is set to read/write data and hence it can be abstracted as a
File, the java.io.FileNotFoundException makes sense. It just tells you that
the web page was not found, not that there was any problem with the
implementation. If 404 is a valid answer, just catch the Exception and treat
it.

> Thanks for your help!


0 new messages