JSP Overview

In the previous example, we have seen that how to write JSPs and write java code inside it. If you have previously worked in Java, you know that in Java, everything is a class. But in JSP, we have not seen anything like that. Let’s have some experiments, to understand it in a little bit of more detail.

Experiment 1

Where to Write method definitions In JSP?


Remove the method from definition tag and add it to the script tag, it will surely give an error as mentioned in the below source.

Now ignore the error and try to run this JSP file by Right clicking on the file and select “RunAs”  then “RunOnServer”. This will give you the 505 Error

1-Could not find Deffination JSP


This experiment tells us that we could not have a method defined in the script tag that is <% %>.

Let’s have our correct code back which is shown below.

Experiment 2

Write plain HTML between JSP script tags:

Question: How to separate Static content from dynamic content?

In this experiment, we will try to find the answer to above question.

Analysis :

Let’s add a for loop to display dynamic values in our source with the help of scripting tag; we can add as many tags as we want according to our needs. Notice the script tag where for loop is implemented in the below source. It has a static and dynamic part in one script tag. You can use the below source and try to run the code. Try to run this JSP file by Right clicking on the file and select “RunAs” -> “RunOnServer”

This will be the output.

2 - JSP display Dynamic content

Now we will separate the static part from the dynamic path in the source below, by adding more ending and closing scripting tags inside the code. You can notice the line where I have commented in the source.


Try to run the above JSP file by Right clicking on the file select “RunAs” -> “RunOnServer”

2 - JSP display Static content out of the script Tag


This tells that we can have static code between two scripting tags, and it will be treated as same.

Combined Results of Exp1 and Exp2:

Above two demonstrations are to describe the two most important behaviors of the scripting tags.

  • We couldn’t define a function inside the scripting tag
  • We can use simple HTML code between the two scripting tags.

How JSP works?

Let’s explain it a bit more in detail; JSP is a class. Our “FileName.jsp” will we converted into the Java Class by the web container, the Java class that is created is a servlet that runs on the container. Every JSP class that is deployed on the server( in our case tomcat), Tomcat takes the JSP and converts it into a Servlet class. If we have to write all the JSP logic code in the servlet we will do it in the doGet() method. So every scripting code that is in the JSP file will be added in to doGet() Method of the generated servlet and for the static content to display we will use printWriter.println(), so that’s exactly what tomcat does as well, it takes the static content and places it into the printWriter.The println() method of the generated servlet.

So, that is the reason of why we cannot have method definition inside the scripting tag, because scripting tag part will go in the doGet() method of the generated servlet.

We can also access the generated servlet class in the tomcat folder. Go to the tomcat installation directory in my case it is <you tomcat installation directory>\apache-tomcat-7.0.67\work\Catalina\localhost\SimpleServletProject\org\apache\jsp this path may also vary according to the tomcat versions.

3- Directory for JSP autogenerated Servlet Class

This directory has two files till know first is FirstJspFile_jsp.java and the second is FirstJspFile_jsp.class file

You can open the Java file and look into the auto-generated source code; I have also placed the code of “FirstJspFile_jsp.java” below if you could not find the directory in your tomcat installation.


In this tutorial, we have accomplished following things

  • Basic concept of JSP
  • JSP tags Explained in detail
  • How web Container handles JSP