Using a JSP File

Let’s learn how to generate dynamic content using a JSP file instead of writing static HTML content in a servlet.

Writing HTML in Java is not good practice. We have to use the println() method for every line of HTML code. A better approach is to use Java Server Pages (JSP) to write HTML content to the browser. A JSP is also compiled as a servlet so there is no performance gain in using JSPs. Rather, it is ease to use and able to send dynamic content to the browser.

Creating a JSP file

Typically, JSPs reside in the views folder in src/main/webapp/WEB-INF. Right now, we only have webapp in the project hierarchy. Inside the webapp folder, we will create a folder WEB-INF which will have a subfolder, views.

In the views folder, we will create a JSP file and call it welcome.jsp. By default, this generates an HTML 5 JSP. This file provides the basic HTML structure of a webpage.

Get hands-on with 1400+ tech skills courses.