File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSP and the fly likes JSP Page Encoding Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "JSP Page Encoding" Watch "JSP Page Encoding" New topic

JSP Page Encoding

Mohammed Yousuff
Ranch Hand

Joined: Oct 17, 2007
Posts: 198
Yesterday I had a interesting problem, I want to send page encoding as a dynamic attribute to the JSP pages. Currently all jsp’s we are using “UTF-8” encoding as hard coded string, but we want this to be dynamic and it should come from Server (or anywhere from java class ).

<%@ page language="java" import="java.text.*" pageEncoding="UTF-8"%>

In above code snippets I was trying to inject the pageEncoding value as dynamic value (by using the evil Scriptlets) as below.

<%@ page language="java" import="java.text.*" pageEncoding="<%=pageEncodingString%>"%>

But Eclipse was shouting that it was not valid pageEncoding value.Then I realized that in @page attribute you can’t give a Scriptlets value… I am not sure is there any other way we can achieve this ? ….

I came to another way by searching google (as usual) , that nothing but adding a jsp-property-group attribute in section (in web.xml file).

<jsp-config> <jsp-property-group> <url-pattern>*.jsp</url-pattern> <page-encoding>UTF-8</page-encoding> </jsp-property-group> </jsp-config>

You know how above code snippets work? It’s quite simple . You don’t need to add the pageEncoding="UTF-8" attributes in @page directive for all your Jsp files. If you make changes here it will affect all you jsp’s (based on your url-pattern).
Class : org.apache.jasper.compiler.JspConfig

But this also doen’t serve my purpose, the reason I have to change the encoding value which coming the Backend layer. I gone little bit further and found that there is some apache API to get the values of jsp-config in java class. When I saw that API I can find only the get methods and not the set methods. So again that doesn’t serve my purpose.
I still feel there should be some way to achieve this. If you guys have any thoughts please let me know thank you

My Thoughts :
Try not to become a man of success but rather to become a man of value.
Sebastian Janisch
Ranch Hand

Joined: Feb 23, 2009
Posts: 1183
Why don't you set the page encoding in the forwarding Servlet. You have the response object.

JDBCSupport - An easy to use, light-weight JDBC framework -
Mohammed Yousuff
Ranch Hand

Joined: Oct 17, 2007
Posts: 198
if i set like that don't i need not to set pageEncoding="UTF-8" in JSP???
I agree. Here's the link:
subject: JSP Page Encoding
It's not a secret anymore!