• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Links

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 67746
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28177
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And how could I do with JSTL?
reply
    Bookmark Topic Watch Topic
  • New Topic