1

I just want to ask an opinion, which are the best practice to use html between this two option:

1) Use JSP by creating html page there

2) Generate html using Java server side by using PrintWriter

Which one are the best practice?

And if I got some situation like only to generate small line of html, is it better to create JSP page or just generate it using Java PrintWriter?

Or there are any better approach?

Thanks!

3
  • What do you mean with server side PrintWriter? Low level Socket Programming or Servlets? What's your runtime environment? What are your use cases? Which non functional requirements do you have?
    – andih
    Commented Jul 21, 2016 at 6:56
  • Like this response.getWriter().println("OK") , Java 1.8 , just for generate the form based on selected user option
    – FreezY
    Commented Jul 21, 2016 at 7:09
  • 1
    You should avoid using the Writer of a JSP directly. JSP is some kind of Template so text like "OK" should placed directly into the JSP. Accessing the writer may be interesting if you want to copy data (may be HTML) into the JSP, otherwise use the features of the (standard) tag libs and the JSP features.
    – andih
    Commented Jul 21, 2016 at 7:46

1 Answer 1

5

I would use a JSP every time. Why?

  • Low-level programming HTML is a slow and error-prone operation. I've done this in C++ on an Arduino board, and I'm glad it was a hobby project.There is no way I could charge commercial rates for the small amount of functionality I produced.
  • A JSP is very close to the HTML it emits, and so is easy to maintain.
  • There are good libraries (JSTL for one) that simplify formatting output, handling loops etc.
  • Investigate the use of scriptlet tags. These are easy to write, and allow encapsulating chunks of the JSP into a separate file. Typically these are used for things like input fields with an associated label and error reporting field. You can call the same scriptlet from multiple JSPs. In my experience, scriptlet tags have all but replaced tags written in Java.

Having said that, JSPs are just one of many web templating options.This Wikipedia article provides a good comparison of the many options.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.