"sendError" method as compared to "setStatus"; weired results; Help !

I am using weblogic server 8.1 sp5.

I have a servlet that generates an error code and returns a "message" as a text.

The problem is that iam not getting the "message" returned on my http protocol layer.

BUT when i use "setStatus" method instead of "sendError" method, everything works fine. setStatus is deprecated and we are told not to use it.

NOTE: When i run the servlet with sendError method, it works fine on the browser BUT it is not returned in my http (at the protocol layer).

Can somebody shed some light on the difference of this behavior?

Attached is the code.

import java.io.*;

import javax.servlet.*;

import javax.servlet.http.*;

publicclass MyWebServletextends HttpServlet{

protectedvoid processRequest(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException{

response.setContentType("text/html;charset=UTF-8");

PrintWriter out = response.getWriter();

out.println("This is MyWebServlet");

String message="Testing custom message of a 592 Error"

response.sendError(592,message);

out.close();

}

protectedvoid doGet(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException{

processRequest(request, response);

}

protectedvoid doPost(HttpServletRequest request, HttpServletResponse response)

throws ServletException, IOException{

processRequest(request, response);

}

}

[2330 byte] By [kevinlnga] at [2007-11-27 8:11:13]
# 1
What do you mean by http (at the protocol layer) ?PS.
puckstopper31a at 2007-7-12 19:55:07 > top of Java-index,Java Essentials,Java Programming...
# 2

i mean at the URL level.

We ran some sniffer N/W traces on the difference between the error code of 500 and 592(as on the servlet).

We see difference like ....the sniffer traces are little out of order but if you read carefully...you may see the diff.

I get "Unknown" for 592 error code whileas i get the whole string message with 500 error code.

Output with 592 error code:

0000 48 54 54 50 2f 31 2e 31 20 35 39 32 20 55 6e 6bHTTP/1.1 592 Unk

0010 6e 6f 77 6e 0d 0a 43 6f 6e 6e 65 63 74 69 6f 6enown..Connection

0020 3a 20 63 6c 6f 73 65 0d 0a 44 61 74 65 3a 20 57: close..Date: W

0030 65 64 2c 20 31 33 20 4a 75 6e 20 32 30 30 37 20ed, 13 Jun 2007

0040 31 33 3a 31 32 3a 30 34 20 47 4d 54 0d 0a 43 6f13:12:04 GMT..Co

0050 6e 74 65 6e 74 2d 4c 65 6e 67 74 68 3a 20 32 34ntent-Length: 24

0060 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65 3a 20..Content-Type:

0070 74 65 78 74 2f 68 74 6d 6c 0d 0a 58 2d 50 6f 77text/html..X-Pow

0080 65 72 65 64 2d 42 79 3a 20 53 65 72 76 6c 65 74ered-By: Servlet

0090 2f 32 2e 34 20 4a 53 50 2f 32 2e 30 0d 0a 0d 0a/2.4 JSP/2.0....

Output with 500 error code:

0000 48 54 54 50 2f 31 2e 31 20 35 30 30 20 54 65 73HTTP/1.1 500 Tes

0010 74 69 6e 67 20 63 75 73 74 6f 6d 20 6d 65 73 73ting custom mess

0020 61 67 65 20 6f 66 20 61 20 35 30 30 20 49 6e 74age of a 500 Int

0030 65 72 6e 61 6c 20 53 65 72 76 65 72 20 45 72 72ernal Server Err

0040 6f 72 0d 0a 43 6f 6e 74 65 6e 74 2d 54 79 70 65or..Content-Type

0050 3a 20 74 65 78 74 2f 68 74 6d 6c 3b 63 68 61 72: text/html;char

0060 73 65 74 3d 55 54 46 2d 38 0d 0a 43 6f 6e 74 65set=UTF-8..Conte

0070 6e 74 2d 4c 61 6e 67 75 61 67 65 3a 20 65 6e 2dnt-Language: en-

0080 55 53 0d 0a 43 6f 6e 74 65 6e 74 2d 4c 65 6e 67US..Content-Leng

0090 74 68 3a 20 31 31 33 39 0d 0a 44 61 74 65 3a 20th: 1139..Date:

00a0 57 65 64 2c 20 31 33 20 4a 75 6e 20 32 30 30 37Wed, 13 Jun 2007

00b0 20 31 33 3a 32 34 3a 34 33 20 47 4d 54 0d 0a 5313:24:43 GMT..S

00c0 65 72 76 65 72 3a 20 41 70 61 63 68 65 2d 43 6ferver: Apache-Co

00d0 79 6f 74 65 2f 31 2e 31 0d 0a 43 6f 6e 6e 65 63yote/1.1..Connec

00e0 74 69 6f 6e 3a 20 63 6c 6f 73 65 0d 0a 0d 0ation: close....

null

kevinlnga at 2007-7-12 19:55:07 > top of Java-index,Java Essentials,Java Programming...
# 3

Have you specified a custom error page? If so it will be used in preference to the message you pass in to the sendError method.

look in your web.xml for an "error-page" element that is bound to your error code (or to a exceptions)

It might also be that weblogic does not handle "extension" error codes acording to spec (this really would not surprise me)

matfud

matfuda at 2007-7-12 19:55:07 > top of Java-index,Java Essentials,Java Programming...
# 4

I have not specified any "error-page" element.

Actually, we want the "message" to be returned so that one of our other "monitoring" utility can parse through it and do some stuff with it.

Let me ask this:

1) If i specify "error-page" element in my web.xml, would that NOT send me the message in my URL and instead just show me that error page?

2) If weblogic can not handle "extension" error codes acording to spec, then why the same weblogic server works fine with 592 and setStatus method? Iam confused.

