File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Removing html tags from a string Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Removing html tags from a string" Watch "Removing html tags from a string" New topic
Author

Removing html tags from a string

shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
Hi Guys

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


Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
shaf maff
Ranch Hand

Joined: Sep 07, 2008
Posts: 180
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

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

Joined: Sep 07, 2008
Posts: 180
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

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.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
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
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61216
    
  66

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

Joined: Sep 07, 2008
Posts: 180
I will try that
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38851
    
  23
Please do a search. Similar questions have come up in the last few weeks, and you might find out something useful from them.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Removing html tags from a string