I used to think that JavaServer Pages (JSPs) were an abomination . They are completely unstructured, often abused, difficult to debug, and widely used. I now believe they can be used effectively if they are used cautiously and conservatively. They are most effective for pages that are comprised of large amounts of HTML information and/or pages that include lots of repeated constructs, such as forms that use tables for formatting labels and fields. They should not be used as a replacement for servlets or for embedding lots of Java code within a page. Thus, within a Model -View - Controller architecture, Servlets are appropriately used as the controller whereas JSPs can be used as the view components.
A Servlet (or a "plain old Java class" ("POJO") called from a Servlet) can use Java language constructs to generate HTML. Used in this way, Servlets or POJOs usually consist of (lots of) Java code with (some) HTML code embedded in their Java constructs, such as System.out.println statements.
By contrast, JavaServer Pages (JSPs) are basically HTML pages with (a little bit of) Java code embedded in them. Or, at least, they should include only a little bit of Java code. If they include a lot of Java, some construct other than straight JSPs should be used. (See the discussion of JSP Taglibs, below, for an appropriate alternative.)
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 View components. A general overview of JSP syntax and the major components it supports is also provided. The lesson ends with a discussion of JSP Taglibs.
Several useful references include an O'Reilly's HTML Meets Java: JSP tutorial, Marty Hall's Servlet and JavaServer Pages 1.0: A Tutorial, and an anonymous JSP Tutorial. Their are, of course, numerous other tutorials available on the net, which can easily be referenced using Google..
JSP Lifecycle
A JSP page begins as a file composed largely of HTML code located in a directory where conventional HTML pages may be accessed, but it is transformed by the server into a special servlet form and thereafter accessed as a servlet.
Hello World
This JSP illustrates a minimal scriplet in which a String variable is instantiated in statements prior to the BODY 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.
JSP Taglibs
Taglibs are the preferred means for including Java code in JSP pages. They localize the code in a set of Tag Handler classes and, thus, make the tags more readable and more reusable.