Solved

display quotation marks in a text field in jsp

Posted on 2003-12-08
11
975 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
Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Export JSON response data to Excel in IE9 3 81
session bean client machine 9 63
immutable object concept 5 127
servlet example 17 53
This article describes my battle tested process for setting up delegation. I use this process anywhere that I need to setup delegation. In the article I will show how it applies to Active Directory
Many businesses neglect disaster recovery and treat it as an after-thought. I can tell you first hand that data will be lost, hard drives die, servers will be hacked, and careless (or malicious) employees can ruin your data.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

830 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