Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Problems with accented characters in jsp

I am trying to collect accented characters [áéíóúÁÉÍÓÚ] on a form but are not correctly sent to action:

JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
[. . .]
<s:form action="resultRot" method="post" theme="simple">
<s:textfield name="param" theme="simple" size="20" maxlength="20" style="text-transform: uppercase; text-align:center"/>
<s:submit name="submit" key="ejercicios.roturaPalabras.corregir" align="center"/>

When I pick the parameter param in the action class, it does not contain the correct values. I use Eclipse and I checked out the project encoding is ISO-8859-1

I´ve tried with UTF-8 encoding too (in my jsp):

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>

I´ve tried with URLDecoder/Encoder too:

String prueba = java.net.URLDecoder.decode(solucionIntroducida, "ISO-8859-1"); 

Thanks in advance.

like image 649
user2213180 Avatar asked Mar 23 '23 04:03

user2213180


1 Answers

The best practice is to use UTF-8 everywhere.

Here you can find how to do it by altering the application server's connectors, while for the other parts you can simply specify it in each JSP (like you are doing), or specify it once in web.xml:

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

To debug (and print) a Request in Eclipse, you need to ensure that you are using UTF-8 as text file encoding.

Go to Preferences -> General -> Workspace -> Text file encoding , and set Other: UTF-8

In the code, be sure to always specify the character encoding when converting String to Byte[] or viceversa:

String str = new String(myByteArray,"UTF-8");

and

byte[] ba = myString.toByteArray("UTF-8");

This are the steps needed in order to preserve the correct characters everywhere.

like image 125
Andrea Ligios Avatar answered Apr 02 '23 17:04

Andrea Ligios