How can I fix the charset on my project? - java

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.

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.

German umlaut characters in form param resteasy not working

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

Chinese character gets scrambled when going from JSP to server in Java

I have already set
<%#page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
This in my JSP. But, after doing as
xmlHttp.setRequestHeader("SEARCH_TEXT", srctxt);
or
passing as a parameter in the AJAX url,
I am still getting Chinese words as scrambled letters or '????' marks.
Required some insight regarding this. Please help.
#Mena, After your comment, I checked the 'encodeURIComponent' and as I encoded the Chinese string and decoded it my server side code, it got resolved. Thanx. Pasting code for reference,
Client Side code,
xmlHttp.setRequestHeader("SEARCH_TEXT", encodeURIComponent(srctxt));
Server Side Code,
CommonUtils.decodedStringValue(request.getHeader("SEARCH_TEXT"));
Hope this helps.

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/

How do I change the address bar when I make an Ajax request?

I have created a website that intensively use Ajax to load its contents.
I want the pages I select to load on AJAX but at the same time the URL also changes without reloading the whole page content. How would I achieve that? I've googled already but my search did not yield any results.
Let's say I have this page:
<%# taglib prefix="s" uri="/struts-tags"%>
<%# taglib prefix="sj" uri="/struts-jquery-tags"%>
<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<sj:head />
</head>
<body>
<h5>Struts Jquery Ajax Integration </h5>
<div id="resultContent"></div>
<noscript>Please Turn On Javascript to make the full use of this site</noscript>
<h4>Choose A task</h4>
<ul>
<s:url value="views/ajaxvalidation.jsp" var="ajaxvalidation" />
<li><sj:a targets="resultContent" href="%{ajaxvalidation}">Ajax Validation </sj:a></li>
</ul>
<div>
<h6>Play A Music while You Navigate</h6>
<audio src="x.mp3" controls>Your browser does not support the
audio element.
</audio>
</div>
</body>
</html>
I clicked the Ajax Validation link. it will not reload the page but however the url would be something like this:
localhost:8090/AppName/ajaxvalidation.jsp
or this:
localhost:8090/AppName/ajaxvalidation.action
How would I achieve such a goal?
Note that I am using this plugin: struts2-jquery
You need to use the History API
window.history.pushState(data, title, url)
Update
Noticing that you use struts2-jquery you can add ajaxhistory="true" to enable the build in ajax history functionality. See http://code.google.com/p/struts2-jquery/wiki/HeadTag#Attributes
<sj:head ajaxhistory="true" />
Keep in mind, though, that it is not supported in all browser versions..
You can use history.pushState if the browser supports (read < IE10)
var boolPushState = false;
if (typeof history.pushState !== "undefined") {
boolPushState = true;
}
if (boolPushState) {
history.pushState(null, null, "URL");
}

Categories