Scroll to top

JSP Basics

A JSP becomes a servlet. You don’t directly create servlet, it is generated by Container only. Container takes what you have written in your JSP, translates it into a servlet class source file then compiles that into a java servlet class. And this code would execute in the same as it were written in java file.

JSP Elements

There are 4 basic JSP elements.

  • Scriptlet
  • Directive
  • Expression
  • Declaration


You might face the situation where you have to add Java code in JSP. You can use scriptlet.




Type of Directive

Its a way to give instruction to Container on at page translation time. Type of Directive

  • page
  • include
  • taglib
Page Directive

There might be situation where you need to import some package in JSP like you do in java file. You can use directive for this purpose.

<%@ %>
Single package import
<%@ page import="java.lang.*" %>
Multiple package Import
<%@ page import="java.lang.*,java.lang.util.*" %>



Expression is used when you want to provide some argument to statement that prints to the implicit response PrintWriter out. Expression becomes the argument to an out.print().


Method parameter

<%= contact.getName()%>

variable access

<%= x %>

Note: If there are local variable as well as instance variable then above statement will print the value of local variable if you want to print the instance variable then use this like below.

<%= this.x %>

Here contact.getName will be provided as the argument to out implicit object as it is. Note: DO not place ”;” in the end of expression.
Note: All scriptlet and expression code lands in a service method. So all the variables declared in scriptlet will be a local variable. To make a class level variable JSP declaration can be used.


JSP declaration defined inside the class but outside the service method. It means declaration is for static and instance variable and methods.


Variable declaration

<%! int i=0;%>

Method Declaration

<%! String getName()
   String name = "JBT";
    return name;

Note: Here we are using ”;” in the end as it be statment in generated java file. So it is required.

Implicit Object

There are some implicit object which container will provide to use directly in JSP these are called Implicit Object. There are 9 implicit Object in JSP

  • out
  • request
  • response
  • session
  • application
  • config
  • exception
  • pageContext
  • page

These implicit objects are map to Object from Servlet/JSP API.

Implicit ObjectAPI

JSP Comment

You can put comments in JSP and there are two type of comments in JSP.

HTML Comment

These comment will be the part of generated response means these comment will go to client side. Syntax:

<!-- Comment -->

JSP Comment

These comments are for the developer only it will not be the part of translated page. Syntax:

<%-- Comment --%>
Author avatar


  1. Toan LM

    I’m newbie JSP developer, It is good tutorial for me, Basic !

  2. hi it is too useful for me because I was not knowing anything about jsp but after reading this tutorial I m getting to know about jsp thank u so much. very good tutorial it is.

  3. sethu don

    good simple guidence…

  4. Vidya

    It is a good simple helpful tutorial. can u give the brief tutorial of jsp

    • There are some other articles written under JSP. DId you get a chance to look at those topics?? Or you are looking for some other advance topics.
      Please let me know.


Post a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.