Solved

String Problem

Posted on 2006-07-06
53
274 Views
Last Modified: 2010-03-31
Hi,

If I pass the string like
<b>TEST</b><i>Italics</i>

to the JSP. How should I use to display it as HTML format? I was trying but no luck - maybe I need some coffee now.

David
0
Comment
Question by:suprapto45
  • 19
  • 18
  • 15
  • +1
53 Comments
 
LVL 92

Expert Comment

by:objects
Comment Utility
what do you mean by html format exactly?
0
 
LVL 2

Expert Comment

by:suthagarank
Comment Utility
Try this,

<b>TEST</b>
<i>Italics</i>
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
Sorry for not being clear.

I am passing the string as &lt;b&gt;TEST&lt;/b&gt;&lt;i&gt;Italics&lt;/i&gt;

Now I want it to be displayed with the HTML format correctly so it should print out TEST with the bold as well as Italics with italic words. Currently, it displayes
<b>TEST</b>
<i>Italics</i>

in my JSP.

Thanks
David
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
How are you passing it?
0
 
LVL 92

Accepted Solution

by:
objects earned 200 total points
Comment Utility
you'll need to use replaceAll() before inserting it

s = s.replaceAll("&gt;", ">").replaceAll("&lt;", "<");
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
then you can insert it

<%= s %>
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
Is there any better way of doing that? Like JSTL perhaps?

The way I passed it is simply using the request object.

David
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
not really, at some point you need to replace those strings
you could use a tag for it, but that would just mean the tag would do the replaceAll().
ie. replaceAll() is what you need, where that is implemented is up to you :)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>The way I passed it is simply using the request object.

Use URLEncoder\Decoder, then you won't need replacements
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
Thanks object for the links.

CEHJ, do you mean to encode or decode it first before sending it to JSP?

David
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 50 total points
Comment Utility
Yes, encode the parameter before sending. This is the correct way to send such parameters, in fact.

Decoding it will give you the original html
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> Yes, encode the parameter before sending. This is the correct way to send such parameters, in fact.
> Decoding it will give you the original html

thats incorrect :)


0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
Ok,

Thanks. objects, if CEHJ is incorrect, does it mean that we can only use replaceAll to do it?

David
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> Thanks. objects, if CEHJ is incorrect, does it mean that we can only use replaceAll to do it?

thats right or a library that does it.
I'd suggest adding commons to your webapp and use the methods it provides. Lots of other useful stuff in there as well :)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>if CEHJ is incorrect

Are you not curious as to *why* i'm 'incorrect'? ;-)
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
>>"Are you not curious as to *why* i'm 'incorrect'? ;-)"
That's why I am asking is that the only solution to objects and I assume that you are still following this thread and can give me the explanation ;)

David
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
The reponsibility for saying why it's 'incorrect' i think rests with the person who baldly stated it to be ;-)
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
You can simply test it of course:

<%=URLDecoder.decode(req.getParameter("html"), "UTF-8")%>
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
I'd actually be more interested in why he thinks url encoding has anything to do with this question :) Its completely unrelated, how is encoding a string and then decoding it going to change the contents of the string. It won't, the string will still be the same as it was originally at which point you'll need to do as I suggested.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
In case there's any doubt, of course you would encode the String you wanted using URLEncoder, i.e. the actual html
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
I agree that object's suggestion is valid and should be working.

Okay, let's straight this thing out. I am going to test on what CEHJ is suggested. If it works, then it means that CEHJ's answer is also valid.

David
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>I am going to test on what CEHJ is suggested.

Don't forget to encode the actual html, not

>>&lt;b&gt;TEST&lt;/b&gt;&lt;i&gt;Italics&lt;/i&gt;

To help you decide which way is better, check the length of the resulting query string
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
Okay,

CEHJ is also valid but it is an alternative way of doing it. I think that objects solution is more accurate based on the original question that I have.

So I am going to split the points to both of you with objects receiving more points.

What two of you think?

David
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> CEHJ is also valid but it is an alternative way of doing it.

No its not :) it totally unrelated
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> I think that objects solution is more accurate based on the original question that I have.

CEHJ suggestion has nothing to do with the original question in fact.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>
I think that objects solution is more accurate based on the original question that I have.
>>

I'd be interested to know why it's more accurate? It's actually less accurate for the following reasons:

1. it's not a standard way to pass parameters that need to be escaped
2 it occupies more space in the query string
3. if you ever pass additional parameters, you may need to use *two* encryption s hemes
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
objects,

I assume that you said that it is "unrelated" because it does not solve the problem originally. Am I right? However, in my opinion, CEHJ provides an alternative way of doing it albeit it does not solve the problem that I have.

In my opinion, wouldn't that be good to have a post that have the direct answer as well as an alternative so that people can get benefit most of it?

