tags:

views:

307

answers:

2

Hi All,

In my java class I am trying to do something like

HttpServletResponse.setCharacterEncoding("UTF-8");

but the code fails to compile with the message:

Depend attribute is not supported by modern compiler.

If I remove this line, code compiles without any issues.

Can anyone shed any light on this?

Thanks

+1  A: 

HttpServletResponse.setCharacterEncoding() is not a static method. You need to call it on an instance. Something like: (assuming your instance is called resp)

resp.setCharacterEncoding("UTF-8");

Alternatively, you could set character encoding in the Content-type header like this:

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

EDIT: Ok, by your comments, I see you are already doing this. I took the wording of the question literally. Anyway, the problem is most likely that you are using javac with dependency tracking turned on. It's a flag you can pass on the command line (or an attribute to the <javac> ant task). Turn off dependency tracking. It's not supported by Sun's compiler.

Asaph
Why it doesn't say the usual thing: *"non static attribute may not be called from non static context"* ( or something like that ) It it for the compiler version?
OscarRyz
The OP must be compiling with dependency tracking turned on. It's a flag you can pass on the command line (or in the javac ant task). It's not supported by Sun's compiler.
Asaph
I am actually using an instance of HttpServletResponse. I just typed in HttpServletResponse.setCharact.... to make the class/object name absolutely create but it ended it up creating more confusion. So to be clear.. I am using something like resp.setCharacterEncoding(...);where resp is an instance of HttpServletResponse
makhan10
@makhan10: Ok, I updated my answer in response to your comment.
Asaph
The normal approach sets the encoding of the response writer. Your "alternative" approach doesn't do that. It only sets the encoding which the client is supposed to use. This is a major difference.
BalusC
@BalusC: That's not exactly true. From the docs for HttpServletResponse.setContentType(): "The response's character encoding is only set from the given content type if this method is called before getWriter is called." -- http://java.sun.com/products/servlet/2.5/docs/servlet-2%5F5-mr2/javax/servlet/ServletResponse.html#setContentType(java.lang.String)
Asaph
Interesting, thanks for the pointer.
BalusC
@BalusC: Also, from the docs for setCharacterEncoding(): " has no effect if it is called after getWriter has been called". And "Calling setContentType(java.lang.String) with the String of text/html and calling this method with the String of UTF-8 is equivalent with calling setContentType with the String of text/html; charset=UTF-8." So I think the normal and alternative method are truly interchangeable.
Asaph
+1  A: 

Did you try to remove the -depend option when invoking javac (which is not supported by modern compiler :).

Pascal Thivent
not really - I will do that now.
makhan10