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");