?
Solved

display quotation marks in a text field in jsp

Posted on 2003-12-08
11
Medium Priority
?
987 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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
 
LVL 19

Accepted Solution

by:
Kuldeepchaturvedi earned 120 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

If you're a modern-day technology professional, you may be wondering if certifications are really necessary. They are. Here's why.
"Any files you do not have backed up in at least two [other] places are files you do not care about."
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses

765 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