• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Removing html tags from a string

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Guys

Is there anyway I can remove html tags from chuncks of text in JSP (preferably using JSTL).


 
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You will definitely have to describe what you are trying to do more clearly. Otherwise, the only answer I can give is not put HTML tags that you don't want there in the first place!

Are you talking about conditionally not including them based upon some server-side condition? Some client-side activity? If the earth starts revolving in the other direction? Other?

Details please.
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its server condition. Basically, the will get the HTML text from the DB and a small snippet of each record will be displayed to the user in the JSP without any HTML formatting so that the text doesn't messup the layout or take more space than it should it. I did initially do the stripping on serverside using regex but it became a problem because of the different types of data that can be retrieved from the DB I had to introduce 4 different Maps (mapping resultset to new object) and its taking up around 100+ lines of code. So instead I have decided to stick to just one object map and do the stripping in the JSP instead.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
So you have an HTML fragment of text from which you want to strip HTML tags? That's something you'll have to do with something other than JSTL.

Personally, I'd make the changes earlier such that HTML isn't stored in the DB in the first place. But if that's not possible I'd certainly make sure to cleanse the string prior to forwarding it to the JSP.

I've moved this to a general Java forum with an appropriate change in title as this is not JSP-specific.
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, if I use this method I can remove around 60 or so lines of code. I know how to remove html from strings, I just wanted to know if its possible in JSPs usings JSTL.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I suppose using the string manipulation functions of the JSTL fn function you might be able to figure out a way do it, but ti'd be messy and cumbersome and something not at all suited to doing in a JSP page. Cleanse the string perior to sending to the JSP in the first place.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
P.S. If you must do this from the JSP (which I do not recommend), you could also write a custom action or custom EL function to do this.
 
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure, but as you're hammering on using JSTL, I guess that you actually meant "escape" instead of "remove". If this is true, you can just use JSTL's c:out for that.
 
Bear Bibeault
Sheriff
Posts: 67747
173
Mac Mac OS X IntelliJ IDE jQuery TypeScript Java iOS
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Bauke Scholtz wrote:I guess that you actually meant "escape" instead of "remove".

Hmm, I never would've thought of that.

Which is it?
 
shaf maff
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I will try that
 
Marshal
Posts: 79240
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please do a search. Similar questions have come up in the last few weeks, and you might find out something useful from them.
 
reply
    Bookmark Topic Watch Topic
  • New Topic