aspose file tools*
The moose likes JSP and the fly likes Links Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSP
Bookmark "Links" Watch "Links" New topic
Author

Links

Pierre Peron
Greenhorn

Joined: Jun 15, 2006
Posts: 3
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

Joined: Jan 10, 2002
Posts: 61658
    
  67

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Pierre Peron
Greenhorn

Joined: Jun 15, 2006
Posts: 3
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

Joined: Jan 10, 2002
Posts: 61658
    
  67

You only encode the parameters, not the whole string. What's happened is that you've encoded the ? too.
Pierre Peron
Greenhorn

Joined: Jun 15, 2006
Posts: 3
What do you mean? I don't understand. Could you give me the code you want me to test please?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18909
    
    8

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
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

Joined: Apr 20, 2006
Posts: 11
And how could I do with JSTL?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Links