Solved

display quotation marks in a text field in jsp

Posted on 2003-12-08
11
967 Views
Last Modified: 2007-12-19
Hi,
  Iam getting data from data base and displaying it in a text field in jsp.but the data is getting truncated whenever a quotation mark (") is encountered.My code is
<input type="text" name="txt_Description" class="textBox100" value=<%=itemRec[2]%>      >

suppose  itemRec[2] = test"quot"only, this is displayed as test"quot" and the other part is getting truncated.can any one pls help me this is very urgent
0
Comment
Question by:msasikala
  • 4
  • 3
  • 2
  • +2
11 Comments
 
LVL 15

Expert Comment

by:jimmack
ID: 9895800
You could do:

value="<%= itemRec[2].replaceAll("\"", "&quot;") %>
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9895805
Sorry, missed the closing quote of my own there ;-)

value="<%= itemRec[2].replaceAll("\"", "&quot;") %>"
0
 

Author Comment

by:msasikala
ID: 9895895
Hi,
  I tried this but I'm getting the following error

cannot resolve symbol
probably occurred due to an error in /GloboWorksDev/En/PurchaseOrder/Create/PRPOUpdate.jsp line 789:
class="textBox100" value="<%= itemRec[2].replaceAll("\"", "&quot;") %>" >
I think there is no replaceAll(string,string) in jsp for a string and if I'm trying with replace() it is giving error as

replace(char,char) in java.lang.String cannot be applied to (java.lang.String,java.lang.String)
probably occurred due to an error in /GloboWorksDev/En/PurchaseOrder/Create/PRPOUpdate.jsp line 789:
class="textBox100" value="<%= itemRec[2].replace("\"", "&quot;") %>" >
 
can u pls suggest the solution
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9895899
Are you using JDK1.4?  If not, then we need a different solution ;-)

If you are confident that no apostrophe's will appear in the text, you could use:

value='<%= itemRec[2] %>'
0
 

Author Comment

by:msasikala
ID: 9895931
I'm not using JDK1.4 ,I'm using JDK1.3.1 and there is quite a possibility that  I may get apostrophe's also in the text
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 40 total points
ID: 9895946
and otherwise you can use following...
value=escape(<%= itemRec[2] %>)
This convert it.....  if not than use following..
Create one class with name XMLUtils
mport java.util.*;

class XMLUtil{

      private static String escapeChar(char c){
         switch(c){
            case('<')  : return "&lt;";
            case('>')  : return "&gt;";  
            case('&')  : return "&amp;";
            case('\'') : return "&apos;";
            case('\"') : return "&quot;";                        
        }
        return null;    
      }
     
      public static String encodeChars(String string){
     
         if(string==null)
         return "null";
         int length = string.length();
         char[] characters = new char[length];
         string.getChars(0, length, characters, 0);
         StringBuffer encoded = new StringBuffer();
         String escape;
         for(int i = 0;i<length;i++){
            escape = escapeChar(characters[i]);
            if(escape == null) encoded.append(characters[i]);
               else encoded.append(escape);
         }
         return encoded.toString();
      }
               
      public static void main(String[] args){
         String test = "AP = ' QT = \" AMP = & LT = < GT = > ";
         System.out.println(encodeChars(test));
      }
}

now put this class in your classpath and then use
value='<%= xmlUtils.encodeChars(itemRec[2]) %>'
this is bound to work........  
0
 
LVL 15

Expert Comment

by:jimmack
ID: 9895997
Like the man says ;-)
0
 

Author Comment

by:msasikala
ID: 9901811
Hi,
  this is working fine for all the charactrers except for apostrophe's .whenever an apostrophe is encountered it is displayed as &apos;

Ex: de'm'o is displayed as de&apos;m&apos;o

how shld this be handled?
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
ID: 9901940
That is strange!! If every other character is being converted properly, then this is also suppose to work.......
I will do some testing on my own box and will let you know.......
0
 

Expert Comment

by:JohnTirone
ID: 11144605
The same &apos happened to me.

Instead of using &apos;, try using &#39;
0
 
LVL 75

Expert Comment

by:Michel Plungjan
ID: 13542775
1. &apos; is not recognised in IE
2. &#39; IS recognised but in UTF-8 pages converted straight back to single quote, messing up my javascript.
Any ideas?
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many Password Managers (PM) out there to choose from. PM's can help with your password habits and routines, but they should not be a crutch you rely on too heavily. I also have an article for company/enterprise PM's.
An analysis of the phishing scam that has been affecting Google users, along with steps to take for protection, as well as what to do if you receive one of the emails.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

867 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now