Thanks for your directions and help though.

kevinlnga at 2007-7-12 19:55:07 > top of Java-index,Java Essentials,Java Programming...
# 5

If an error page is set (in your web.xml and possibly at the app server level ( don't know enough about weblogic to know if it has this option)) then any message you pass to sendError will be discarded and the error page will be sent for that error code. I am not sure what happens if the error page specified does not exist (you may just get an empty body).

As for specification complience, I have had problems with websphere, weblogic and tomcat not complying with the spec (actually the spec is ambiguous in many places which mostly accounts for the difference).

have you tried using sendError with a 500 code rather then 592?

matfuda at 2007-7-12 19:55:07 > top of Java-index,Java Essentials,Java Programming...
# 6

I added following line to my web.xml without the index.html page and i get 503 service unavaible.

<servlet>

<servlet-name>MyWebServlet</servlet-name>

<servlet-class>MyWebServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>MyWebServlet</servlet-name>

<url-pattern>/MyWebServlet</url-pattern>

</servlet-mapping>

<error-page>

<error-code>592</error-code>

<location>/index.html</location>

</error-page>

Yes. 500 works fine with weblogic and with correct message string returned.

I am still confused why setStatus method works with weblogic and with 592 error code ...can you imagine a possible answer to this?

kevinlnga at 2007-7-12 19:55:07 > top of Java-index,Java Essentials,Java Programming...
# 7

setStatus just sets the status number in the response this has been around for a long time. Weblogic will probably not change its implementation as many apps may still depend on the way it works (esp. since its been deprecated for the last 3 versions of the servlet API). It is likely that sendError has an entirely different implementation.

matfud

matfuda at 2007-7-12 19:55:07 > top of Java-index,Java Essentials,Java Programming...
# 8
hmm ...so what should i do ? Open a BEA case and let then handle it?
kevinlnga at 2007-7-12 19:55:07 > top of Java-index,Java Essentials,Java Programming...
# 9

hey kevin,

from your post it seems that you want to send HTTP protocol errors and if thats true then as per my knowledge sendError() should be the first method you have to call, i mean to say is that you can not send HTTP protocol errors if you already have sent some content.

in that case remove any out.println() from your servlet and test it again.

this is just a suggestion... hope it will help ya..

regards

i_virus

i_virusa at 2007-7-12 19:55:07 > top of Java-index,Java Essentials,Java Programming...
# 10

> hey kevin,

>

> from your post it seems that you want to send HTTP

> protocol errors and if thats true then as per my

> knowledge sendError() should be the first method you

> have to call, i mean to say is that you can not send

> HTTP protocol errors if you already have sent some

> content.

>

> in that case remove any out.println() from your

> servlet and test it again.

>

> this is just a suggestion... hope it will help ya..

>

> regards

> i_virus

Good point.

If you write anything to the output stream you cannot be assured that you can send an error (or set headers) unless you call isCommitted(). However I don't think this is the case as the status code is arriving correctly. The OP is just missing the body content.

matfud

matfuda at 2007-7-12 19:55:07 > top of Java-index,Java Essentials,Java Programming...
# 11

hey mat,

but i don't think so, because if that is the reason then if a servlet misses <body> tag then nothing should be shown by the browser which is not what happens actually.

i think you cannot set any header content using the output stream of the response and everything outputted to the output stream of the response goes by default as body.

regards

i_virus

i_virusa at 2007-7-12 19:55:07 > top of Java-index,Java Essentials,Java Programming...
# 12
a response that is committed has already sent the status code and headers.if this was the problem then I think the OP would not get the correct status (which he does)matfud
matfuda at 2007-7-12 19:55:07 > top of Java-index,Java Essentials,Java Programming...