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
Scriptlet
You might face the situation where you have to add Java code in JSP. You can use scriptlet.
<%
<a href="http://www.php.net/system">system</a>.out.println("Hello");
%>
Directive
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.
Syntax
<%@ %>
Single package import
<%@ page import="java.lang.*" %>
Multiple package Import
<%@ page import="java.lang.*,java.lang.util.*" %>
Expression
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().
Syntax
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.
Declaration
JSP declaration defined inside the class but outside the service method. It means declaration is for static and instance variable and methods.
Syntax
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 Object | API |
---|---|
out | JspWriter |
request | HttpServletRequest |
response | HttpServletResponse |
session | HttpSession |
application | ServletContext |
config | ServletConfig |
exception | Throwable |
pageContext | PageContext |
page | Object |
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 --%>
hi
hello
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.
Regards
good simple guidence…
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.
I’m newbie JSP developer, It is good tutorial for me, Basic !
Good tutorial for understanding JSP basic.
great … effort…
well done ,,, thats good effort to make things simpler and easy … like your work….
keep it up
Simply Awesome Tutorial
really very easy to learn..thanks
its good,but cannot access next n prevs pages.
Hi Sunanda,
Thanks for your comment. Currently there is no Next and Previous button on page because of some tech issue that is the reason i have provided right navigation for better movement between same type of topics.
I have already taken your suggestion and will update the site as per your suggestion soon.
Thanks Again for your suggestion
it’s good,but in too short.
Explained in simple and short manner(not to confuse more). It would be good if also included some focus on Directive elements include and taglib.
it is best for me ,, goooooooood tutorial
ok..