| Author |
Control a String Value Display
|
Steve Dyke
Ranch Hand
Joined: Nov 16, 2004
Posts: 1260
|
|
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
Joined: Jan 10, 2002
Posts: 56538
|
|
& 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 <c ut> to display the value.
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Steve Dyke
Ranch Hand
Joined: Nov 16, 2004
Posts: 1260
|
|
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
Joined: Jan 10, 2002
Posts: 56538
|
|
When this displays in my JSP field ...
So what does the above mean then?
|
 |
Steve Dyke
Ranch Hand
Joined: Nov 16, 2004
Posts: 1260
|
|
|
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
Joined: Jan 10, 2002
Posts: 56538
|
|
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
Joined: Nov 16, 2004
Posts: 1260
|
|
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
Joined: Dec 11, 2004
Posts: 13410
|
|
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.
|
Java API J2EE API Servlet Spec JSP Spec How to ask a question... Simple Servlet Examples jsonf
|
 |
Steve Dyke
Ranch Hand
Joined: Nov 16, 2004
Posts: 1260
|
|
Thanks, javascript.escape() worked.
|
 |
 |
|
|
subject: Control a String Value Display
|
|
|