0

My java server works as follows:

http://locahost:5555/?search="java"

The above link would work fine. However, if I ever want to use "#" as part of search string, it all goes wrong. For example:

http://locahost:5555/?search="c#"

For some reason everything after "#" gets ignored. If I use the decoded version of "#" it works fine again. For example:

http://locahost:5555/?search="c%23"

The system should be used by people that don't understand url encoding so they would never put %23 instead of #. Is there anyway around it?

tashuhka
  • 5,028
  • 4
  • 45
  • 64
nafas
  • 5,283
  • 3
  • 29
  • 57
  • Will that people write the url and its parameters directly in the address bar? If not, how is the url built? – BackSlash Aug 01 '14 at 08:47
  • 2
    People not understanding URL encoding shouldn't use the browser address bar to search for things. They should fill an input field in a form, then submit the form. The form will encode URL parameters properly. – JB Nizet Aug 01 '14 at 08:48
  • @JBNizet, its just a test server, but at work is getting quite popular and they use it on daily bases. so I was hoping to get it working right. – nafas Aug 01 '14 at 08:50
  • The server being a test server doesn't prevent you from providing a form to fill and submit. Where's the problem? – JB Nizet Aug 01 '14 at 08:51
  • if at work that kind of searching gets popular they should know about encoding those special characters – Ker p pag Aug 01 '14 at 08:53
  • 1
    @JBNizet my laziness has no boundaries. :D – nafas Aug 01 '14 at 08:53
  • @Kerppag problem is that search can be quite long. sometimes 300 chars or so. – nafas Aug 01 '14 at 08:54
  • 1
    you could refer to my answer below. it is just a few lines of code – Ker p pag Aug 01 '14 at 08:54

3 Answers3

5

Other than encoding it there is no way around it. More over the string after # treats as the location of the URL.

String after # will not be passed to the server through GET parameters. Use POST method instead.

https://developer.mozilla.org/en-US/docs/Web/API/Window.location

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
1

the user supposedly should not access the url directly so if they put "c#" in the url there would be no process on the other hand you could use

<form action="yourcontroller" method="post">

   <input type="text" name="txtSearch" />

   <input type="submit" value="search"/>

</form> 

with this, it will take care of the special characters like "#" you mentioned.

don't forget to catch the parameter in your controller

request.getParamter("txtSearch");
Ker p pag
  • 1,568
  • 12
  • 25
0

It is in the browser. The server never gets a request with the hashtag (#) symbol, just up to the symbol.

A javascript workaround is probably a bad idea.

Ragnar123
  • 5,174
  • 4
  • 24
  • 34