?
Solved

Dynamically changing pageEncoding when JSP "loads"

Posted on 2005-04-05
15
Medium Priority
?
375 Views
Last Modified: 2012-05-05
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
Comment
Question by:varkon
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 7
  • 6
15 Comments
 
LVL 35

Expert Comment

by:TimYates
ID: 13710401
Does

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

work?
0
 

Author Comment

by:varkon
ID: 13714266
No, as with the <jsp:.... /> syntax in my question, this also gives "Unsupported encoding: <%=encoding%>". Again, it is not parsed at all.
0
 

Author Comment

by:varkon
ID: 13724930
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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 35

Expert Comment

by:TimYates
ID: 13724959
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
 

Author Comment

by:varkon
ID: 13772329
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
 

Author Comment

by:varkon
ID: 13831255
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13831565
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
 

Author Comment

by:varkon
ID: 13831946
Thanks for the info TimYates. I'll look into it and let you know.
0
 
LVL 35

Expert Comment

by:TimYates
ID: 13831965
http://tagunov.tripod.com/i18n/i18n.html

looks quite useful too :-)
0
 

Author Comment

by:varkon
ID: 13832787
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
 
LVL 35

Expert Comment

by:TimYates
ID: 13832828
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
 

Author Comment

by:varkon
ID: 14034142
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
 
LVL 35

Expert Comment

by:TimYates
ID: 14043316
Fair enough :-)

Sorry I couldn't help :-(

Maybe next time ;-)

Tim
0
 

Accepted Solution

by:
PashaMod earned 0 total points
ID: 14072706
Closed, 500 points refunded.
PashaMod
Community Support Moderator
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The Summer 2017 Scholarship Winners have been announced!
In today's business world, data is more important than ever for informing marketing campaigns. Accessing and using data, however, may not come naturally to some creative marketing professionals. Here are four tips for adapting to wield data for insi…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

764 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question