JSP: Can't figure out how to configure UTF-8 for GlassFish 3.1.2 response

I am using GlassFish 3.1.2 and see the following warning when I access my JSP pages:

PWC4011: Unable to set request character encoding to UTF-8 from context / myapp because request parameters have already been read or ServletRequest.getReader () has already been called

My JSP files start with:

<%@page pageEncoding="UTF-8"%>

      

My WEB-INF/glassfish-web.xml

file:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" "http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app>
    <parameter-encoding default-charset="UTF-8"/>

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

</glassfish-web-app>

      

My file WEB-INF/web.xml

:

   <?xml version="1.0" encoding="UTF-8"?>
   <web-app xmlns="http://java.sun.com/xml/ns/j2ee"
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
     version="2.5">

      

I believe the warning is triggered (as per here ) when the code setCharacterEncoding()

I always place before the first instance request.getAttribute()

in the JSP page is executed, like

request.setCharacterEncoding("UTF-8");
personName = request.getAttribute("personName");

      

but I don't know how to solve this.

My is web.xml

using Servlet 2.5, which I believe is JSP 2.1 (I know JSP 1.x does not support UTF-8, but I think JSP 2.1 supports UTF-8). I tried updating the file web.xml

to start with

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
    version="3.0">

      

but this only led to serious GlassFish errors starting with

[# | 2014-11-06T16: 11: 09.772-0800 | HEAVY | glassfish3.1.2 | javax.enterprise.system.tools.deployment.org.glassfish.deployment.common | _ThreadID = 1; _ThreadName = threaded 2; | DPL8015: Invalid deployment descriptors in the WEB-INF / web.xml deployment descriptor file in archive [myapp]. Line 25 Column 23 - cvc-complex-type.2.4.a: Invalid content was found starting at element 'display-name'. One of '{" http://java.sun.com/xml/ns/javaee ": servlet-class, " http://java.sun.com/xml/ns/javaee ": jsp-file, " http : //java.sun.com/xml/ns/javaee ": init-param," http://java.sun.com/xml/ns/javaee ": load-on-startup," http: // java .sun.com / xml / ns / javaee ":included, "http://java.sun.com/xml/ns/javaee ": async support," http://java.sun.com/xml/ns/javaee ": run-as," http: //java.sun .com / xml / ns / javaee ": security-role-ref," http://java.sun.com/xml/ns/javaee ": multipart-config} 'expected. | #]

Whenever I make changes, I have to restart the GlassFish server. I am assuming GlassFish detects the presence automatically glassfish-web.xml

, but let me know if I need to configure it somewhere for GlassFish in order to implement this file.

I followed all the advice I could find on the internet. Need help to figure this out. Any advice is greatly appreciated.

UPDATE 1

I notice that if I remove request.setCharacterEncoding("UTF-8");

from the JSP page so that the following

request.setCharacterEncoding("UTF-8");
personName = request.getAttribute("personName");

      

becomes simple

personName = request.getAttribute("personName");

      

that the warning goes away. Does this mean it request.setCharacterEncoding("UTF-8");

should only be present in pre-release Java servlets request.getParameter()

and NOT in JSP pages?

UPDATE 2

I can still see the encoding warning in the GlassFish server.log, although this time it doesn't come from my web app:

[# | 2014-11-10T10: 02: 57.234-0800 | WARNING | glassfish3.1.2 | org.apache.catalina.connector.Request | _ThreadID = 57; _ThreadName = Thread-2; | PWC4011: Unable to set the character encoding of the request to UTF-8 from the context because the request parameters have already been read or ServletRequest.getReader () has already been called | #]

Not sure where this came from or how to fix it, but I'll modify the JVM encoding of GlassFish by adding -Dfile.encoding=UTF8

this option to the Glassfish console Configurations>server-config>JVM Settings>JVM Options

and restart the server.

I also added the character set filter shown here: How do I get UTF-8 working in Java Webapps? ... Although this is for Tomcat, I hope it still works for GlassFish.

+3


source to share


1 answer


Solution: Create / edit glassfish-web.xml file (WEB-INF folder):



<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE glassfish-web-app PUBLIC "-//GlassFish.org//DTD GlassFish Application Server 3.1 Servlet 3.0//EN" 
"http://glassfish.org/dtds/glassfish-web-app_3_0-1.dtd">
<glassfish-web-app error-url="">
    <class-loader delegate="true"/>
    <!-- Change the default character encoding from ISO-8859-1 to UTF-8 -->
    <parameter-encoding default-charset="UTF-8"/>
    <jsp-config>
        <property name="keepgenerated" value="true">
            <description>Keep a copy of the generated servlet class' java code.</description>
        </property>
    </jsp-config>
</glassfish-web-app>

      

+1


source







All Articles