Introduction to JSP

In our Previous tutorials we have learned Servlets in considerable details, From now on we will start learning JSPs, to understand JSP’s we have to understand servlets first, You can check our web development in Java series to understand servlets. It is strongly recommended that before starting this tutorial of introduction to JSP, you have completed Servlets first, because if you don’t know Servlets, there is now reason to learn JSP’s.

Servlet Series

  1. Set up environment
  2. Create a dynamic web application
  3. Create a new servlet
  4. Understanding servlets
  5. Get and Post in servlets
  6. Object Scopes with session objects
  7. Session objects in servlets
  8. Understanding lifecycle of servlets
  9. Servlets filters
  10. Servlet Listeners

Download Complete Servlets Project Here

Limitation in Servlets:

We have learned in our servlet tutorial that user makes an HTTP request from its browser, and it is received by the Web server in form of URL request, web server passes it to the container, and then container passes it to the servlet, servlet after processing sends back the response in form of HTML page, for this purpose servlet uses print writer, for large web pages we have to write many print writer lines and if these lines have escape characters in them, it will become a headache for us to maintain this kind of web pages. To avoid this sort of problem we use JSP,s. So what happens in JSP’s? Let’s take a look in the introduction section below.

Introduction of JSP:

JSP are the files having HTML text inside its output can create a static HTML page layout with JSP’s and by using Java code we manage the dynamic whether that HTML page.

Creating First JSP:

Start Creating JSP
Start Creating JSP
  1. Go to the Navigation Tab in the Top Left corner
  2. Expand the Project that we have previously created. Search for Web Content Folder and Right click on it.
  3. Click on New from the opened context list window. It will open another context list window.
  4. Click on Other from this list; it will give us the selection wizard.
    Create new JSP FIle
    Create new JSP File

     

  5. In this Selection wizard finds the “Web” folder and expand it by clicking on it.
  6. Find item “JSP File” from the list under “Web” folder
  7. Click on Next. This will open another window.

    Giving Destination and Name to JSP
    Giving Destination and Name to JSP
  8. Select the “WebContainer” to tell the destination of the file.
  9. Give Name to this File “FirstJspFile.jsp”
  10. Click Finish.

    Running JSP Template
    Running JSP Template
  11. Right, Click on the File that is just Created with the name “FirstJspFile.jsp”
  12. Click on RunAs. It will open another list.
  13. Select RunOnServer from the originated list. It will Run the File with no output.

We have just created our First JSP through simple steps.

The created JSP File will have the following code.

Now let’s edit the JSP file to test whether it is working or Not, we have added the HTML h3 element to test the output as follows

  • After editing the source “Save” the JSP file by pressing “Ctrl+S” and Run the file again Or you can also Refresh the page to see the effect of editing.
  • Notice the URL through which server is accessing the JSP

  • Here we can see the changes. So we have successfully created and executed our First JSP.
Test Run JSP
Test Run JSP

How JSP Different From simple HTML?

There are tags that are not available in HTML like <% .. java logic code come here ..%>. We can have dynamically generated content that we have to display on the web page, for this purpose we use this script tag <%%>. For Example:

Output will be like this

Dynamic Output with JSP
Dynamic Output with JSP

If we want to remove static text from our above script, we can also do it like this.

Output will be like this

JSP Script Output
JSP Script Output

We also have the declaration tag like this <%! %> we can declare methods inside this tag. This declared method would then be available in all script tags. The bellow Source is the example of this tag we have declared a function add in the declaration tag and call it in the script tag.

Output will be like this

Declaration Tag in JSP
Declaration Tag in JSP

Summary:

In this introductory tutorial, we have learned following things.

  • Why we don’t use servlets to send dynamic HTML responses.
  • What are JSP’s
  • How to Create a Simple JSP’s
  • What are some common tags in JSP’s

In the next tutorial, I will give complete JSP overview through examples.

Hope you like this tutorial! Stay tuned for more upcoming tutorials.