• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Passing # to Servlet as part of a parameter.

 
Suku Kana
Greenhorn
Posts: 3
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 64173
83
IntelliJ IDE Java jQuery Mac Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out java.net.URLEncoder for server-side URL encoding, and the Javascript escape() method for client-side encoding.
hth,
bear
 
gautam shah
Ranch Hand
Posts: 72
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi the following are the escape characters for & and #
& = %26
# = %23
Hope u find this info useful.
Loke.
 
Mike Curwen
Ranch Hand
Posts: 3695
IntelliJ IDE Java Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic