I'd like to make a simple link from a jsp (A) to another (B) with adding a parameter that I want to transmit from A to B.
In jsp A: <% String value="object_1"; %> <a href="url_b.jsp?code=<%= value %>"><%= value %></a>
In jsp B: <% String code = (String)request.getParameter("code"); %>
This operation is correctly done. But if I change the variable "value" of my jsp A into "object%1" there is a problem. The value of variable "code" in the jsp B is null. The problem seems to come from the character '%'
How can I do to avoid this problem in case of specific character that I have to consider in my application?
Request parameters need to be URL-encoded to avoid these types of problems.
Rather than building up URL's in strings, you should use built-in mechanisms such as the JSTL <c:url> tag which will make sure that everything is set up correctly.
If you still want to do it by hand, check out the java.net.URLEncoder class.
The returned url seems to be correct (test.jsp%3Fcode%3Dtest) but it doesn't works even if there is no specific character. The following error message is thrown: "The requested resource (/test.jsp%3Fcode%3Dtest) is not available"
Could you tell me what am I doing wrong? Or how can I use JSTL in this example (I hadn't ever use it).
The URL "test.jsp?code=banana" has one parameter, and it is "code=banana". You only have to apply URL-encoding to the parameter values, namely "banana" in this case. Do not URL-encode anything else.
Pierre Peron
Greenhorn
Joined: Apr 20, 2006
Posts: 11
posted
0
Ok! So by this way a part of my problem is solved. The specific characters that were not recognized like %=@ are checked now. But I still can't make it work whith the unicode UTF-8 (Japanese characters for example).
That is what should enable me to recognize the unicode in the jsp pages:
for java: <%@ page language="java" pageEncoding="utf8" contentType="text/html; charset=UTF-8" %> and before getting the requesting value of the "code" parameter: request.setCharacterEncoding("UTF-8");
I try by decoding the request too but the result is the same: the code string is: ジポネキ the encoded code is : %E3%82%B8%E3%83%9D%E3%83%8D%E3%82%AD and the value requested is : ������������
Am I doing something wrong? Or does something miss me?
Pierre Peron
Greenhorn
Joined: Apr 20, 2006
Posts: 11
posted
0
And how could I do with JSTL?
I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.