JavaServer Pages are an abomination . . . at least in my opinion. They are completely unstructured, often abused, difficult to debug, and widely used.
A Servlet is Java code that can use Java language constructs (e.g., System.out.println) to generate HTML, but it is laborious to generate a lot of it this way. A JavaServer Page (JSP), is the converse; that is, a page of HTML that can include Java code. The argument is made that when the output to be returned to a user is largely HTML (and includes a substantial amount of it), then a JSP is often easier to write than the corresponding Servlet. But, since a JSP page is fundamentally HTML text, it is not subjected to any of the error checking provided during compiling nor are there comparable debugging tools for JSP.
The one use that I consider legitimate is when the Java code is kept to a bare minimum and when they are used to provide the view component in a model - view - controller architecture. In a Web application, the controller function is usually provided by a Servlet; consequently, JSPs become an adjunct to Servlets rather than a replacement for them.
In the discussion below, a minimal Hello, World JSP is discussed. After that a set of JSPs and a servlet are provided illustrating their use as the view component. Finally, a general overview of JSP syntax and the major components its support is provided.
Several useful references include an O'Reilly's discussion of JavaServer Pages Using a Servlet and their HTML Meets Java: JSP tutorial. Their are, of course, numerous other tutorials available on the net.
Hello World
This JSP illustrates a minimal scriplet in which a String variable is instantiated in the HTML HEAD and then accessed within the HTML BODY.
JSP View
This discussion illustrates the interaction between a servlet controller and a JSP view object within a model - view - controller architecture..
JSP Syntax
Actions are JSP tags that transfer control to other server objects or perform operations on other objects. They may also generate output. They are another means of building view objects within an m - v - c architecture.