Please let me know what you think objects.

0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
you may need to use *two* encryption s hemes=you may need to use *two* encoding schemes
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
CEHJ,

>>"1. it's not a standard way to pass parameters that need to be escaped"
It may be true but currently, this is the String that is passed by the third party system so there is nothing I can do. So I assume that objects hits the point correctly. But I am not saying that you are wrong! Your solution may be practical if the third party would like to implement it as well in which they don't

What do you think?
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>it does not solve the problem that I have

Why doesn't it? If it doesn't then it's useless ;-)
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
>>"Why doesn't it? If it doesn't then it's useless ;-)"
The reason is in my previous comment CEHJ ;)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
Its not an alternative, the encoding/decoding of the parameters is already handled for you. Otherwise you would not be recieving them.

            System.out.println(URLDecoder.decode("&lt;b&gt;TEST&lt;/b&gt;&lt;i&gt;Italics&lt;/i&gt;", "UTF8"));

output:

&lt;b&gt;TEST&lt;/b&gt;&lt;i&gt;Italics&lt;/i&gt;

Wow, thats useful :-D
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>this is the String that is passed by the third party system so there is nothing I can do.

Well if it's not *you* that's passing the string, then you can't alter it, but that's not what you said ;-)

>>I am passing the string as &lt;b&gt;TEST&lt;/b&gt;&lt;i&gt;Italics&lt;/i&gt;
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
If you'd said

'I am receiving a string as &lt;b&gt;TEST&lt;/b&gt;&lt;i&gt;Italics&lt;/i&gt;'

then i wouldn't have wasted my time ;-)
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
>>"Well if it's not *you* that's passing the string, then you can't alter it, but that's not what you said ;-)"
Maybe I should mention it earlier.

>>"If I pass the string like"
This is what I am saying.

Apologized for that.

objects, CEHJ solution will work if you have encoded it previously before passing it.

I am not going to go further about this and I will stick with objects get more points and CEHJ get less points but it is a split.

What you think?
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
its really irrelevant where the string comes from , at some point you have to change it as I suggested which is what you were asking :)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> CEHJ solution will work if you have encoded it previously before passing it.

No it won't :-D
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
>>"at some point you have to change it as I suggested which is what you were asking :)"
That is why I think that you deserve more points
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
In fact it has already been encoded before sending, and decoded when recieved as I mentioned above.
ie. CEHJ is suggesting something that is already occurring.
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
>>"CEHJ is suggesting something that is already occurring."
It is mainly my fault as CEHJ wouldn't waste much time if I said that I received it from third party which I can't control. But if people are reading this post, I think that they will get a benefit by looking at what CEHJ's suggested. Don;t you think so?

So objects, what are you proposing?
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
That is just my judgement. If any of two of you think that it should not be. Let me know, I just want everybody to be happy and not to be offended :)

David
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> It is mainly my fault as CEHJ wouldn't waste much time if I said that I received it from third party which I can't control.

Where it comes from doesn't make any difference. They are already encoding the parameter as required, they cannot avoid doing that.

> I think that they will get a benefit by looking at what CEHJ's suggested. Don;t you think so?

No, because what he is suggesting is already being done. It has absolutely nothing to do with your problem.
Suggesting to do it again is a pointless waste of time.
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
Ok,

Let's ask Venabili to be the judge. I will agree to what Venabili suggests. I hope that everyone is happy about it.

Before that, I would like to thank you for all of you who has responded to my questions.

Thanks
David
0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
objects, CEHJ,

I am not going to be available soon. I will be back tonight
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
no need to waster her time. I don't mind what you do really, just want you to understand better as CEHJ's comment's are actually quite misleading.
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
just be aware that your parameter is already being encoded and decoded :)
it has nothing to do with the problem at hand.
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
>>>>"at some point you have to change it as I suggested which is what you were asking :)"

My previous comments therefore apply to how *they* are passing the parameter. If they had passed it as html you wouldn't have to change it
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> If they had passed it as html you wouldn't have to change itIf they had passed it as html you wouldn't have to change it

rotfl, well thats stating the extremely obvious :)  And if that was the case the question wouldn't need to be asked, so again not at all relevant.

0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
Thanks guys,

I hope that everyone is happy about this.

David
0
 
LVL 86

Expert Comment

by:CEHJ
Comment Utility
:-)
0
 
LVL 92

Expert Comment

by:objects
Comment Utility
> Yes, encode the parameter before sending. This is the correct way to send such parameters, in fact.
> Decoding it will give you the original html

fine by me, though for the benefit of others reading the question that comment is incorrect.
See my earlier comments for an explanation :)

0
 
LVL 16

Author Comment

by:suprapto45
Comment Utility
Thanks objects and CEHJ.

David
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

772 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

10 Experts available now in Live!

Get 1:1 Help Now