7

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

Community
  • 1
  • 1
user46688
  • 733
  • 3
  • 11
  • 29

1 Answers1

12

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:

Community
  • 1
  • 1
unwichtich
  • 13,712
  • 4
  • 53
  • 66
  • Thanks @unwichtich, so is it correct that BOTH files must exist in WEB-INF folder (that is, `web.xml` AND `glassfish-web.xml`)? Also, do I need to configure GlassFish to look for this `glassfish-web.xml` file after I create it, or will GlassFish find it automatically? – user46688 Nov 06 '14 at 20:10
  • 1
    The glassfish-web.xml is not really required if you don't have to use it for some Glassfish-specific configuration properties like the default-encoding. The file is scanned automatically if placed in the WEB-INF folder, make sure to Clean & Build your project before redeploying :) – unwichtich Nov 06 '14 at 20:17
  • Oh sorry, I forgot that this is also required. I updated my answer. – unwichtich Nov 06 '14 at 20:56