0

I'm trying to get a Spanish characters in Java using request.getParameter() method. But it is returning junk characters.

URL: http://localhost:8080/myweb/encode.jsp?first_name=Ramón Martínez

Encoded URL: http://localhost:8080/myweb/encode.jsp?first_name=Ram%C3%B3n%20Mart%C3%ADnez%20

Example 1:

JSP CODE:

   <%  request.setCharacterEncoding("UTF-8");
  String firstname= request.getParameter("first_name");
    %>
    <%=firstname %>

Example 2:

JSP CODE:

<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
<%@ page import="java.io.*,java.util.*" %>
<%@ page import="org.apache.commons.lang3.StringEscapeUtils,java.net.URLDecoder,java.net.URLEncoder"   %>

      <%  request.setCharacterEncoding("UTF-8");
String firstname = java.net.URLDecoder.decode(((String[])request.getParameterMap().get("first_name"))[0], "UTF-8");
%>


<%=firstname %>

Output:

Ramón Martínez

Expected Output:

Ramón Martínez

We tried by editing server.xml using the following suggestion (How to get UTF-8 working in Java webapps?) . It's working but we don't want to use this solution by editing server.xml.

We tried by adding following code, but this is not working.

CODE : request.setCharacterEncoding("UTF-8"); 
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ananth
  • 1,520
  • 3
  • 15
  • 28
  • Check this answer http://stackoverflow.com/questions/19086190/request-getparameter-does-not-display-properly-character-encoding-in-java-serv and also refer http://balusc.blogspot.in/2009/05/unicode-how-to-get-characters-right.html for further reading – Mayank Pandya Jan 27 '15 at 10:49
  • Don't just append raw values in the query part of your url, you need to encode them properly using a helper like `java.net.URLEncoder` – morgano Jan 27 '15 at 10:53
  • @MayankPandya , Thanks for your reply i have tried your suggestion, but still it reproducing junk characters. I have updated my question. – Ananth Jan 27 '15 at 13:24
  • @morgano , Thanks for your reply, i have tried your code also, but its not working. I have updated my question – Ananth Jan 27 '15 at 13:25

1 Answers1

0

Try to put <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> at the very beginning of your main jsp file.

Hope this helps.

Leo
  • 5,017
  • 6
  • 32
  • 55