0%
JavaServer Pages (JSP)

JSP

Generate HTML without drowning the presentation in Java code.

15-20 min

JavaServer Pages (JSP)

Take the servlet from the previous lesson and imagine it has to produce a catalog page: a header, a menu, a table of thirty products, pagination, a footer. Written with a PrintWriter, that comes out as three hundred lines of out.println("<tr><td>") where every quotation mark in the HTML has to be escaped, where the indentation of the generated page has nothing to do with that of the Java code, and where a front-end integrator who wants to change a color has to recompile the application.

That observation is what gave birth to JSP: in a web page, there is a lot of fixed HTML and a little variable data. Writing the HTML from Java inverts that ratio. JavaServer Pages, renamed Jakarta Pages since Jakarta EE 9, turns the problem around: you write an HTML file and insert markers for the variable parts.

The mechanism, often misunderstood, is simple: a JSP is a servlet. On the first call, the container translates the .jsp file into Java code, compiles it, instantiates it, and runs it. You are not writing an interpreted template language: you are writing the source of a servlet in a more convenient form.

Comments

Comments are powered by GitHub Discussions

Sign in with GitHub to join the discussion

Link copied!