Documente Academic
Documente Profesional
Documente Cultură
Introduction to JSP
Agenda
Click to edit Master text styles
What is JSP?
Why JSP?
JSP Vs Servlets
Implicit objects in JSP
SP Scripting Elements
Life Cycle
Examples
Hello World!
Current time is Sun Jun 17 14:39:53 IST 2007
Why JSP?
Click
If you
to are
editaMaster
text styles
programmer
or a web designer you will agree that not every programmer is a good
designer and not every good designer is a good programmer.
This is the exact problem posed by Java Servlets. Which means Java Servlets required the Java
programmer to know the designing skills because the Java Servlets did not separate the
Programming logic from the presentation layer.
Therefore there was a need to separate the design aspects from the Core Java programmers. This
was the reason why, JSP was introduced.
Why JSP?
JSP BenefitsClick
to edit Master text styles
Content and display logic are separated
Easier to develop web pages
Simplify web application development with JSP, JavaBeans (POJOs with getters, setters
and no argument constructor) and custom tags
Supports software reuse through the use of components (JavaBeans, Custom tags)
Automatic deployment
Recompile automatically when changes are made to JSP pages
Platform-independent
JSP Vs Servlets
Servlets can do a lot of things, but it is pain to:
Click
to edit
Master
text statements
styles
Use
those
println()
to generate HTML page
Format
<%= expression %>
<jsp:expression>expression</jsp:expression>
Semicolon are not allowed for expressions
Examples
Current time: <%= new java.util.Date() %>
Random number: <%= Math.random() %>
Format
<% Java Code %>
<jsp:scriptlet> Java code </jsp:scriptlet>
Examples
Display query string
<% String queryData = request.getQueryString(); %>
%>
<tr>
<td align="right" bgcolor="#ffffff"> <%=item.getQuantity()%> </td>
<td bgcolor="#ffffaa"> <strong><a href= <%=request.getContextPath()%>/bookdetails?bookId=
<%=bd.getBookId()%>"><%=bd.getTitle()%>
</a> </strong> </td>
...
<%
} // End of while
%>
Used to define variables or methods that get inserted into the main body of Servlet
class
Click to edit Master text styles
Outside _jspService()
Implicit objects are not accessible to declarations
Use declaration to override jspInit() and jspDestroy()
Format
<%! Java Code variables and methods %>
<jsp:declaration> variables and methods </jsp:declaration>
Examples
<H1>Some heading</H1>
<%!
private String randomHeading() {
return(<H2> + Math.random() + </H2>);
}%>
Resulting
Servlet class
public class xxxx implements HttpJSPPage {
Translation Phase
JSP page gets translated into a Servlet code
Compilation Phase
Servlet (java code) gets compiled
Execution Phase
Instance of the Servlet class is created
jspInit() method is executed
Servlet object is ready to serve the request; calls _jspService().
Once object is ready for destruction jspDestroy() is called
Translation/Compilation Phase
Click to edit Master text styles
Execution Phase
If the
instance
of the
Servlet class does not
Click
to edit
Master text
styles
exit
Container loads the Servlet class
<%@pageattribute="value"%>
include directive
<%@includefile="resourceName"%>
taglib directive
<
%@tagliburi="uriofthetaglibrary"prefix="prefixoftaglibrary"%>
Thank You