Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Removing html tags from a string

 
Ranch Hand
Posts: 180
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys

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


 
Marshal
Posts: 67449
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Posts: 67449
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Posts: 67449
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
Marshal
Posts: 67449
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • 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
Marshal
Posts: 67449
173
Mac Mac OS X IntelliJ IDE jQuery Java
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
I will try that
 
Marshal
Posts: 70257
282
  • Mark post as helpful
  • send pies
  • 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.
 
I love a good mentalist. And so does this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
    Bookmark Topic Watch Topic
  • New Topic