Chinese characters not being rendered on JSP - java

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.

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

jsp ${pageContext.request.contextPath} dosent get requested

Currently i have a .jsp project where my welcome page is a servlet
<welcome-file>frontpage</welcome-file>
The servlet sets gets two ressources, a header file containing the < nav> and a footer containing the < footer>
request.setAttribute("header1", sc.getResource("/includes/nav.jsp").toString());
request.setAttribute("footer", sc.getResource("/includes/footer.jsp").toString());
And forwards to the index.jsp page
getServletContext().getRequestDispatcher("/WEB-INF/jsp/index.jsp").forward(request, response);
My question is.
When i get the ressource (footer.jsp), how can i in the footer.jsp dynamically import / include images?
I tried the following
<img src="${pageContext.request.contextPath}/images/picture1.png" alt="picture1"/>
But the expression ${pageContext.request.contextPath} gets treated as a string instead of a command, and does not get the context path.
I suspect its because the content of the footer.jsp is fetched in this manner and their for the context path isint actually ever requested within the footer.jsp.
But how do i solve this?
add <%# page isELIgnored="false" %> in top of your JSP page, to enable expression language.
and to include a JSP page with other use <jsp:include like:
<jsp:include page="/includes/nav.jsp"/>
<jsp:include page="/includes/footer.jsp"/>
This is not the way to include stuff. Use jsp:include action to include the header/footer. If for some reason you really want to do it in the servlet, see this post. As long as you just grab a resource like you do, you're reading the file like any text, there is no JSP compilation/evaluation.

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

how do i get utf8 value from querystring using jsp

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

Categories

Resources