how do i get utf8 value from querystring using jsp - java

I am using JSP pages and I am trying to get the UTF-8 value from query string using the statements as below request.getparameter("q");
It is working fine, it gives me the appropriate results but when I am trying it using IE9 it gives me ????? instead of unicode value.
My question is how do I get proper unicode value from query string using JSP that will gives correct values on all browser including IE, and what statements I need to add within JSP page to get correct values at IE as well.
Please help me in this regard.
thank you

In the jsp page directive you need to set content-type to utf-8 (for each jsp page)
<%# page contentType="text/html; charset=UTF-8" pageEncoding="ISO-8859-1" %>
If still problem persist then use this SO question to handle encoding for DB, Tomcat. HERE

Related

What is page contentType in jsp?

What is the point of this part of an autogenerated jsp-page in intellij?
"<%# page contentType="text/html;charset=UTF-8" language="java" %>"
Because I don't see any loss of functionality if I remove this line. What's it's purpose?
A jsp page should have the contentType set to text/html but the webserver may default files to text/html. Same with the charset.
The UTF-8 in the middle is to set the encoding format, otherwise there will be garbled code when the browser opens the page, such as garbled code displayed in Chinese

UTF-16 instead of UTF-8 with mySQl to work with foreign languages

I have a JSP page that shows arabic strings from the database and I'm setting UTF-8 encoding in that way:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
The column that I defined in MySql Workbech 6 is VARCHAR(255).
In this way everything works fine and I can see Arabic strings in my browser.
Now the problem is that I want to use UTF-16 instead of UTF-8. I tried in many ways but without success.
1) First way.
I just changed the JSP directive in that way:
<%# page language="java" contentType="text/html; charset=UTF-16" pageEncoding="UTF-16"%>
But the page shows strange character.
2) Second attempt.
I tried not only to change the JSP, but also to change the collaction of my table to:
Collation: UTF-16 default collation
But in that way it doesn't work too.
what do you suggest me to do, in order to use UTF-16?

How to set the response content type as applicaiton/json in JSP?

How can I set the content the as applicaiton/json in jsp ?
I have tried the following code,
<%# page language="java" contentType="application/json; charset=UTF-8"
pageEncoding="UTF-8"%>
But when I run the JSP file ,I got this and the page doesn't opened in browser.
Hope our stack users will give best solution.
With the content type set to application/json. JSON string will be downloaded similar to a file since it is not a HTML doc. You need to set the content as text/html. But it is incorrect to return JSON data as content type text/html. What do you want let us know specifically ?
Page direct should work. You can also try
<%
response.setContentType("application/json");
%>

Character is corrupted when use request.getParameter() in java

I have a web page where I do a search based on the text given in a text box. This text can be in any language like japanese, chinese etc (or any mbcs character).
Now when i enter a text in japanese (or any other mbcs character), the result populates the screen (form) with some wierd characters.
For Example: testテスト will turn into testãã¹ã.
When i see the post parameters in Firebug (debugging tool) i can see that the search string goes as testテスト however when i put debug statements in my code, i can see that request.getParameter("searchString") is not able to identify the japanese characters and turn them into some wierd chars.
My JSP header already has <%# page contentType="text/html; charset=UTF-8"
I have also tried putting pageEncoding="UTF-8" in this but it didn't help.
I tried setting character encoding like request.setCharacterEncoding("UTF-8") also just before doing request.getParameter but that too didn't work for me.
After going through a few forums and blogs i also tried setting useBodyEncodingForURI=true in the <Connector> of tomcat config but that also did not help me.
Can anybody suggest me something to resolve this issue?
set the following encoding in every servlet/ action
response.setContentType("UTF-8");
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
also set following in first servlet/action
for japanese
response.setCharacterEncoding("UTF-8");
request.setCharacterEncoding("UTF-8");
session.setAttribute(Globals.LOCALE_KEY, new Locale("jp", "ja_JP"));

Chinese characters not being rendered on JSP

I have a jsp say hello.jsp. Now there are 2 use cases
the request is redirected to hello.jsp through mainserverlet and in this case, it renders the "editable" text in chinese properly.
The second case is when i change the drop down menu on hello.jsp, it "resubmits" the request to itself, instead of mainservlet and in this case the text in chinese is not being displayed properly.
The charset=UTF-8 encoding has been set in HTML tag of the jsp.
I have tried to see how form is being submitted through javascript and the chinese text remains the same just before "submit". I don't know what happens that it is not being rendered after this.
Any pointers or suggestions?
Have you tried page tag?
Sample:
<%# page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
Obs: You need to put this on top of every page you use.
PS:
If you are thinking you can add above line in your header.jsp file and then include header file in your required file like this:
<jsp:include page="/resources/header.jsp" />
It won't work unless you add that line in every page.

Categories

Resources