Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 378
  • Last Modified:

Dynamically changing pageEncoding when JSP "loads"

Hi,

I've been trying to create a page that is changing its encoding (as per the jsp page attribute) depending on the browser being shown.

Right now I'm doing something like this:

<%@ page language="java" %>
<%
if(request.getHeader("user-agent").equals("some browser")){ //Adding some string manipulation here to find the browser name....
         %><jsp:directive.page pageEncoding="ISO-8859-7" /><%
}
else{
       %><jsp:directive.page pageEncoding="ISO-8859-1" /><%
}
%>

However, jsp complains that it can only have one "pageEncoding" attribute.

Then, I tried:

<%@ page language="java" %>
<%
String encoding = "ISO-8859-1";
if(request.getHeader("user-agent").equals("some browser")){ //Adding some string manipulation here to find the browser name....
       encoding = "ISO-8859-7";
}
%>
<jsp:directive.page pageEncoding="<%=encoding%>" />

In this case, I get an exception that  <%=encoding%> is not a valid pageEncoding value. The parameter is not parsed at all...

Is there any other way for me to change the pageEncoding value of a JSP from within that JSP when loaded? Answers with examples will be greatly appreciated.
0
varkon
Asked:
varkon
  • 7
  • 6
1 Solution
 
TimYatesCommented:
Does

<%@ page pageEncoding="<%=encoding%>" %>

work?
0
 
varkonAuthor Commented:
No, as with the <jsp:.... /> syntax in my question, this also gives "Unsupported encoding: <%=encoding%>". Again, it is not parsed at all.
0
 
varkonAuthor Commented:
Since I see there is not much interest in this, I'll increase the points by 50. I'll need a tested and working solution, though.
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
TimYatesCommented:
instead of that, can't you do:

<%
    String encoding = "ISO-8859-1";
    if( request.getHeader("user-agent").equals("some browser") )  //Adding some string manipulation here to find the browser name....
    {    
       encoding = "ISO-8859-7";
    }
    response.setContentType("text/html; charset=" + encoding );
%>

?

And just as a question, is UTF-8 not good enough for both encodings?
0
 
varkonAuthor Commented:
TimYates unfortunately, no. I have to display the pages to unicode-unaware browsers. Furthermore, I've tried what you proposed, but still I do not get the expected results. I think it has something to do wth the amount of data send to the browser and/or the time they are sent...
0
 
varkonAuthor Commented:
Ok, now lets complicate things a bit more...
I've tried using the following code:

String encoding = "ISO-8859-1";
if( request.getHeader("user-agent").equals("some browser") ) {    
       encoding = "ISO-8859-7";
}
<jsp:directive.page contentType="<%=encoding%>" />

This seems to work on some browsers but not on others. For example, firefox will load and render the page normally, but IE will not. Instead it will attempt to download the page, as if not understanding that it is a html file. Same behaviour on a Lynx browser. if I use the bit of code  TimYates suggested, I CAN render the page in IE, but any greek characters will be converted to "?".

Since I'me increasing the difficulty of this question, I'll also increase its point value.
0
 
TimYatesCommented:
With Servlets v 2.2, you can apprently do:

response.setLocale(new Locale("ja", "JP"));

http://www.di.unipi.it/~ghelli/bdl/A97329_03/web.902/a95882/jspnls.htm
0
 
varkonAuthor Commented:
Thanks for the info TimYates. I'll look into it and let you know.
0
 
TimYatesCommented:
http://tagunov.tripod.com/i18n/i18n.html

looks quite useful too :-)
0
 
varkonAuthor Commented:
I seem to be doing everything as mentioned in the URIs you gave me TimYates, but no progress....

I do not leave blanks between script tags, I checked the generated jsp so that no data is sent to the browser before setting the content, but still!

I do not know anymore if this is a problem with my code, or if it is an AS-specific issue. I'm using JBoss 4.0.1sp1 for the record.

Right now I'm writting redirection pages so that I first check the browser type, and then redirect to a page with the appropriate encoding... I'd appreciate to know if something comes up that might help me avoid the solution I'm currently working on...
0
 
TimYatesCommented:
I've bookmarked this question, so I'll be sure to post anything I find on my travels around the web :-/

Sorry I couldn't help any further...
0
 
varkonAuthor Commented:
Since the solution that I'll use is the one I suggested in my previous post, I'll ask this question to be closed. Thanks for everything.
0
 
TimYatesCommented:
Fair enough :-)

Sorry I couldn't help :-(

Maybe next time ;-)

Tim
0
 
PashaModCommented:
Closed, 500 points refunded.
PashaMod
Community Support Moderator
0

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now