Hi all !
I need to know how to dynamically either set or change the character set of my JSP
page.
In my site the user can choose the language he wants to see his pages in. He has the
choice between english, french and japanese.
The character set of the first 2 is ISO-8859-1. The character set of the japanese one
is SHIFT_JIS.
If I declare the following at the top of my page:
<%@ page import="i18nLocale" buffer="none" autoFlush="true" contentType="text/html;
charset=SHIFT_JIS"%>, for example for japanese, it works perfectly, the japanese
characters are returned just fine(btw, the literals all come from a ressourcebundle
class, one per language).
My problem is that I can't seem to be able to set the content type dynamically.
First, I tried to put it in a variable and insert the variable in the declaration,
like, for example:
<%@ page import="i18nLocale" buffer="none" autoFlush="true" contentType="text/html;
charset=<%=myNewCharSet%>"%>
but it didn't work... the JSP page was blowing up at compile time.
Then, I tried to use setContentType as follows:
<%String locale = request.getParameter("locale");
String charsetType;
if (!locale.equals("fr")) {
charsetType = "SHIFT_JIS";
}
else {
charsetType = "ISO-8859-1";
}
response.setContentType("text/html; charset=SHIFT_JIS");%>
<%@ page import="i18nLocale" buffer="none" autoFlush="true"%>
It still didn't work because when the JSP page is compiled, as there is no content
type set up in the <%@...%> directive, it defaults to ISO-8859-1.
And, although I don't write anything out, when the response.setContentType("text/html;
charset=SHIFT_JIS") executes, it apparently doesn't reset the character set.
Thirdly, I tried to mess with the page context, the JSP writer and the http session
which are all initialized thanks/because of the <%@...%> directive. I tried to
override them right after my response.setContentType("text/html; charset=SHIFT_JIS").
But still it didn't work, the character set was still ISO-8859-1.
Fourth,
I wrote 3 JSP pages that I called TEST_FR.JSP, TEST_EN.JSP and TEST_JP.JSP. I inserted
the <%@...%> directive in all of them with the character set hard-coded. I then used a
<jsp:include ... /> to add my file. Well, besides the fact that it has to compile
every time it is invoked, besides the fact that I would end up with 1 file for each
language I want the web site to handle, it actually didn;t work.
I finally went to the SUN website to see if they had suggestions on how to dynamically
set up the character set but I didn't find anything.
So, does somebody have an idea or a suggestion on how I could achieve that?
Thank you for any help you can provide,
Pascal BLANC
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets