JSP



JSP

If you've never used JSP before, you will want some background.

The book Pure JSP by James Goodwill is an excellent one. It gets the central concepts across in pages, not tens of pages.

1. Running JSP as HTML
To see things running right away, you should try the following:

Create a directory myjspapp in the webapps directory of your Tomcat base directory.

In this directory, put a file

Hello.jsp

with the simple text at the top of the file:

Hello

Run Tomcat.

Register the directory myjspapp in the Tomcat Web Application Manager to which the link is in the top left of the Tomcat main page in your browser.

Then in the browser window, point to http://127.0.0.1:8080/myjspapp/Hello.jsp

You should see the text "Hello" towards the top of the browser window.

This is your first JSP application, and you can take it in various directions from here.

2. Running JSP Actively
Now you will want to put some Java code in this active running file.

After the Hello statement on top as shown in (1), write the following:

<br>

<%
    String s = "Hello with Java";
%>

<%= s %>

In your Tomcat Web Application Manager click "Reload" for your application myjspapp..

Then you can refresh your browser window.

You should see

Hello
Hello with Java

on top of your browser screen.

Now you have vast power in your hands.

You can put any arbitrary Java code in the <%  .. %> block including the entirety of classes in the Java SDK and also from any additional frameworks you wish to use.

To print any Java variables to the browser window (most likely String variables) you can just type

<%= stringVariableName %>

With this, you can do anything. All that's left is some native tags and components that JSP provides you. The most valuable ones are:

application
out
request
response
session

You should look up some JSP specific documentation on the web to see what you can do with them.

My goal in this section has been to get you started with running actual JSP applications if you are new to that, and show you the way forward. If there is a lot of demand, I will put more detail here in the JSP section - particularly how to use the components named just above.