venkatesh.mansani@yahoo.
com
Naresh i Technologies
Java Server Pages part-1
JSP:
JSP stands for Java Server Pages.
JSP is a specification for developing applications with Java programming
language.
JSP Engine:
JSP Engine is a piece of software and converts JSP into a Servlet
There are three phases in JSP Architecture:
1) Conversion 2) Compilation 3)Execution
1) JSP converted into a Servlet by JSP Engine.
2) Converted Servlet compiled by Java Compiler
3) Servlet Excecuted by Java Runtime Environment and output captured like
HTML.
4) Jasper is a JSP Engine name in Tomcat Server
5) Catalina is a Web Container name in Tomcat Server
JSP Benefits:
1) Content and display logic separated
2) Simplifying web application development by using JSP with Java beans and
custom tags.
3) Recompiles automatically whenever changes are made in JSP
4) Easier to write and develop web applications
Converted Servlet Life Cycle Methods:
1) jspInit() 2)_jspService() 3)jspDestroy()
jspInit():
It is called by web container whenever first request comes to a servlet.
[email protected] Naresh i Technologies
_jspService():
It is called by web container for every request.
jspDestroy():
It is called by web container whenever web application is undeployed from
server or server shuts down.
JSP Elements:
JSP elements are used to write JSP programs. JSP Elements are divided into three
categories.
1) Scripting Elements
2) Directives
3) Actions
Scripting Elements:
There are four scripting elements in JSP:
1) Scriptlets 2) Declarations 3)Expressions 4) Comments
1) Scriptlets:
Syntax: <% ……………. %>
(or)
<jsp:scriptlet>
============ => Java Code
</jsp:scriptlet>
2) Declarations:
Syntax: <%! ……………. %>
(or)
<jsp:declaration>
[email protected] Naresh i Technologies
============ => Java Code
</jsp:declaration>
3) Expressions:
Syntax: <%= ……………. %>
(or)
<jsp:expression>
============ => Java Code
</jsp:expression>
4) Comments:
Syntax: <%-- …………. --%>
JSP Implicit Object References:
There are 9 implicit object references are present in JSP. They are ready to use.
1) “out” is an object reference of JspWriter
2) “request” is an object reference of HttpServletRequest
3) “response” is an object reference of HttpServletResponse
4) “config” is an object reference of ServletConfig
5) “application” is an object reference of ServletContext
6) “page” is an object reference of Object
7) “pageContext” is an object reference of PageContext
8) “session” is an object reference of HttpSession
9) “exception” is an object reference of Throwable
JSP Programs:
1) JSP program to display welcome message:
[email protected] Naresh i Technologies
<% out.println("Welcome to JSP"); %>
2) JSP program using HTML:
<html>
<body bgcolor=red text=yellow>
<h1>
<% out.println("Welcome"); %>
</h1>
</body>
</html>
3) JSP program to count the no. of views:
<html>
<body bgcolor=green text=yellow>
<h1><%! int count=0; %>
This page has been accessed <%= ++count %> times
</h1>
</body>
</html>
4) Program to display system time:
<html>
<body bgcolor=green text=yellow>
<h1>
<% java.util.Date d=new java.util.Date(); %>
<%= d.getHours() %>:<%= d.getMinutes() %>:
<%= d.getSeconds() %>
[email protected] Naresh i Technologies
</h1>
</body>
</html>
5) Program to display multiplication table:
<html>
<body bgcolor=red text=yellow>
<table border=20>
<% for(int i=1;i<=100;i++) { %>
<tr>
<td> 5 </td>
<td> x </td>
<td><%= i %></td>
<td> = </td>
<td><%= 5*i %></td>
</tr>
<% } %>
</table>
</body>
</html>
By
Mr. Venkatesh Mansani
Naresh i Technologies
Java By Venkatesh Mansani
[email protected]