How do I set pageEncoding for all my JSPs without touching each JSP file?
Asked Answered
R

4

8

I’m using Java 6, JBoss 7.1.3 and Spring 3.2.11.RELEASE. Despite the fact that we set this in our application context

<bean id="systemPrereqs"
    class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
    <property name="targetObject" value="#{@systemProperties}" />
    <property name="targetMethod" value="putAll" />
    <property name="arguments">
        <!-- The new Properties -->
        <util:properties>
            <prop key="org.apache.catalina.connector.URI_ENCODING">UTF-8</prop>
        </util:properties>
    </property>
</bean>

I notice on my JSPs, special characters aren’t rendered correctly unless we specify a

<%@ page contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

at the top of the JSP page. This is fine for one JSP, but is cumbersome to have to go through the entire application adding these directives. Is there a more global place I can specify this, like in a Spring context or somewhere, that will universally add the above directive to all our JSP pages?

Russon answered 30/12, 2016 at 20:56 Comment(0)
M
0

Put in your web.xml the following:

<jsp-config>
  <jsp-property-group>
     <url-pattern>*.jsp</url-pattern>
     <page-encoding>UTF-8</page-encoding>
     <default-content-type>text/html</default-content-type>
  </jsp-property-group>
</jsp-config>

More info: https://docs.oracle.com/cd/E17904_01/web.1111/e13712/web_xml.htm#WBAPP539

Middy answered 6/1, 2017 at 1:32 Comment(0)
P
0

Add the following System property also:

<property name="org.apache.catalina.connector.USE_BODY_ENCODING_FOR_QUERY_STRING" value="true"/>
Polydactyl answered 31/12, 2016 at 5:42 Comment(0)
M
0

Put in your web.xml the following:

<jsp-config>
  <jsp-property-group>
     <url-pattern>*.jsp</url-pattern>
     <page-encoding>UTF-8</page-encoding>
     <default-content-type>text/html</default-content-type>
  </jsp-property-group>
</jsp-config>

More info: https://docs.oracle.com/cd/E17904_01/web.1111/e13712/web_xml.htm#WBAPP539

Middy answered 6/1, 2017 at 1:32 Comment(0)
M
0

Put the following in your web.xml .

<jsp-config>
    <jsp-property-group>
        <url-pattern>*.jsp</url-pattern>
        <page-encoding>UTF-8</page-encoding>
    </jsp-property-group>
</jsp-config>
Motorcycle answered 9/1, 2017 at 14:12 Comment(3)
Hi, How is your answer different from code_angel's, entered on 1/6?Russon
I added an optional line default-content-type to make it different again ;)Middy
K, let me give it a go with that directive because the original answer as you ahd it didn't work for me. SPecifically cnotent stored in the db as 'and “Evidence” ' (notice the smart quotes, hope those cut and pasted over) was still being rendered as ' and ?Evidence? 'Russon
V
0

How about using filter?

<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
    <param-name>encoding</param-name>
    <param-value>UTF-8</param-value>
</init-param>
</filter>

<filter-mapping>
    <filter-name>characterEncodingFilter</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>
Vacuity answered 11/1, 2017 at 5:10 Comment(2)
I forgot to mention that this configuraiton is already present in my web.xml and yet the problem remains.Russon
@Russon try to use <system-properties> <property name="org.apache.catalina.connector.URI_ENCODING" value="UTF-8"/> </system-properties>Vacuity

© 2022 - 2024 — McMap. All rights reserved.