Tutorial: Getting Started with JSP

Page content

What is JSP and why should I use it?

JSP stands for Java Server Pages and it’s part of the J2EE specification. That means with JSP your not using a scripting language, but a real object oriented programming language: Java. These days Java is one of the most used programming languages, popular mainly because of its ease of use.

So why should you be building your websites with JSP instead of ASP or PHP? Well, for one, PHP and ASP aren’t really scalable. They’re good fun when you want to build a small website, with some dynamic features, but they can’t handle larger web-applications. JSP can handle large applications, JSP can handle huge applications. Another great benefit is, that once you’ve got your website running, JSP code is easier to manage because most of it will be good old Java.

Setting up the environment

To start working with JSP, you’ll need an application server. For application servers I can advice Apache Tomcat (If your planning on building something small, like a personal website with some dynamic features) or JBoss (for the more heavy-load applications). Both are easy to install, I promise (an installation guide would be too much for this article, but both JBoss and Tomcat have great support on their websites). After installation, you should follow the documentation of your application server to find out three things: how to start it, where to put your files and how to visit your site. This is different for all application servers, so I can’t really help you on that.

Hello World

Right, I’m going to assume you’ve visited the JBoss website, installed the latest JBoss application server on your computer and found out all the basic information needed to use JBoss. So let’s make our first simple JSP page. Make an ordinary HTML page, but name it index.jsp. The JSP extension will tell the application server that there is Java code inside. Now add the following code in this HTML page:

<%= “Hello, World” %>

place this in your application server and watch what happens when you look at it in a browser (it shows the text “Hello, World”, but you already figured that out, right?).

What just happened?

Okay, in the first example I showed you this code:

<%= “Hello, World” %>

Whenever you see <% [some code] %>, you’ll know it’s JSP or Java code and so does the application server. What happened in the example was a little different. Because we just wanted to show some text, we added = to the JSP code start. This is a shorthand version of the following:

<% out.print(“Hello, World”); %>

This does the exact same thing. “out” is a variable that represents your output stream to the web browser and “print” is one of its many methods. This is in fact, ordinary Java code. Between <% and %> you can place any Java code you like. Let me show you another example:


Integer x = 0;

Integer y = 0;

for (int i = 0; i < 10; i++) {

x = x + 1;

y = y + x;




You see, ordinary Java code, this example prints a bunch of numbers (“135812172330384757” in fact). The point is that inside my JSP file, anything that is between <% and %> is Java, the rest is HTML.

Beyond your first step

If you already know Java, you know that you can use a lot of libraries with existing code. Because JSP is really just Java, all these libraries can be used. I’ll give you a small example:

<%= new Date() %>

This uses the Date object in the java.util package (package is just the word Java uses for libraries). If I try to run this, it will fail and an error message will show up in my browser. That’s because I have to tell my application server to import the java.util package. This is done by placing the following line in the top of the JSP file:

<%@ page import=“java.util.*” %>

This is what is known as a page directive, it tells the application server to do something for this page. In this case, import the java.util package. You can recognize a page directive by the @ sign.


Now you know the absolute basics of JSP, you can agree that it really isn’t very difficult to get into. However, because you can use the entire Java language, it’s extremely powerful. If, at this point you were to brush up your knowledge of Java, you’ll be able to build really nice web applications.

But believe me when I say these are just the absolute basics of JSP. The language has much more to offer and when used correctly, you can use JSP to build the most complex and powerful applications.