本文介绍了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:我的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 asrequest.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每当我进行更改,我确保重新启动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:不知道这是来自哪里,或如何修复,但是我将通过在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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-23 02:34