Solved

display quotation marks in a text field in jsp

Posted on 2003-12-08
11
963 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
Comment Utility
You could do:

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

Expert Comment

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

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

Author Comment

by:msasikala
Comment Utility
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
Comment Utility
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
Comment Utility
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
How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 40 total points
Comment Utility
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
Comment Utility
Like the man says ;-)
0
 

Author Comment

by:msasikala
Comment Utility
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
Comment Utility
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
Comment Utility
The same &apos happened to me.

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

Expert Comment

by:Michel Plungjan
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

Suggested Solutions

A procedure for exporting installed hotfix details of remote computers using powershell
A safe way to clean winsxs folder from your windows server 2008 R2 editions
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …

743 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

18 Experts available now in Live!

Get 1:1 Help Now