Creating Your First JSP Page - Creating Your First Web Application
(Page 4 of 7 )
Okay, you’ve got Java, you’ve got Tomcat: Now you’re ready to write a little Web application. This example will be your first look at JSP technology, which mixes Java code with standard Hypertext Markup Language (HTML), and what could be better for your first application than a version of the timeless “Hello World” program? Far be it from us to break from long-standing tradition. To add a dynamic touch, you’ll make it perform some hard-core number crunching.
Trying It Out: Building a Simple Web Application
To build a simple Web application, follow these steps:
You’ll start by creating a new Web application called
helloJSP. To do this, create a new folder with that name in the
webapps subdirectory off the Tomcat installation folder. This will create the directory structure shown in Figure 1-3.
Figure 1-3. Tomcat’s directory structure- Inside this folder, you need to create another folder. You won’t actually use it in this chapter, but it must be present for Tomcat to recognize your Web application.
Inside the helloJSP folder you just created, create a folder called WEB-INF (on Linux, this directory name must be ALL CAPS). As you’ll see in later chapters, this folder is the location for essential files for the Web application.
You can now place all sorts of resources in the helloJSP Web application folder that form part of the Web application itself. This might include HTML files and JSP pages, which can be placed directly inside the helloJSP folder, and you could view them by navigating in your Web browser to a URL such as http://localhost:8080/helloJSP/filename.html.
- You’re going to create a JSP page, which can include Java code that’s run by the Web server (in other words, by Tomcat) to alter what’s sent to the browser. It’s important to be clear that the code is running on the server— although, as you run examples in this book, you’ll probably have the browser running on the same machine as the Web server, that’s not the case for applications that have been deployed.
Create a new text file in your helloJSP folder, and call it index.jsp. When writing code files, be they JSP or Java, you’ll need to use a text editor such as Notepad or emacs rather than a word processor such as Microsoft Word that would insert special formatting characters that would render your code useless.
- Open the new index.jsp file, and add the following
<html>
<head>
<title>My First JSP</title>
</head>
<body>
Hello, world!
<p/>
2 + 2 is ${2 + 2} and 4 * 4 is ${4 * 4}
</body>
</html>
Save the file, and you’re done.
Now, you’ll check to see how your JSP page appears in a Web browser. Start Tomcat (if it’s still running from earlier, you’ll need to shut it down and restart it so it’ll recognize your new
helloJSP Web application), and then navigate to
http://localhost:8080/helloJSP/index.jsp in your browser. You should see a screen that looks something like the one in Figure 1-4.
Figure 1-4. A first JSP
Congratulations! You’ve created your first JSP page, and you’re on your way to a larger world of Java fun. If all this still seems a bit hazy at the moment, don’t worry because things will become clearer as you progress through the book.
If it didn’t work for you, make sure that Tomcat is running and that you have capitalized the URL just as the index.jsp file and helloJSP folder are capitalized on your hard disk.
How It Works
If you’ve done any work with HTML, the JSP you wrote should look familiar to you. In fact, the only JSP-centric code you wrote was this line:
2 + 2 is ${2 + 2} and 4 * 4 is ${4 * 4}
JSP uses the ${...} special notation to distinguish itself from normal HTML. The servlet container (that is, Tomcat) will then attempt to evaluate the expression within those tags.
In this case, you’ve created code that completes two simple mathematical expressions: 2 + 2 and 4 * 4. Of course, you’ll soon learn how to do much more than simple arithmetic.
While you’re here, select View -> Source in your browser to see exactly what HTML the browser received. Notice that it’s entirely HTML: The JSP code elements have been replaced with the result of the contained expressions, and the Java code was run on the server and never made it as far as the browser:
<html>
<head>
<title>My First JSP</TITLE< B>></title>
</head>
<body>
Hello, world!
<p/>
2 + 2 is 4 and 4 * 4 is 16
</body>
</html>
The URL you used to request the page, http://localhost:8080/helloJSP/index.jsp, comprises five distinct parts:
http: This indicates that your request and response are carried over the Hypertext Transfer Protocol (HTTP), which is the standard mode of communication between browsers and servers on the World Wide Web.
localhost: This is the name of the Web server. localhost is a reserved name to indicate the local machine—that is, the machine on which the browser is running. Normally, a Web browser will access a server somewhere else in the world, in which case this part would be replaced with the domain where you’re making the application available, such as www.myJSP.com. During development and testing, however, pages are often accessed from a browser running on the same machine as the Web server, in which case the localhost shortcut can be used.
Technically, localhost is an alias for the IP address 127.0.0.1, sometimes called the loopback address, which indicates the local machine.
8080: This is the port number on which the Web server is listening. A computer can have various server programs each waiting for clients(such as Web browsers) to connect to them. To avoid clashing, each server program must have a unique port number. Normally Web servers use port 80, so Tomcat uses port 8080 by default to coexist with any other Web server that may be running on the same machine.
helloJSP: This is the name of the Web application you created, and it tells Tomcat which subfolder of webapps contains the resources for this application.
index.jsp: This is the name of the actual document you requested. The .jsp extension tells Tomcat to treat the file as a JSP file and execute any Java code it contains. Files that have the .html extension are left untouched and sent directly to the browser.
You can also create subfolders in your helloJSP folder to hold other Web resources, such as images. Such subfolders don’t become Web applications in their own right, though—only folders directly within Tomcat’s webapps folder are Web applications.
Next: Exploring a Brief History of Java and the Web >>
More Java Articles
More By Apress Publishing
|
This article is excerpted from Beginning JSP 2: From Novice to Professional, by Peter den Haan et al (Apress, 2004; ISBN: 1590593391). Check it out at your favorite bookstore. Buy this book now.
|
|