wood burning stoves 2.0*
The moose likes Web Component Certification (SCWCD/OCPJWCD) and the fly likes setting characters encoding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Certification » Web Component Certification (SCWCD/OCPJWCD)
Bookmark "setting characters encoding" Watch "setting characters encoding" New topic
Author

setting characters encoding

James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

Are the following used for different purpose? in terms of setting the response type.

1. response.setCharacterEncoding();
2.<%@ page session="true" contentType="text/html; charset=UTF-8"%>
3.<%@ page session="true" contentType="text/html; charset=UTF-8" pageEncoding=""UTF-8%>

If they are different .could someone explain?


SCJP 6
Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Rajeev Rnair
Ranch Hand

Joined: Mar 22, 2010
Posts: 308

response.setContentType("text/html; charset=UTF-8"); and
<%@ page contentType="text/html; charset=UTF-8"%> are same.

Character encoding can be subset of Content type. For example:
response.setCharacterEncoding("UTF-8"); and
<%@ page pageEncoding="UTF-8"%> are same.

If you use contentType, you may not need to use characterEncoding! Please check the ServletResponse API
setCharacterEncoding

public void setCharacterEncoding(java.lang.String charset)
Sets the character encoding (MIME charset) of the response being sent to the client, for example, to UTF-8. If the character encoding has already been set by setContentType(java.lang.String) or setLocale(java.util.Locale), this method overrides it. 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.
This method can be called repeatedly to change the character encoding. This method has no effect if it is called after getWriter has been called or after the response has been committed.

Containers must communicate the character encoding used for the servlet response's writer to the client if the protocol provides a way for doing so. In the case of HTTP, the character encoding is communicated as part of the Content-Type header for text media types. Note that the character encoding cannot be communicated via HTTP headers if the servlet does not specify a content type; however, it is still used to encode text written via the servlet response's writer.

Parameters:
charset - a String specifying only the character set defined by IANA Character Sets (http://www.iana.org/assignments/character-sets)


SCJP6, SCWCD5, OCP-JBCD5, OCE-JWSD6 OCE-JPAD6 , OCM-JEA5 1,OCM-JEA5 2,3 - Brainbench certifications: J2EE, Java2, Java2-NonGUI, JSP, SQL2000 Admin, SQL2000 Programming , Brainbench certified Java Programmer, Computer Programmer, Web Developer, Database Administrator
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: setting characters encoding