Writing Your First JSPJava 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.
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.
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 startup
<html> <body> <% out.println("Hello world of JSP!"); %> </body> </html>
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 'firstJsp_jsp.java'. These files were generated when we deployed our 'jspLessons' application that contains 'firstJsp.jsp' in our Tomcat webapps folder and started the application server.