Servlet response.sendRedirect encoding problems - java

So I'm redirecting my user using GET in a naive way:
response.sendRedirect("/path/index.jsp?type="+ e.getType()
+"&message="+ e.getMessage());
And this was working fine until I had to send messages, as actual text to be shown to users. The problem is if the message has non-ASCII characters in it. My .jsp files are encoded in UTF-8:
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
So all non-ASCII characters in 'message' gets garbled. I don't want to set my JVM default encoding to UTF-8, so how do I solve this? I tried to use
response.setCharacterEncoding("UTF-8");
on the Servlet before redirecting, but it doesn't work. when I try to execute:
out.print(request.getCharacterEncoding());
on my .jsp file it prints 'null'.

The sendRedirect() method doesn't encode the query string for you. You've to do it yourself.
response.sendRedirect("/path/index.jsp?type=" + URLEncoder.encode(e.getType(), "UTF-8")
+ "&message=" + URLEncoder.encode(e.getMessage(), "UTF-8"));
You might want to refactor the boilerplate to an utility method taking a Map or so.
Note that I assume that the server is configured to decode the GET request URI using UTF-8 as well. You didn't tell which one you're using, but in case of for example Tomcat it's a matter of adding URIEncoding="UTF-8" attribute to the <Context> element.
See also:
Unicode - How to get the characters right?
Unrelated to the concrete problem, the language="java" is the default already, just omit it. The contentType="text/html; charset=UTF-8" is also the default already when using JSP with pageEncoding="UTF-8", just omit it. All you really need is <%# page pageEncoding="UTF-8"%>. Note that this does effectively the same as response.setCharacterEncoding("UTF-8"), so that explains why it didn't have effect. The request.getCharacterEncoding() only concerns the POST request body, not the GET request URI, so it is irrelevant in case of GET requests.

Thanks you ... When i am using the response.sendRedirect("/path/index.jsp?type=" + URLEncoder.encode(e.getType(), "UTF-8"), My problem got fixed...
When we are using the response.sendRedirect(): We should encode the URL by the URLEncoder.encode() function then only.. it will be encoded correctly..
Thanks again...

Related

Getting junk value while sending hindi characher in request string

i am sending दर्शन as a request parameter value from javascript and while i am fetching in my struts action using requestMap.get(key)[0] then it gives me दरà¥à¤¶à¤¨ junk character?
here my action implements ServletResponseAware and i declare requestMap variable so it directly map request parameter value to requestMap object
The default encoding for HTTP protocol is not utf8. You need to handle it separately on your server side code.
Please refer the following link for more.
Common problems with i18n and servlets/jsp-s
You need to add the following in your jsp
<%#page contentType="text/html" pageEncoding="UTF-8"%>
Displaying Hindi font in jsp

Currency formatting Yen in Internet Explorer has extra symbols "ï¿¥"

I'm just using:
NumberFormat cfLocal = NumberFormat.getCurrencyInstance(Locale.JAPAN.toString());
And it works fine on most devices/browsers/currencies except in IE and Yen I'm getting a few extra characters - could it be a weird encoding being sent, or browser specific settings screwing up handling of the ¥ symbol?
The output looks like this:
ï¿¥15,180
Would appreciate any leads or tips.
Edit:
I am outputting the values with JSP. JSP file is defined with this preamble:
<?xml version="1.0" encoding="ISO-8859-1" ?>
<%# page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
I'm no encoding expert but your XML appears to say one thing and your content-type another - try setting both to UTF-8.
If your data is coming from outside the application (e.g. a database, file, etc.), what is the encoding of the source? For example, a MySQL database may have a different character set specified.
If you are using a web server like Apache, is that changing the encoding? For example you can have a httpd.conf directive to set the default character set:
AddDefaultCharset utf-8
It would be worth checking the HTTP Headers in the browser to see what is actually being sent to the browser, and work back from there.
EDIT
Thinking about it more, I'm not sure if the XML encoding is necessarily the problem. It would probably be best to check the headers first, and compare it to the the html being produced.

HTML not passing validation

The below line is not passing validation in my application. The error is in Netbeans is...
Bad value " /content/edit" for attribute href on element "a": WHITESPACE in PATH
Add Content
The runtime error is:
org.apache.jasper.JasperException: /base.jsp(9,25) PWC6213: quote symbol expected
I am passing an attribute for this value. Why am I getting this error when I pass a value?
Don't use scriptlets in JSP. Use the JSP EL:
Add Content
Add Content
Use single quotes with urlPrefix. It should work.
Try this:
<% String urlPrefix = (String)request.getAttribute("urlPrefix"); %>
Add Content
or better this:
<%
String urlPrefix = (String)request.getAttribute("urlPrefix");
String url = urlPrefix + "/content/edit";
%>
Add Content
or even better use EL:
Add Content
It's worth mentioning the protection against XSS attacks as Asaph pointed out in his comment:
Add Content
might do the trick if you include
<%# taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
at the top of your JSP.
I've just done a simple test and the following line has no syntax error and runs without throwing an exception whether the urlPrefix attribute is set or not:
Add Content
There is no syntax error at all. In the case of there being no urlPrefix attribute set, the resulting html is:
Add Content
In the case of urlPrefix being equal to http://example.com, the resulting html is:
Add Content
Here is a quick little standalone test.jsp file to demonstrate:
<% request.setAttribute("urlPrefix", "http://example.com"); %>
Add Content
You can remove the first line to test the null case.
So we've demonstrated that the line you posted as the alleged offending line is not actually problematic. Some possibilities:
Are you sure you're looking at the correct line?
Are you sure you're looking at the correct file?
Are you sure you've deployed your application?
Are you sure you're looking at the correct url/environment?

J2EE - How to remove empty space,blank lines from server response

Is there a way to remove blank lines from server response? I have tried out:
<init-param>
<param-name>trimSpaces</param-name>
<param-value>true</param-value>
</init-param>
and
<%# page trimDirectiveWhitespaces="true"%>
which didn't solve the issue properly since in init param method it removed even space between words, 2nd method didn't work either since it needs java servlet version to be 2.5.
Your advice will be very helpful ..
you should create a filter-servlet that remove blank lines and chained the response to your servlet.
when the server convert the JSP page to servlet it add implicitly blank lines (\n).
add out.clear() befor writing data to clear out object.
Add the following directives to your page and the lines should disappear:
<%# page contentType="text/xml" %>
<%#page pageEncoding="UTF-8"%>
<%# page trimDirectiveWhitespaces="true"%>
I think, you should use an IDE like eclipse. Where you can just select the whole code and format it and follow all java syntax guidelines by just right clicking and seletcing a few options.
Please use good IDE's all of them will help you follow good guidelines and make your work simpler and easier to read and understand

Arabic characters appears like ??? after adding a Filter to JSP page

When I add a Filter to a particular JSP file, the Arabic characters in the output appears like ???, even when the page encoding is been set to UTF-8 by <% #page pageEncoding="UTF-8"%> and <% response.setCharacterEncoding("UTF-8");%>.
The strange thing is, before I added the Filter, the output of all Arabic pages appears with correct encoding. Can someone tell how this problem is caused and how I can solve it?
The filter is either directly or indirectly commiting the response and/or accessing the Writer or OutputStream of the HttpServletResponse which causes that the encoding cannot be changed anymore in the JSP. Fix the code in the filter accordingly. The filter should in any way not be writing anything to the response body. There the JSP (for HTML) or Servlet (for other content) is for.
By the way, you don't need to call <% response.setCharacterEncoding("UTF-8");%>. The <%#page pageEncoding="UTF-8"%> already implicitly does that.

Categories