< Day Day Up > |
Recipe 11.3 Creating JSP Files11.3.1 ProblemYou want to write JSP files and view them in a web browser. 11.3.2 SolutionCreate and edit JSP files in Eclipse. 11.3.3 DiscussionAs 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.
Figure 11-2. Editing JSP codeThe 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 fileThat 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.0Eclipse 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. 11.3.4 See AlsoRecipe 11.1 on installing Tomcat; Recipe 11.7 on editing web.xml in place; Recipe 11.9 on connecting to a JavaBean; Chapter 3 in JavaServer Pages (O'Reilly); Chapter 1 in the Java Servlet and JSP Cookbook (O'Reilly). |
< Day Day Up > |