Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Links

 
Pierre Peron
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64702
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Pierre Peron
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your answer. I already thought about testing with the URLEncoder.class :

<% String url = URLEncoder.encode("test.jsp?code=" + value, "UTF-8"); %>

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).
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64702
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You only encode the parameters, not the whole string. What's happened is that you've encoded the ? too.
 
Pierre Peron
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean? I don't understand. Could you give me the code you want me to test please?
 
Paul Clapham
Sheriff
Pie
Posts: 20958
31
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 html:
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<title>test</title>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
</head>
<body text="#000000" link="#000000" vlink="#000000" alink="#000000">

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
Posts: 11
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And how could I do with JSTL?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic