How to allow special characters like the dash in Java Application? - java

How to allow special characters like the dash in Java Application?
This issue is when user Copy and Paste the value from a Word Document then System shows “?” instead of dash “–” character. The copy String has dash char instead of the hyphen. Em dash and hyphen (En dash) are different characters and Em dash character is not a part of ASCII character. Its character code is \u2014. My Application does not support the dash character.
Example 1:- Not Working: Input String: "Anil – Satija" and Display Value: "Anil ? Satija"
Example 2: Name= "Anil - Satija" - hyphen is working fine and shows correct value.
Client-side technologies are Angular 1.5.5 and Server-side technologies are Spring5.2.2. Sending the request as POST. The application supports charset "UTF-8" and content-type is "application/xml". I tried to add the below code in API also to accept the all UTF-8 character set.
index.html Meta Data :
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
API Code is:
#RequestMapping(value = "/data", method = RequestMethod.POST)
#Consumes("application/xml; charset=utf-8")
public #ResponseBody
String getData(HttpServletRequest request, HttpServletResponse response) throws Throwable {
RequestPayLoad requestPayLoad = parseRequest(request);
// requestPayLoad has corrupted value.
// code
}
In debug, at the Client layer (xFactory.js) $scope.data has the correct value with dash but API layer (HttpRequest) request has the corrupted character. So, the System displays the '?' for the corrupted character in this case.

Related

Writing shrugging ASCII emoji ¯\_(ツ)_/¯ in plain text with java

I am developing a java program that writtes output in a text file. When something goes wrong, I must put this ASCII art:
¯\_(ツ)_/¯
I did it with this BufferedOutputStream:
errorOutput.writeln("##################################\n"
+ "##### Error Output ######\n"
+ "##### ¯\\_(ツ)_/¯ ######\n"
+ "##################################\n");
The problem is that when I see the txt log writted with java I get this:
##################################
##### Error Output ######
##### ¯\_(ツ)_/¯ ######
##################################
How can I write the correct ASCII emoji in Java?
Saving the .java file as UTF-8 this code works for me:
String string = "##################################\n"
+ "##### Error Output ######\n"
+ "##### ¯\\_(ツ)_/¯ ######\n"
+ "##################################\n";
Charset.forName("UTF-8").encode(string);
System.out.println(string);
OUTPUT:
##################################
##### Error Output ######
##### ¯\_(ツ)_/¯ ######
##################################
DEMO HERE.
The file is in UTF-8, but you are viewing it in a single-byte encoding:
You are seeing UTF-8 multi-byte sequences for special chars with a char per byte.
Ensure that you read it as UTF-8, because you are indeed using non-ASCII, comma-like, quotes and Japanese. So UTF-8 is fine.
A dirty trick under Windows would be:
String string = "\uFEFF##...
This writes a Unicode BOM char, which when being the first char of a file is interpreted as Unicode marker.
Otherwise create an HTML file with charset specified:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<pre>...</pre>
</body>
</html>
Displaying on the console, System.out, is not possible on a non-UTF-8 system like Windows.
Also for your application to be portable, make sure you specify the encoding for the writing; it often is an optional argument, with an overriden method/constructor.
Solved with these snippets of code:
#GET
#Path("getStdErr/{idApp}")
#Produces("text/html; charset=UTF-8")
public Response getStdErr(#PathParam("idApp") Integer idApp) {
return super.getStderr(jobsMap.get(idApp));
}
.
.
.
.
return Response.ok(job.getStdErr(), "text/plain; charset=UTF-8").build();

Encode-DecodeAdventure

I tried to encode special characters in javascript using encodeURI() and encodeURIComponent() functions and decode them using the java.net.URLDecoder.decode() method and this worked like a charm in firefox. but it doesn't seem to be working in Internet explorer. Is there any alternative code where the same code would work on both browsers?
Example:
when I pass $%^& as the value, after encoding, it becomes %24%25%5E%26. After decoding using java.net.URLDecoder.decode() method, it becomes $%%5E&
this is the actual value-
var str = "$%^&";
var valueJS = encodeURI(str);
var valueJS = encodeURIComponent(valueJS); // to encode even those chars in valueJS that were not encoded by encodeURI()
this is the encoded value-
String value = "%2524%2525%255E%2526";
while(value.matches(".*%25[A-Za-z0-9]*")) {
value = value.replace("%25", "%"); // manually trying to achieve %24%25%5E%26
}
value = java.net.URLDecoder(value, "UTF-8");
// I was expecting the decoded value to be $%^&, but it turns out to be $%%5E&
Fixed it. Added 2 <meta> tags
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
and used javascript escape() instead of encodeURI() and encodeURIComponent()

How to solve Flex utf-8 encoding

I develop a facebook application using flex' s XMLSocket and Java.
When i type 'ş' character in my client side, it prints, however when i send 'ş' character,
it is printed as ??? or any kind of unpredictable characters.
I tried to change my html file's meta tag to
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
but it did not work.
On the whole how can i get rid of this problem.
Thanks.
Use encodeURIComponent(yourstring), this might do the trick.

Special Characters In Webapp being saved differently

I'm creating a webapp using Spring MVC and some of the information I'm pulling is from a Database, so it was edited elsewhere. When I import some have, what I consider, special characters, such as
“_blank”
as opposed to using the standard keyboard
"_blank".
When I display this on my website textarea, it displays fine, but when I attempt to save it back into the string when submitting the form in the spring textArea, the string now has ? where the 'special' characters were. They were obviously imported into a String fine, but somewhere in the save process it's not allowing it as a special character. Any idea what is causing this or why?
Sounds like a character encoding problem. Try setting the character set of the page containing the form to UTF-8.
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />

How to generate Chinese characters from a Java servlet?

My servlet looks like this
protected void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException,IOException
{
PrintWriter out=response.getWriter();
out.println("<Html><Head><Title>Signup</Title></Head>\n<Body>\n");
out.println("\u5982 电话\n");
out.println("</Body>\n</Html>");
}
My browser can display Chinese characters from other websites.
I'm trying 2 different ways to display Chinese characters, but they all showed up as ???
What's the correct way to do it ?
No explicit encoding has been set for the response. The response would therefore be written by the container with the default encoding of ISO-8859-1.
You'll therefore need to specify the appropriate character encoding using the HttpServletResponse.setCharacterEncoding() or HttpServletResponse.setContentType methods. This would be either of:
response.setCharacterEncoding("GB18030");
response.setContentType("text/html; charset=GB18030");
You may also use UTF-8 as the explicit encoding.
Try adding
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
You would need to send Unicode, have your servlet send UTF-8, and have the browser locale set up properly to interpret the characters correctly.
Just setting the character encoding as UTF-8 worked for me.
response.setCharacterEncoding("UTF-8")

Categories