Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 963143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T01:37:45+00:00 2026-05-16T01:37:45+00:00

I need to tidy user input in a web application so that I remove

  • 0

I need to tidy user input in a web application so that I remove certain HTML-tags and encode < to &gt etc.

I’ve made a couple of simple util methods that strips the HTML, but I find myself adding these EVERYWHERE in my application.

Is there a smarter way to tidy the user input? E.g. in the binding process, or as a filter somehow?

I’ve seen JTidy that can act as a servlet filter, but I’m not sure that this is what I want because I need to clean user input, not output of my JSP’s.

From JTidy’s homepage:

It can be used as a tool for cleaning up malformed and faulty HTML generated by your dynamic web application.

It can Validate HTML without changing the output and generate warnings for each page so you could identify JSP or Servlet that need to be fixed.

It can save you hours of time. The more HTML you write in JSP or Servlets, the more time you will save. Don’t waste time manually looking for problems, figuring out why your HTML doesn’t display like it should.

In addition to JTidy validation you could submit dynamically generated pages to online HTML validators for example W3C Markup Validation Service, WAVE Accessibility Tool or WDG HTML Validator even if you are behind the firewall.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T01:37:46+00:00Added an answer on May 16, 2026 at 1:37 am

    I find myself adding these EVERYWHERE in my application.

    Really? It’s unusual to have many user inputs that accept HTML. Most inputs should be plain text, so that when the user types < they literally get a less-than sign, not a (potentially-tidied/filtered-out) tag. This requires HTML-encoding at the output stage. Typically you’d get that from the <c:out> tag.

    (Old-school JSP before JSTL, lamentably, provided no HTML-encoder, so if for some reason that’s what you’re working with you would have to provide your own HTML-encoding method built out of string replacments, or use one of the many third-party tools that contain one.)

    For the usually-few-if-any ‘rich text’ fields that are deliberately meant to accept user-supplied HTML, you should be filtering them strongly to prevent JavaScript injection from the markup. This is a difficult job! A “couple of simple util methods that strips the HTML” are highly unlikely to do it correctly and securely.

    The proper way to do this is to parse the input HTML into a DOM; walk over it checking that only known-safe element and attribute names are used; then serialise it back to well-formed [X]HTML. There are a number of tools that can do this and yes, jTidy is one. You would use the method Tidy.parseDOM on the input field value, remove unwanted items from the resulting DOM with removeChild and removeAttribute, then reserialise using pprint.

    A good alternative to HTML-based rich text is to give the user a simpler form of textual markup that you can then convert to known-safe HTML tags. Like this SO text box I’m typing into now.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a third-party app that creates HTML-based reports that I need to display.
I have a large number of HTML files that I need to process with
Possible Duplicate: Do I really need to encode '&' as ' &amp;' ? I
Need help with a query that I wrote: I have three tables Company id
Currently HTML Tidy is changing any empty HTML tag and combining them into one,
I need to transform HTML into XHTML 1.1. I'm doing it in a Java
I am trying to tidy up my code so that I can easily reuse
I'm building an app in Symfony2 that receives a HTML string via an API,
I need to tidy up an array. And gather all similar information together. This
I'm putting some page content (which has been run through Tidy, but doesn't need

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.