What is page contentType in jsp? - java

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

Related

Is Explicit decoding required here?

Say I am displaying escaped value in HTML with below code under text area:
<c:out value="${person.name}" />
My question do I need to decode this value at server side manually or browser will do it automatically ?
No, you need not to decode this value manually .. All you need is:
Specify your HTTP response content type encoding as UTF-8. To be precise use HttpServletResponse.setContentType ("text/html;charset=utf-8");.
Your JSP should have content type encoding set as UTF-8 in your JSP .. To be precise add this meta tag in your JSP and you should be good to go <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
When you have this tag in your JSP then browser will understand that content of this page should be render as per UTF-8 encoding rules.
If don't specify page encoding explicitly using these kind of meta tags or some other mechanism then browser use default encoding associated with it while page rendering and you may not see expected result especially for characters from Unicode's advanced blocks of BMP and Supplementary Multilingual Plane. Check this on how to see the default encoding of browser.
Concept
Server should specify desired encoding scheme in "response stream" and same encoding scheme should be used in JSP/ASP/HTML page.
Server side encoding options
PHP
header('Content-type: text/html; charset=utf-8');
Perl
print "Content-Type: text/html; charset=utf-8\n\n";
Python
Use the same solution as for Perl (except that you don't need a semicolon at the end).
Java Servlets
resource.setContentType ("text/html;charset=utf-8");
JSP
<%# page contentType="text/html; charset=UTF-8" %>
ASP and ASP.Net
<%Response.charset="utf-8"%>
Client side encoding options
Use following meta tag in your HTML page <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
Further reading:
HTTP-charset
This answer
when I get the request.parameter for the escaped input (done thru) <c:out value="${person.name}" />, I get the escaped value and store it in db as it is. For example :- <script>test</script> is stored as <script>test</script> Now when value is fetched from DB and displayed on browser, it renders it correctly i.e <script>test</script> is displayed as <script>test</script>

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

request.getCharacterEncoding() returns NULL... why?

A coworker of mine created a basic contact-us type form, which is mangling accented characters (è, é, à, etc). We're using KonaKart a Java e-commerce platform on Struts 1.
I've narrowed the issue down to the data coming in through the HttpServletRequest object. Comparing a similar (properly functioning) form, I noticed that on the old form the request object's Character Encoding (request.getCharacterEncoding()) is returned as "UTF-8", but on the new form it is coming back as NULL, and the text coming out of request.getParameter() is already mangled.
Aside from that, I haven't found any significant differences between the known-good form, and the new-and-broken form.
Things I've ruled out:
Both HTML pages have the tag: <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
Both form tags in the HTML use POST, and do not set encodings
Checking from Firebug, both the Request and Response headers have the same properties
Both JSP pages use the same attributes in the <%#page contentType="text/html;charset=UTF-8" language="java" %> tag
There's nothing remotely interesting going on in the *Form.java files, both inherit from BaseValidatorForm
I've checked the source file encodings, they're all set to Default - inherited from Container: UTF-8
If I convert them from ISO-8859-1 to UTF-8, it works great, but I would much rather figure out the core issue.
eg: new String(request.getParameter("firstName").getBytes("ISO-8859-1"),"UTF8")
Any suggestions are welcome, I'm all out of ideas.
Modern browsers usually don't supply the character encoding in the HTTP request Content-Type header. It's in case of HTML form based applications however the same character encoding as specified in the Content-Type header of the initial HTTP response serving the page with the form. You need to explicitly set the request character encoding to the same encoding yourself, which is in your case thus UTF-8.
request.setCharacterEncoding("UTF-8");
Do this before any request parameter is been retrieved from the request (otherwise it's too late; the server platform default encoding would then be used to parse the parameters, which is indeed often ISO-8859-1). A servlet filter which is mapped on /* is a perfect place for this.
See also:
Unicode - How to get the characters right?
The request.getCharacterEncoding() relies on the Content-Type request attribute, not Accept-Charset
So application/x-www-form-urlencoded;charset=IS08859_1 should work for the POST action. The <%#page tag doesn't affect the POST data.

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