11.3. Creating JSP Files

Problem

You want to write JSP files and view them in a web browser.

Solution

Create and edit JSP files in Eclipse.

Discussion

As with any other project, you can use Eclipse to write and store .jsp files that can be used in servlet containers. At its simplest, Eclipse will enable you to create these files, and you can copy them to the correct directory in your web server installation.

JSP files are can hold Java code in scriptlet, declaration, and expression elements. Of these, scriptlets are the most general and can contain general Java code. You enclose a scriptlet between the markup <% and %>, as shown in Example 11-1. In this case, we’re using the built-in out object’s println method to display text in the JSP page’s output in the browser, This JSP is functional..

Example 11-1. A sample JSP

<HTML>
    <HEAD>
        <TITLE>JSP Sample</TITLE>
    </HEAD>

    <BODY>
        <H1>JSP Sample</H1>
        <% out.println("This JSP is functional."); %>
    </BODY>
</HTML>

An easy way to create this JSP file is to enter it into Eclipse, as shown in Figure 11-2, where we’ve created a new project, JSP, and a new file, greeting.jsp, to hold the JSP code. There’s no syntax checking going on here; Eclipse is just using its standard, default editor.

Tip

If you do want to check syntax of JSP documents, give the XML editor named XML Buddy a try. It’s available for free at http://www.xmlbuddy.com.

Editing JSP code

Figure 11-2. Editing JSP ...

Get Eclipse Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.