Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSP/GlassFish: how to setup UTF-8 encoding correctly

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

like image 661
user46688 Avatar asked Nov 06 '14 01:11

user46688


1 Answers

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:

  • glassfish-web.xml vs sun-web.xml vs web.xml
  • Glassfish encoding
like image 162
unwichtich Avatar answered Oct 21 '22 11:10

unwichtich