JSP:无法弄清楚如何为GlassFish 3.1.2响应设置UTF-8 [英] JSP: can't figure out how to set UTF-8 for GlassFish 3.1.2 response

查看:154
本文介绍了JSP:无法弄清楚如何为GlassFish 3.1.2响应设置UTF-8的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用GlassFish 3.1.2,并在访问我的JSP页面时看到以下警告:

I'm using GlassFish 3.1.2 and seeing the following warning whenever I access my JSP pages:


PWC4011:无法设置从
context / myapp请求字符编码为UTF-8,因为请求参数已被读取,
或ServletRequest.getReader()已被调用

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

我的JSP文件以:

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

我的 WEB-INF / glassfish-web.xml 文件是:

<?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>

我的 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">

我相信警告被触发(根据 setCharacterEncoding()代码时,.com / display / GlassFish / FaqWebAppUnableToSetRequestCharEncodingrel =nofollow noreferrer>这里)一个JSP页面中的 request.getAttribute(),如

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.

我的 web.xml 使用servlet 2.5,我相信是JSP 2.1(我知道JSP 1.x不支持UTF-8,但我认为 JSP 2.1支持UTF-8。我尝试升级 web.xml 文件以开始

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">

但这只是导致GlassFish从

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:
部署描述符文件中的部署描述符无效
归档[myapp]中的WEB-INF / web.xml。第25行第23列 -
cvc-complex-type.2.4.a:从
元素'display-name'开始发现无效的内容。其中一个
'{ 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,
< a href =http://java.sun.com/xml/ns/javaee =nofollow noreferrer> http://java.sun.com/xml/ns/javaee :async-支持,
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}'。|#]

[#|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.|#]

每当我进行更改,我确保重新启动GlassFish服务器。我假设GlassFish自动检测 glassfish-web.xml 的存在,但是让我知道如果我需要将其配置为GlassFish的某个地方,以实现该文件的存在。

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.

更新1

我注意到,如果我删除请求.setCharacterEncoding(UTF-8); 从JSP页面,以便以下

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");

只是

personName = request.getAttribute("personName");

警告消失。这是否意味着 request.setCharacterEncoding(UTF-8); 应仅在发布 request.getParameter()之前存在于Java servlet中,而不是在JSP页面中?

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?

更新2

我在GlassFish server.log文件中仍然看到编码警告,虽然这次不是来自我的网络应用程序:

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 |警告| glassfish3.1.2 | org.apache.catalina.connector.Request | _ThreadID = 57; _ThreadName = Thread-2; | PWC4011:
无法设置请求字符编码到上下文中的UTF-8,
因为请求参数已被读取,或
ServletRequest.getReader()已经被调用|#]

[#|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|#]

不知道这是来自哪里,或如何修复,但是我将通过在GlassFish管理控制台 -Dfile中添加选项来修改GlassFish JVM编码.encoding = UTF8 这里:配置> server-conf ig> JVM设置> JVM选项,然后重新启动服务器。

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.

我还添加了这里显示的字符集过滤器:如何让UTF-8在Java webapps中工作?虽然,它是为Tomcat,我希望它仍然适用于GlassFish。

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):

The Solution: Create/edit the file glassfish-web.xml (folder 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>

这篇关于JSP:无法弄清楚如何为GlassFish 3.1.2响应设置UTF-8的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