Chinese character gets scrambled when going from JSP to server in Java - 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.

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.

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/

Issue in MetaKeyword,MetaDescription information in JSP using Java

<meta name="description" content="${metaDescription}" />
In case the user is in the french culture, When I view the page source
<meta name="description" content="Trouvez des pneus fiables et s�curitaires pour votre auto, VUS ou camionnette. Canadian Tire offre un grand choix de pneus d'hiver, toute saison et performants"/>
In place of ?, It should be é
I tried to put equivalent UTF-8 code for é. I got the same UTF-8 code in view page source.
Does anyone know what I've done wrong?
This normally indicates that you are looking at a UTF-8 encoded document using ASCII decoding. You might be missing he correct content type definition in your html file, try adding
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
to the <head> in the HTML document.
Hope that helps.
You need to set the JSP page encoding to the desired charset. Add the following to the top:
<%#page pageEncoding="UTF-8" %>
This will do two things:
It tells the server that it should treat the characters in JSP as UTF-8 by response.setCharacterEncoding("UTF-8").
It tells the browser that it should interpret the characters from the server as UTF-8 by response.setContentType("text/html;charset=UTF-8").
See also:
Unicode - How to get the characters right?

Spring variables not carrying into view

I'm very new to Spring and can't seem to figure out what's wrong here. I'm sure it has to be something simple, but anyway:
I was going through the tutorial on this page, and I have nearly identical code. After pulling down the Apache Commons library and the JSTL stuff, I was in business. Everything works, down to it actually using the jsp I specified as the view, but the variable "message" in the controller is not being displayed when the site is rendered. It's not because of the expression language stuff, either, because I'm not getting the ${message} text displaying either. It's just a blank page.
The only reason I know that the jsp is actually being triggered is because I put a title in there that is no where else and it is being used on page display. I also know that the variable is being set in the controller and the action is being called because of a simple sysout that I put in the mapped function.
Thanks for any help!
EDIT
Here's the jsp:
<%#page contentType="text/html" pageEncoding="UTF-8" isELIgnored="false"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>om nom JSP Page</title>
</head>
<body>
${message}
</body>
</html>
And the controller:
package org.me.home.controllers;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.portlet.ModelAndView;
#Controller
public class SomeController {
#RequestMapping("/somepage")
public ModelAndView someAction() {
String mymsg = "Saying hi!";
System.out.println(mymsg);
return new ModelAndView("somepage", "message", mymsg);
}
}
Try to use such a construction:
<%# taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
...
<c:out value="${message}" />
And are you sure, that the problem is in displaying? Is page loaded fine, I mean is all other HTML displayed on page, besides "message"?
EDIT
Try to use org.springframework.web.servlet.ModelAndView instead of
org.springframework.web.portlet.ModelAndView
Your tomcat maybe using the wrong jstl.jar. Under your tomcat lib folder, check if your jstl.jar is the same version you are using with your project.

Categories