Bright Java Tutorial

is brought to you by DoITBright LLC


Writing Your First JSP

Java Server Pages a.k.a. JSP are server side programs. This means, JSP runs on an application server that is capable of supporting Java technology. Unlike HTML that can be developed be saved in any of your local directory and eventually opening it using a browser to see the out come, JSP needs an application server for you to see the out come. In this tutorial, we will be using Apache Tomcat. This application server is free to use and is very popular. A lot of web hosting companies offers Tomcat as web application server. The first thing we need to do is download the latest copy of Tomcat. You may click here to download it from the Apache website. At the time I am writing this, the latest version is Tomcat 9. Look for the 'Binary Distributions', then 'Core' and then 'Zip'. Here is the screen capture for this.

Download Tomcat

My operating system is Windows 8. So the next set of steps is for this kind of OS. Extract the ZIP file into your 'c:\BrightJavaTutorial\workspace' directory. Rename the extracted folder to tomcat9 for simplicity. Then, open a command prompt. key in the following commands as shown in the screen caputure below.
Start Tomcat

It is assumed that you have an existing Java Development kit on your local machine which is located in c:\BrightJavaTutorial directory and you have named the JDK folder as 'jdk1.8'. For your convenience, here are the commands used to start your tomcat using your command prompt as shown on the screen capture above.

  cd \
  set JAVA_HOME=c:\BrightJavaTutorial\jdk1.8
  set TOMCAT_HOME=c:\BrightJavaTutorial\workspace\tomcat9
  cd BrightJavaTutorial\workspace\tomcat9\bin
Once the Tomcat application server had successfully started, open your favorite browser and key in the below URL on your browser URL bar.

You should now be viewing the Tomcat Home Page that is served by your local application server. To shutdown the server, on the same command prompt where you started it, key in the command as shown below.

It is now time to create your first JSP. Go to your c:\BrightJavaTutorial\workspace directory. Create a folder and name it as 'jspLessons'. Then, open your favorite text editor like the Notepad. Copy and paste the code that is shown below.

        out.println("Hello world of JSP!");
Save the file as 'firstJsp.jsp' under the 'jspLessons' directory. Then, copy the 'jspLessons' directory and into the 'c:\BrightJavaTutorial\workspace\tomcat9\webapps' directory. Next step is to start your Tomcat application server. Once the server had started, supply this URL to your browser.

You should see a page that looks like the screen capture below.
Start Tomcat

As you have seen, a JSP is something like an HTML document that contains Java code inside it. We surrounded the Java code with '<%' and '%>' to accomplish such thing.

JSP are converted into Servlet. The Servlet is the underlying implementation of the JSP. Just to feed our curious minds, go to the directory 'C:\BrightJavaTutorial\workspace\tomcat9\work\Catalina\localhost'. You will notice that there is a directory named 'jspLessons'. Go inside that directory and continue to go deeper until you find files that are named 'firstJsp_jsp.class' and ''. These files were generated when we deployed our 'jspLessons' application that contains 'firstJsp.jsp' in our Tomcat webapps folder and started the application server.