Recipe 11.3 Creating JSP Files

11.3.1 Problem

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

11.3.2 Solution

Create and edit JSP files in Eclipse.

11.3.3 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

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.

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.

Figure 11-2. Editing JSP code

The next step is to install greeting.jsp in your installation of Tomcat; in this case, just copy that file to the directory we're going to use for the examples in this chapter, webapps\ch11 .

Now restart Tomcat, open a web browser, and navigate to http://localhost:8080/Ch11/Ch11_01.jsp . You should see the results that appear in Figure 11-3. Internally, what's happened is that Tomcat has compiled the JSP file into servlet form and has run it, with the results you see in the figure.

Figure 11-3. A working JSP file

That creates a JSP file. Although Eclipse didn't do much more than act as a text editor here, it can do a lot more; see the following recipes for details.

11.3.3.1 Eclipse 3.0

Eclipse 3.0 is intended to add more syntax highlighting for JSP files, but as of this writing, that hasn't appeared. Such support for JSP code would be great if it does come to pass.