JSP / GlassFish:如何正确设置UTF-8编码 [英] JSP/GlassFish: how to setup UTF-8 encoding correctly

查看:614
本文介绍了JSP / GlassFish:如何正确设置UTF-8编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我发现这篇不错的文章:



http://www.javapractices.com /topic/TopicAction.do?Id=206



描述我需要担心编码的3个位置。由于我的(1)Oracle数据库目前设置为UTF-8,这使得浏览器和(3)服务器担心。



我也发现详细文章



http://balusc.blogspot.com/2009/05/unicode-how-to-get-characters-right.html#JSPServletRequest



我在下面试着跟随,但是有一些关于实现的新手问题。

为了解决浏览器问题,我确保在每个JSP页面的顶部包含以下内容:

 <%@ page pageEncoding =UTF-8%> 

(仅供参考,请参阅 here )。



致地址在发布 request.getParameter() request.getAttribute()之前,我确保在Java servlet和JSP页面中包含以下行)语句:

  request.setCharacterEncoding(UTF-8); 

由于我使用的是GlassFish 3.1.2,我知道它默认不使用UTF-8 ,所以我需要以某种方式手动设置它。

我看到很多网站都在讨论名为 glassfish-web.xml 的文件。这是正常glassfish安装的一部分吗?我不知道在哪里找到它。我在我的Web应用程序的 WEB-INF 文件夹中使用了 web.xml 文件。有人可以帮我弄清楚是否需要修改这个 web.xml文件,或者我需要找到还是创建一个名为的新文件glassfish- web.xml ,为glassfish配置编码?



我的 web.xml 文件开头为:

 <?xml version =1.0encoding =UTF-8?> 
<!DOCTYPE web-app PUBLIC - // Sun Microsystems,Inc. //DTD Web Application 2.3 // EN
http://java.sun.com/dtd/web- app_2_3.dtd>
< web-app>
...

对于JSP / servlet请求,我在 web.xml file

 < parameter-encoding default-charset = UTF-8/> 

可以放入 web.xml 文件?或者,它是否需要进入一些 glassfish-web.xml 文件?

对于JSP / servlet响应,我将以下内容放在 web.xml 文件中(请参阅接受的答案< a href =https://stackoverflow.com/questions/6279504/unable-to-change-charset-from-iso-8859-1-to-utf-8-in-glassfish-3-1>这里 a>):


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


我假设这些行只是在< web-app> < / web-app> 。但是,让我知道他们是否应该使用其他描述符(例如< glassfish-web-app> < / glassfish- web-app> )?



我也在JSP < head> 部分:

 < meta http-equiv =content-typecontent =text / html; charset = UTF-8\" > 

有用的参考资料:

如何摆脱警告: PWC4011:无法将请求字符编码设置为UTF-8

https://stackoverflow.com/tags/servlet-filters/info



https://wikis.oracle.com/display/GlassFish/FaqHttpRequestParameterEncoding

解决方案 glassfish-web.xml 是一个您可以在 WEB-INF 文件夹。对于Glassfish 3.x,它必须类似于这样:

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

你说得对,参数编码设置必须在这个文件中,而不是在 web.xml :)



另见:


I'm looking for help to get all my layers in the stack to UTF-8 encoding.

I found this nice article:

http://www.javapractices.com/topic/TopicAction.do?Id=206

describing the 3 places I need to worry about encoding. Since my (1) Oracle database is currently set to UTF-8, that leaves the (2) browser and (3) server to worry about.

I also found this detailed article

http://balusc.blogspot.com/2009/05/unicode-how-to-get-characters-right.html#JSPServletRequest

which I'm trying to follow below, but with some newbie questions about implementation.

To address the browser, I've made sure to include the following at the top of each JSP page:

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

(For reference, see here).

To address the server, I made sure to include the following line in the Java servlet and JSP pages before issuing a request.getParameter() or request.getAttribute() statement:

request.setCharacterEncoding("UTF-8");

Since I'm using GlassFish 3.1.2, I understand it does not use UTF-8 by default, so I need to set it manually somehow.

I've seen a lot of websites talking about a file named glassfish-web.xml. Is this part of the normal glassfish install? I don't know where to find it. I've been using the web.xml file in my WEB-INF folder for my web application. Could someone help me figure out whether I need to modify this web.xml file, or do I need to locate or create a new file named glassfish-web.xml, to configure encoding for glassfish?

My web.xml file starts with:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" 
    "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app> 
...

For the JSP/servlet request, I include the following line in the web.xml file

<parameter-encoding default-charset="UTF-8"/>

Is this OK to put in the web.xml file? Or, does it need to go in some glassfish-web.xml file?

For the JSP/servlet response, I put the following in my web.xml file (see accepted answer here):

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

I'm assuming these lines just insert between <web-app> and </web-app>. But, let me know if they should instead go inside some other descriptor (such as <glassfish-web-app> and </glassfish-web-app>)?

I also put the following in the JSP <head> section:

<meta http-equiv="content-type" content="text/html; charset=utf-8">

Useful references:

How to get rid of WARNING: PWC4011: Unable to set request character encoding to UTF-8

https://stackoverflow.com/tags/servlet-filters/info

https://wikis.oracle.com/display/GlassFish/FaqHttpRequestParameterEncoding

解决方案

The glassfish-web.xml is a file you can create in your WEB-INF folder. For Glassfish 3.x it has to look similar to this:

<?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>
    <jsp-config>
    </jsp-config>
    <parameter-encoding default-charset="UTF-8" />
</glassfish-web-app>

And you are right, the parameter-encoding setting has to be in this file and not in the web.xml :)

See also:

这篇关于JSP / GlassFish:如何正确设置UTF-8编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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