Professional Documents
Culture Documents
www.interviewDuniya.com
Agenda
Introduction
JSP – What and Why?
My first JSP
JSP Fundamentals
JSP Directives and APIs
JSP and Tags
MVC (Model 2) Application
www.interviewDuniya.com
Introduction
Architecture
www.interviewDuniya.com
Architectures
Developing web pages is easy
Designing flexible and extensible web applications
that combine HTML, JSP, JavaBeans, databases,
etc., is not
We discuss two common approaches for building
such structures
Both use the Model-View-Controller design pattern
Model 1 involves JSPs and JavaBeans
Model 2 adds Servlets to act as a controller
www.interviewDuniya.com
Model-View-Controller
A Design Pattern
Controller -- receives user interface input,
updates data model
Model -- represents state of the world (e.g.
shopping cart)
View -- looks at model and generates an
appropriate user interface to present the data
and allow for further input
www.interviewDuniya.com
Model-View-Controller
JSP
View
Bean
Controller
Model
Servlet
www.interviewDuniya.com
www.interviewDuniya.com
www.interviewDuniya.com
A powerful advantage of JSP is the ability to
separate an application's business logic from
its presentation.
Using Smalltalk object-oriented terminology,
JSP encourages MVC (model-view-controller)
web applications.
JSP classes or beans are the model, JSP is the
view, and a servlet is the controller.
www.interviewDuniya.com
Model 1 Architecture
www.interviewDuniya.com
Model 1 Architecture
Browser submits requests to JSP pages
JSPs access business objects indirectly,
through JavaBeans
Insulates the JSPs from changes in the business
objects
As long as bean interfaces remain constant, the
JSPs are independent of the business object
implementations
www.interviewDuniya.com
Model 1 Architecture
Software developers implement the business
objects and beans
Web content designers implement the JSP
pages
Oftentimes, however, the JSP pages decisions to
be made regarding destinations
Winds up incorporating Java into the page
www.interviewDuniya.com
Model 2 Architecture
The Model 2 architecture also separates content
generation from content presentation
Browser requests are submitted to a servlet
Servlet accesses business objects to create content
Most Java code goes here
Content is stored in a JavaBean
JSP pages access the beans for presentation
www.interviewDuniya.com
Model 2 Architecture
Model-2 is very clean, Makes generation of complex
pages simpler as it removes most of the Java code
from the JSP file and places most of the logic work
in Java (where it belongs).
But its not as simple as Model-1 and is makes
creation of simple JSP files a bit complex.
www.interviewDuniya.com
Model 2 Architecture
Model 2 is a modified Model-View-Controller
architecture
The business objects and beans are the model
The JSPs are the view
www.interviewDuniya.com
What is available in J2EE Technology
Web Browser,
Applets HTTP
and optionally Session Beans
JSPs
Java Bean Entity Beans DB
and Servlets
Components Message Driven
RMI
Beans
Application Clients or IIOP
(Java)
Web Tier Business Tier
Client Tier EIS Tier
J2EE Server
J2EE Architecture
www.interviewDuniya.com
J2EE and Design Patterns
www.interviewDuniya.com
MVC Structure for J2EE
www.interviewDuniya.com
Advantages of MVC
Separating Model from View (that is,
separating data representation from
presentation)
- easy to add multiple data presentations for
the same data,
-facilitates adding new types of data
presentation as technology develops.
-Model and View components can vary
independently enhancing maintainability,
extensibility, andwww.interviewDuniya.com
testability.
Servlets
www.interviewDuniya.com
Servlets
Servlet 1
Request for Servlet 2 Web Server
Servlet Container
www.interviewDuniya.com
JSP
What is it? Why do we need it?
www.interviewDuniya.com
helps effectively separate presentation from
content
separation of presentation from content by
using the JSP Model 2 architecture. This model
can also be seen as a server-side implementation
of the popular Model-View-Controller (MVC)
design pattern
www.interviewDuniya.com
What is JSP?
JSP == Java Server Pages
www.interviewDuniya.com
JSP – A standard
JSP is a Java Standard:
Defined by a group of companies led by JavaSoft.
Current version is 1.2, but previous versions are also in use
(mainly 1.1).
Ongoing effort to improve JSP.
www.interviewDuniya.com
Why JSP?
www.interviewDuniya.com
Steps..
A JSP page is written and stored in the
document tree of a Web server, usually as a file
with name ending in .jsp
The web server receives a request for the
document. Since the name ends in .jsp, it
forwards the request to the servlet engine
The servlet engine passes the request on to the
JSP engine, which is typically a servlet itself.
www.interviewDuniya.com
The last step…
The JSP engine determines whether the .jsp file
is newer than the servlet class that implements
if.
If this is the case, it parses the .jsp file and creates
java source code for the equivalent servlet.
It then compiles the servlet and causes the servlet
engine to load and execute it.
www.interviewDuniya.com
Remember…
When a .jsp file is first requested from a web
browser, the JSP engine compares its timestamp
with the timestamp on the generated servlet
class.
If the class is older(or non existent), the JSP
engine creates the java source code for the
servlet and compiles it.
The generated class is then loaded as any other
servlet would be.
www.interviewDuniya.com
The JSP Syntax
Inline Java code delimited by <% and %>.
Also printing of expressions as text by using <%= %>.
Special tags to declare class wide variables and methods.
Special tags to use with JavaBeans.
Special tags to expose JSP services.
JSP directives to specify.
Interfaces implemented by the Servlet, classes it extends,
packages to import etc.
www.interviewDuniya.com
JSP Directives and Scripting
Elements
Directives <%@ directive %>
Declarations <%! declaration %>
Expressions <%= expression %>
Code Fragment/Scriptlet <% code fragment %>
Comments <%-- comment --%>
www.interviewDuniya.com
My first JSP
Hello World
www.interviewDuniya.com
Helloworld.jsp – The code
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN">
<HTML>
<HEAD>
<%@ page language="java"
contentType="text/html; charset=US-ASCII"
pageEncoding="US-ASCII" %>
<META name="GENERATOR" content="IBM WebSphere Studio">
<TITLE>helloworld.jsp</TITLE>
</HEAD>
<BODY>
<P> <% out.print("This is a sample JSP file"); %> </P>
</BODY>
</HTML>
www.interviewDuniya.com
Helloworld.jsp – Output
www.interviewDuniya.com
Helloworld.jsp – Observations
At first, the JSP file looks like a regular HTML page
HTML tags, doctype, etc.
www.interviewDuniya.com
Examples
JSPscriptlet.jsp
http://localhost:8080/examples/jsp/JSPscriptle
t.jsp?bgColor=black
JSPExpressions.jsp
www.interviewDuniya.com
JSP Fundamentals
www.interviewDuniya.com
JSP Fundementals
Adding Java to a plain HTML page.
Scriptlets
Use JSP implicit variables.
To obtain information.
To write data back to the user.
www.interviewDuniya.com
JSP Directives and APIs
JSP 1.0 provides directives and APIs to allow
enhanced control over the page.
Directives provides:
Declarative page control (caching for example).
Defining Java related page attributes.
www.interviewDuniya.com
Servlet Instantiation
The container instantiates a new Servlet in two
occasions:
A user request service from a Servlet that was not
instantiated yet.
The Servlet is in the list of startup Servlets and the
container is starting.
www.interviewDuniya.com
MVC (Model 2)
Application
www.interviewDuniya.com
General Web Application Design
Rules
Separate presentation and business logic.
JSP is only about presenting data.
The JSP file does not need to know where the data came
from.
Separate the work done by the HTML and Java coders
to eliminate contention.
These are two different developers.
The tools used by the HTML/Java coders are not that good
when it comes to handle Java/HTML code.
www.interviewDuniya.com
Reminders…
Data access and business logic is done in Java.
JSP get to see the results of this business logic
via JavaBeans/special tags.
Servlets and JSP can interact in two access
models known as Model-1 and Model-2
www.interviewDuniya.com