Getting Started with JSP
Welcome to JavaServer Pages (JSP)! Over the next 21 days, you'll get an in-depth guided tour of the ins and outs of JSP. From the most basic aspects to the most advanced, we're going to become masters of JSP in this book. You've come to the right place.
If you want to do Web programming on the server, there's just nothing like JSP. It's easy to get started with JSP, and because it has the incredible power of Java behind it, there's no limit to how far you can go. Traditionally, server programming was a task only for experts, but with JSP, that's all in the pastnow anyone can make Web pages come alive as never before.
Let's dig into JSP immediately. In this, our first day on the job, we're going to take a look at these topics:
What JavaServer Pages can help you do
Setting up your development environment
Running the Tomcat Server
How we got here: from HTML to JSP
Building and dissecting your first JSPs
Understanding JSP syntax
JSP resources online
We're going to work with JavaServer Pages 1.2 in this book, but as you'll find, JSP itself is only the beginning. Because JSP lets you use Java to create Web pages on the server, much of what we're going to study is Java itself, as we see what we can do with JSP. And you'll find there's practically no limit.
What Are JavaServer Pages Good For?
Take a look at Figure 1.1, which shows the home page of Delta Airlines. Note the URL at the top of the browser: . The JSP in the index.jsp portion is the JSP in JavaServer Pages.
Figure 1.1 The Delta Airlines home page.
It's easy to find JSP at work everywhere on the Web these days. In fact, JSP is becoming the new standard for easily developed, easily maintained Web applications. As you might expect, there are many, many reasons for JSP's soaring popularity, and more are being invented all the time. Here are a couple of things that JSP can do for your Web pagesthis is just a starter list, of course:
Making Web pages come aliveThere are too many static pages on the Internet already. Why add yours to the list? To get noticed, your page must be liveit must do something, or offer some service. What better way to liven pages up yourself than to use JSP? Now your Web pages can interact with the user in real time.
Getting data from the userAll the text fields, radio buttons, check boxes, and so on, you see in Web pages can hold data. JSP makes it easy to read that data back to the server, and to send Web pages that use that data back to the browser, as we'll see in Day 4, "Reading Data from Web Pages: Buttons and Fields," and Day 5, "Reading Data from Web Pages: Check Boxes, Radio Buttons, and Select Controls."
Easier to programThere are plenty of server-side programming options out there, but none easier to program than JSP. In fact, JSP is specifically designed to be easy to program, as you'll see today.
More power through JavaJSP lets you run Java code when creating Web pages. That fact alone makes what you can do with JSP practically boundless. Java is an ever-expanding, nearly endless programming package, and JSP puts it at your service. And you don't have to be a Java expert eitheryou can start small, as we will in this book, building up to whatever level you want.
Connecting to databasesOne of the most popular things you can do with JSP on the server is connect to databases via Java. It's not as hard as you might think, and we'll see how to connect to databases, add our own data to them, get data out of them, and more in this book.
PerformanceJSP is built to perform better than other server-side programming packages. JSP is built right into the server software itself, which means that many users can access your Web page without using a lot of extra memory or degrading performance. In some other server-side programming packages, a whole new process has to start for every user that accesses your Web page, and if a lot of users are downloading that page, it can bring the server to a virtual halt.
Separating code and dataA very big topic in the programming world these days is the separation of code and data, and JSP fits right into that. The idea is that when your JSP programming code that the server runs is separate from the data that that code works on, it's easier to write and maintain your programs. As we'll see in this book, there are some new ways of working with JSP that let you separate code and data very effectively.
Handling cookiesA popular use of JSP is to work with cookies, storing information on the user's machine. Some people love cookies, some hate them, but there's no doubt that JSP allows you to use them. Want to record a user's special settings for your Web page? A JSP cookie will do that. You'll see how to work with cookies in Day 7, "Tracking Users with Sessions and Cookies."
In choosing JSP, you've chosen the right package for server-side programming. You can do startlingly powerful things with JSP, because JSP has the full power of Java behind it on the server. For example, take a look at Figure 1.2, which shows an example we'll develop later in the book (in Day 20, "Creating Images on the Server and Handling Internet Programming," where you'll learn how to create and draw images using Java). In this case, the user can "draw" a line in the browser using the mouse, and the software we'll develop for the server will actually create the corresponding image file (a JPEG file) and send that image back to the browser, as you see in the figure. That's a JPEG image file you're looking at in the figure, created interactively with Java on the server thanks to JSP.
Figure 1.2 Creating a JPEG image file.
Let's get down to business by setting up your own development environment in which to create and use JSP ourselves. This development environment will give you a JSP server and allow you to develop your own JSP-enabled Web pages. All the software you'll need is free, and you can download it from various sites on the Web.