wood burning stoves 2.0*
The moose likes Servlets and the fly likes Passing # to Servlet as part of a parameter. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Servlets
Bookmark "Passing # to Servlet as part of a parameter." Watch "Passing # to Servlet as part of a parameter." New topic
Author

Passing # to Servlet as part of a parameter.

Suku Kana
Greenhorn

Joined: Mar 14, 2002
Posts: 3
Hi Friends,
We are faced with a problem. We have a servlet which takes several parameters from the UI. Sometimes, the value of a parameter needs to contain special characters like # and &.
To my knowledge, # and & have different meanings in URL. Is it possible to send # and & as part of parameter values to Servlet? Is there any escape character that can be sent instead of # and &, like %20 for blank space?
Regards,
suku.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61092
    
  66

Check out java.net.URLEncoder for server-side URL encoding, and the Javascript escape() method for client-side encoding.
hth,
bear


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
gautam shah
Ranch Hand

Joined: Oct 29, 2000
Posts: 72
Originally posted by Suku Kana:
Hi Friends,
We are faced with a problem. We have a servlet which takes several parameters from the UI. Sometimes, the value of a parameter needs to contain special characters like # and &.
To my knowledge, # and & have different meanings in URL. Is it possible to send # and & as part of parameter values to Servlet? Is there any escape character that can be sent instead of # and &, like %20 for blank space?
Regards,
suku.

u can send any character as a part of ur request in the pattern "%XX" where XX is the hexadecimal equilant to the ascii of that character u want to sent.
let suppose asciicode for & = 64 then u can send %40 instead of &.
lokesh reddy
Ranch Hand

Joined: Sep 15, 2000
Posts: 66
Hi the following are the escape characters for & and #
& = %26
# = %23
Hope u find this info useful.
Loke.
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

Forget all that if you are using forms.

It will automatically encode and decode special characters for you.

If I type in the characters $%&< into a form textbox named "specialChars" and submit the form, the following code:This will produce $%&< in the servlet log file (from System.out) and the webpage that gets generated by this servlet will contain <h2>special chars:</h2>$%&<

The address bar of your browser should contain something like:
http://localhost:8080/examples/encTest?specialChars=%24%25%26%3C&submitIt=Go

So only if you are building the query string manually would you need to worry about encoding. I've only just tried this myself (tomcat 4.x and IE5.5), so if another container or browser behaves differently, someone please correct me.
 
 
subject: Passing # to Servlet as part of a parameter.