0

I have one showdata.jsp which contains all the data from MySQL database in table format. The table fields are,

id,firstname,lastname 

and all fields are varchar in the database.

At each row, the table has update button to edit the row details. I am passing the id of the row from the showdata.jsp to the updatedata.jsp where updatedata.jsp has the text field for retrieving data and edit it. This is working fine for parameter(id/string) without containing special characters, but for special character strings/ ids this is not retrieving the data. URL passed on showdata.jsp is

<a href="updatedata.jsp?id=<%=request.getParameter(1)%>">Update</a>
VPK
  • 3,010
  • 1
  • 28
  • 35
Anju
  • 3
  • 5
  • no sir ...if suppose i have a string "science & technology" and i want to search a row containing this string from database then in such situation i am not able to get the required result – Anju Jan 27 '17 at 11:42
  • 1
    You can't pass `science & technology` into the argument, space are not accepted and `&` is a reserved character. But you can escape those character, this is what `` will do if you pass those values in the param part. – AxelH Jan 27 '17 at 11:48
  • ok thank you sir... and how to do it in simple jsp and not jstl – Anju Jan 27 '17 at 11:55
  • 1
    Hum, you don't ;) you should not use java scriptlet in a JSP file, using JSTL is cleaner and specificly ready to correct some issues of encoding like those. But you have the URLEncoder if need, but you would not use this – AxelH Jan 27 '17 at 11:57

1 Answers1

1

Here is a solution using JSTL

The url tag will encode the param value automaticly.

<c:url value="www.myurl.org" var="mylink">
    <c:param name="arg1" value="Science & technology" />
    <c:param name="arg2" value="Hello" />
    <c:param name="arg3" value="World" />
</c:url>
<a href="${mylink}">The link</a>

The resulting link hrefwill be :

<a href="www.myurl.org?arg1=Science+%26+technology&arg2=Hello&arg3=World">The link</a>

Note : a solution without JSTL would be to use URLEncoder.encode or URI (the last is safer) but since this is not the best solution, I would only gave the idea ;)

AxelH
  • 14,325
  • 2
  • 25
  • 55
  • okay..thank you sir...but the value i am passing is not hardcoded one.. i am receiving that value from database on one jsp and passing that to another jsp.. and i have tried for url encoder but that does not work properly.. – Anju Jan 30 '17 at 04:40
  • @Anju That's the point of using the jstl, you can easily get an argument from the name like `${my_parameter}` directly in the param value. I only gave you the solution for your encoding problem, for how to use JSTL, you can find existing post or good tutorial. But you still can use you scriplet to set the value into the param value... – AxelH Jan 30 '17 at 07:43
  • Where can i get full list of special characters and their URLencoding representation in java and ajax?? – Anju Feb 02 '17 at 05:09
  • @Anju Well, I would guess everything that is not [allowed in a URL](http://stackoverflow.com/q/1856785/4391450) for the JSTL `c:url`, but you don't need to worry, pass EVERY variable in a `c:param` value to be safe, it is the right way. In ajax you need to call a method (can't remember the name) to translate those character. (Ajax as nothing to do with java or jsp) – AxelH Feb 02 '17 at 06:42