File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes how to handle single quote? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "how to handle single quote?" Watch "how to handle single quote?" New topic
Author

how to handle single quote?

Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
here is the code..
iam having the data with single quote as value for the list box..
while retrieving the data is truncated at the single quote position, can any suggest me the ways of solving this problem..

here is the code..while retrieving list box value, iam getting only Father instead of Father's Name


<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<%
if (request.getParameter("submit")!=null)
{
out.println(request.getParameter("s"));
}
%>
</HEAD>
<%String str="Father's Name";
%>
<BODY>
<form >
<SELECT NAME="s">
<option value='<%=str%>'><%=str%></option>
</SELECT>
<INPUT TYPE="submit" name="submit" value="submit">
</form>
</BODY>
</HTML>


Abhishek
Qussay Najjar
Ranch Hand

Joined: Jan 18, 2008
Posts: 53
Originally posted by Abhishek Reddy Chepyala:
here is the code..
iam having the data with single quote as value for the list box..
while retrieving the data is truncated at the single quote position, can any suggest me the ways of solving this problem..

here is the code..while retrieving list box value, iam getting only Father instead of Father's Name


<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<%
if (request.getParameter("submit")!=null)
{
out.println(request.getParameter("s"));
}
%>
</HEAD>
<%String str="Father's Name";
%>
<BODY>
<form >
<SELECT NAME="s">
<option value='<%=str%>'><%=str%></option>
</SELECT>
<INPUT TYPE="submit" name="submit" value="submit">
</form>
</BODY>
</HTML>


You can use this:

it will work..


Qussay NAJJAR
Qussay Najjar
Ranch Hand

Joined: Jan 18, 2008
Posts: 53
Originally posted by Qussay Najjar:


You can use this:

it will work..


sorry, for my typing mistake.. it should be..
[CODE]
str = str.replace("\'","\'\'");
[CODE]
instead of
[CODE]
str = str.replace('\'','\'\'');
[CODE]
notice the double quotes("") in the replace parameters.
Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
thnak you Qussay Najjar for the reply

i have tried your code, but it is not working.
have you tried ?
Qussay Najjar
Ranch Hand

Joined: Jan 18, 2008
Posts: 53
Originally posted by Abhishek Reddy Chepyala:
thnak you Qussay Najjar for the reply

i have tried your code, but it is not working.
have you tried ?


Yes, I tried the second one, and it's working with oracle DB.
Abhishek Reddy
Ranch Hand

Joined: Mar 28, 2006
Posts: 259
thanks for the reply again..
i have tried, but it is not working, i don't know where i went wrong.
can you look at the following code and tell me where i went wrong?
here is the code ..

<HTML>
<HEAD>
<TITLE> New Document </TITLE>
<%
if (request.getParameter("submit")!=null)
{
out.println("<div align=right><a href='test.jsp'>back</a></div>");
out.println(request.getParameter("s"));
}
%>
</HEAD>
<%String str="Father's Name";
str=str.replace("\'","\'\'");
%>
<BODY>

<form >
<SELECT NAME="s">
<option value='<%=str%>'><%=str%></option>
</SELECT>
<INPUT TYPE="submit" name="submit" value="submit">
</form>
</BODY>
</HTML>
Qussay Najjar
Ranch Hand

Joined: Jan 18, 2008
Posts: 53
Okay my friend,,
If your page is just like that, you don't have to do that
"replace" technique (this one you use only when you're saving to DB, because
Database quires won't work with single quotes.
but in your case, you went wrong in this line.




just change the single quotes for the value to double ones, like this:



and later on if you needed to save this name to DB, you change it in the servlet that is receiving the request from this jsp page..
Got it...?
 
jQuery in Action, 2nd edition
 
subject: how to handle single quote?
 
Similar Threads
Please solve the problem.
pass the value to next form
error showing while retrieving the data from drop down list
How to Validate more then one selection list?
Error in request.getParameter