JSP: can't figure out how to set UTF
I'm using GlassFish 3.1.2 and seeing the following warning whenever 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 is:
<?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 WEB-INF/web.xml
file is:
<?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 executing the setCharacterEncoding()
code that I always place before the first instance of request.getAttribute()
in a JSP page, such as
request.setCharacterEncoding("UTF-8");
personName = request.getAttribute("personName");
but I've no idea how to resolve this.
My web.xml
uses 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 does support UTF-8). I tried upgrading the web.xml
file 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 just led to GlassFish severe errors starting with
[#|2014-11-06T16:11:09.772-0800|SEVERE|glassfish3.1.2|javax.enterprise.system.tools.deployment.org.glassfish.deployment.common|_ThreadID=1;_ThreadName=Thread-2;|DPL8015: Invalid Deployment Descriptors in Deployment descriptor file WEB-INF/web.xml in archive [myapp]. Line 25 Column 23 -- cvc-complex-type.2.4.a: Invalid content was found starting with 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":enabled, "http://java.sun.com/xml/ns/javaee":async-supported, "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}' is expected.|#]
Whenever I make changes I make sure I restart GlassFish server. I'm assuming GlassFish auto-detects the presence of glassfish-web.xml
, but let me know if I need to configure it somewhere for GlassFish to realize this file exists.
I've followed all the advice I could find online. Need some help to figure this out. Any advice much 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 just
personName = request.getAttribute("personName");
that the warning goes away. Does this mean that request.setCharacterEncoding("UTF-8");
should only be present in the Java servlets before issuing a request.getParameter()
, and NOT in the JSP pages at all?
UPDATE 2
I'm still seeing an encoding warning in GlassFish server.log file, although this time it is not coming 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 request character encoding to UTF-8 from context , because request parameters have already been read, or ServletRequest.getReader() has already been called|#]
Not sure where this is coming from, or how to fix, but I'll modify GlassFish JVM encoding by adding in the option from the GlassFish admin console -Dfile.encoding=UTF8
here: Configurations>server-config>JVM Settings>JVM Options
, and restart server.
I also added the character set filter shown here: How to get UTF-8 working in Java webapps? . Although, it's made for Tomcat, I hope it still works for GlassFish.
解决方案:创建/编辑文件glassfish-web.xml(文件夹WEB-INF):
<?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>
链接地址: http://www.djcxy.com/p/68248.html
上一篇: 任何人都会遇到PCI合规性扫描在应用程序服务器上创建错
下一篇: JSP:无法弄清楚如何设置UTF