Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Control a String Value Display

 
Steve Dyke
Ranch Hand
Posts: 1629
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I set a session attribute up then use it in expression language to display on my JSP. The string value is like this: "R&D1040 Test Article" When this displays in my JSP field all I see is "R". I am sure it tis the "&" causing it to fail. How can I fix this. I have tried:



But it still strips the string to only "R"
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
& is the character that introduces an HTML entity. It must be properly encoded to appear on an HTML page.

The easiest way to do this is to use <cut> to display the value.
 
Steve Dyke
Ranch Hand
Posts: 1629
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am not using the string value in my HTML. I use javascript and the syntax:

var v1 = "${stringvalue}"
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When this displays in my JSP field ...
So what does the above mean then?
 
Steve Dyke
Ranch Hand
Posts: 1629
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a JSP with a list of program descriptions which display(even the ones with "&" in them). Usually the program description are plain text with no special characters. However, a couple of them are unique. Example: "R&DTest". Also on the form has a table to hold child details of the selected program. So when the user makes a selection the form posts a request to display the table values. The request sends selected program description as a parameter. I set the parameter as an attribute. Now at this point the user cannot tell from the list what program was selected. So I added an input box that displays the program that was selected previously, the attribute, in the onload event using the syntax "${progdesc}".
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64990
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So how is that:
not using the string value in my HTML

?

If the string appears anywhere on the display, it's being used in the HTML and must be properly encoded.
[ May 22, 2008: Message edited by: Bear Bibeault ]
 
Steve Dyke
Ranch Hand
Posts: 1629
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
After doing some more testing the string breaks when the request is made here:



The alert shows entire string and it is placed in my input box okay. However, if I check the parameter value that is sent to the servlet the string changes from "R&DTest" to "R".

What encoding do I use to keep this from happening? Is this something I do at the HTTPRequest side.

Once again I thank you for all the help.
 
Ben Souther
Sheriff
Posts: 13411
Firefox Browser Redhat VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To encode it on the server side, before sending it to the browser, use
java.net.URLEncoder.

To encode it on the client side, look at Javascripts escape() function.
 
Steve Dyke
Ranch Hand
Posts: 1629
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,

javascript.escape() worked.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic