Java – JSP / GlassFish: how to correctly set UTF-8 encoding

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

I found this good article:

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

Describe three areas where I need to worry about coding Since my (1) Oracle database is currently set to UTF-8, I need to worry about (2) browser and (3) server

I also found this detailed article

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

I'm trying to follow the following, but there are some novice questions about implementation

To solve the browser problem, I ensure that the following is included at the top of each JSP page:

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

(for reference, see here)

To solve the server problem, I make sure to issue a request Getparameter () or request Before the getattribute () statement, include the following lines in the java servlet and JSP pages:

request.setCharacterEncoding("UTF-8");

Because I use GlassFish 3.1 2. I know it does not use UTF - 8 by default, so I need to set it manually in some way

I've seen many websites talking about a website called GlassFish web XML file Is this part of an ordinary GlassFish? I don't know where to find it I've been using web. Inf in the web - inf folder XML file as my web application Someone can help me find out if I need to modify this web XML file, or do I need to find or create a file called GlassFish web XML to configure the encoding of GlassFish?

My web XML file to:

<?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 JSP / servlet requests, I'm on the web The XML file contains the following lines

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

Can be put into the web XML file? Or, does it need to go into some GlassFish web XML file?

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

I assume that these lines are just inserted between < web app > and < / web app > But let me know if they should enter other descriptors (such as < GlassFish web app > and < / GlassFish web app >)?

I also added the following content to JSP < head > part:

<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

Solution

glassfish-web. XML is a file that you can create in the web - inf folder For GlassFish 3 x. It must be similar:

<?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>

You are right. The parameter encoding settings must be in this file, not in the web xml

The above is the whole content of Java – JSP / GlassFish: how to correctly set UTF-8 coding collected by programming house for you. I hope this article can help you solve the program development problems encountered in Java – JSP / GlassFish: how to correctly set UTF-8 coding.

If you think the content of the programming home website is good, you are welcome to recommend the programming home website to programmers and friends.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>