JSP / GlassFish: how to properly configure UTF-8 encoding

I am looking for help to get all my layers on the stack before UTF-8 encoding.

I found this nice article:

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

describing 3 places, I need to worry about coding. Since my (1) Oracle database is currently installed in UTF-8, this leaves the server (2) and 3) the server to worry about them.

I also found this detailed article.

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

which I am trying to follow below, but with some newbies implementation questions.

To access the browser, I included the following at the top of each JSP page:

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

(For reference see here ).

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

 request.setCharacterEncoding("UTF-8"); 

Since I use GlassFish 3.1.2, I understand that it does not use UTF-8 by default, so I need to somehow install it manually.

I have seen many websites talking about a file called glassfish-web.xml . Is this piece of regular glass fish installed? I don’t know where to find him. I used the web.xml in my WEB-INF folder for my web application. Can someone help me figure out if I need to modify this web.xml , or do I need to find or create a new file called glassfish-web.xml to set up the encoding for glass fish?

My web.xml 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 a JSP / servlet request, I include the following line in the web.xml

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

Is it possible to insert the web.xml in this order? Or, do I need to go into some glassfish-web.xml ?

For the JSP / servlet response, I put the following in my web.xml (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 assume that these lines are simply inserted between <web-app> and </web-app> . But, let me know if they should instead enter a different descriptor (for example, <glassfish-web-app> and </glassfish-web-app> )?

I also added the following to the JSP <head> section:

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

Useful links:

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

stack overflow

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

+6
source share
1 answer

glassfish-web.xml is a file that you can create in your WEB-INF folder. For Glassfish 3.x, it should look something like 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 of the encoding parameter should be in this file, and not in web.xml :)

See also:

+9
source

Source: https://habr.com/ru/post/977802/


All Articles