German umlaut characters in form param resteasy not working - java

My form
<form accept-charset="UTF-8">
My Html header
<meta http-equiv="Content-type" content="text/html; charset=utf-8"/>
<meta charset="utf-8">
Tried both and single as well.
My Resteasy filter
request.setCharacterEncoding("UTF-8");
My Method annotation
#Consumes(MediaType.APPLICATION_FORM_URLENCODED + "; charset=UTF-8")
Alter all this settings , my Resteasy service receiving the german characters wrongly for only form post. json request working fine.
Is there any other setting I have to try?

works fine with filter level settings. I didn't restart the server properly while trying these settings.
request.setCharacterEncoding("UTF-8");

Related

ERROR http 400 with java and servlets, JSP

How about community, this error happened to me today and I would like to know if some of you ever happened to you and how to solve it. I am creating an application with servlets in java that the functionality is to have a button, where by clicking it sends me a json with 50 records through the URL so that I can generate an excel. Now, all good when it comes to 10 records, no problem happens, when you have more than 20 records in that json or so, that's where the error happens to me. Investigating I found that the error happens because the header I am sending is too long. Here is an example of what the header I'm sending is:
Url: localhost:8080/pruebas/vistas/excel/Reporte_Insertados.jsp?registros=[{"CODPER":"123456","NRO":"1","DNI":"45874587","APELLIDOS_NOMBRES":"ROJAS%20LOPEZ%20GUSTAVO","FECHA":"14/01/2020","MONTO":"150.50","OBSERVACION":"DADSADSA","RAZON_SOCIAL":"","ESTADO":"DATOS%20CORRECTOS","":""}...] so until you have 50 records.
So my question is: If any of you solve it or how could I do it from my servlet to allow the header to be long, since it is the problem for which it does not leave me and the error happens to me.
I leave code of my application. Thank you in advance community.
My Javascript code:
function exportarReporteInsertados(registrosInsertados){
let tabla_reporte_insertados = $("#tablaCargaMasiva").tableToJSON({});//Here grab all 50 records.
window.location = "excel/Reporte_Insertados.jsp?registros="+JSON.stringify(tabla_reporte_insertados)
}
My jsp code to receive the data
<%#page import="org.json.JSONObject"%>
<%#page import="java.text.SimpleDateFormat"%>
<%#page import="java.text.DateFormat"%>
<%#page import="java.util.Date"%>
<%#page import="org.json.JSONArray"%>
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<%
String datos = request.getParameter("registros");
JSONArray jsonArray_datos = new JSONArray(datos);
%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>JSP Page</title>
</head>
<body>
<%
response.setContentType("application/vnd.ms-excel");
response.setHeader("Content-Disposition", "attachment; filename=REPORTE_CARGA_MASIVA.xls");
%>
ERROR
enter image description here
#Felipo If you need to send a json, kindly refrain from adding it to the url. The url string has a character limit.Please look at : What is apache's maximum url length? . Also, according to convention in order to send a json via the request you need to add it to the request body instead of the url. Use an ajax request to send the data to the endpoint instead of passing it via the url.

Java - Servlet post parameters bad encoding [duplicate]

This question already has answers here:
How to pass Unicode characters as JSP/Servlet request.getParameter?
(5 answers)
Closed 7 years ago.
I was working normally on my netbeans project. I sent post parameters from html forms to servlets and then capture them with request.getParameter... and all worked fine. But some hours ago, I don't know what happened with netbeans because all new servlets that I create, the requested post parameters are not encoded well. I mean that if I send "tílde" or "ñ" it will be received as "tílde" or "ñ". The old servlets still work fine, receiving well the post parameters.
With GET parameters works fine, but with POST parameters always the same with new servlets. Old servlets work fine post parameters.
I tested receiving parameters with php and they are correctly formated, so I discart the posibility of being the jsp encoding.
I think may be netbeans error, but I really dont know.
Here some code and images:
The servlet
public class TestParameters extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
PrintWriter out = response.getWriter();
System.out.println("> Ñ,ñ, tílde. Parámetros: " + request.getParameter("name"));
}
The JSP:
<%#page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<form method="post" action="/TestParameters">
<input type="text" name="name">
<button type="submit">Enviar</button>
</form>
</body>
</html>
Html input:
NetBeans console with POST parámeters:
Netbeans console with GET parámeters:
And receiving with php, works well:
echo $_POST['name'];
Php html echo:
Finally I have resolved my problem. I have implemented a filter that encode all requests. But this only work for POST parameters that is what I need. That's because for POST parameters Tomcat's default encoding is iso-8859-1 and I was ignoring that. For GET parameters the way is
Encoding filter for java web application

How can I fix the charset on my project?

It is a JPA and
I`m getting the word "descri��o" on DB, and in my debug on serverside, right before the persistence, when it receives from the Form it is like "descri��o".
I have a form, <form action="..." method="post">, it has some fields and sends to a REST service, #post, and is in there that my breakpoint comes and show it.
I used <meta http-equiv="Content-Type" content="text/html" charset="UTF-8" /> on the head of the form HTML page, but it is still not as I want...
Problem: bad characters on server side.
Thanks a lot.
The meta tag is invalid ...
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
This should fix issue with the form.
You can also try the form accept-charset attribute.

JSP data to be downloaded to Excel sheet using ActiveQuery results in character problems

downloading data using Active query from JSP page with some parameters is leading to character problems. Special characters in the german language as for example, ö, ä, ß are printed as ö, ä and ß.
Debugging the JSP page in Java shows that the result that is returned by the JSP page is correct. So the problem seems to be due to conversion within excel after download, most probably due to a unsopported charset.
I tried to convert the result string in JSP to different charsets, but the problem still persists.
Does anyone know a solution?
Thank You very much in advance!
Did you try setting the encoding of the page?
<%# page contentType="text/html; charset=UTF-8" pageEncoding="UTF8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
...
If you can't find a solution on the Microsoft side, I'd recommend this alternative here:
http://poi.apache.org/

Java check - charset, encoding of html page - like browsers do

How to check what really charset, encoding of some html page ?
For example, the charset of some html page is iso-8859-1, but the content of the html written with utf8
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
...
here is content with utf8
...
</html>
How to check it, Is it possible to check with java charset, encoding of html page,
like it's done in browsers ?
Thank you !

Categories