You are on page 1of 45

JSP -1

JAVA Server Pages


Designed By:

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

” Model-1 is very simple to understand and itMakes generation of


simple pages simple.
” But it also messy when a complex page is needed and tends to
require lots of Java code in the JSP file (a real no no from a
design point of view).

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

„ The servlets are the controllers

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

►EJB components, by design, are meant for distributed computing


►EJB components are scalable, transactional, and secure
►J2EE Containers provide primary services such as Naming, Login, Transaction,
Deployment and Security
►Developer can focus more on Business Logics
►Tons of applications have already bean written using EJBs

www.interviewDuniya.com
J2EE and Design Patterns

„ The J2EE architecture is built to enable


component developers to use a Model
View Controller (MVC) Design Pattern.

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

Request for Servlet 1

Servlet 1
Request for Servlet 2 Web Server
Servlet Container

Request for Servlet 1 Servlet 2

Requests handled by multiple threads inside


the 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

„ A simplified way to generate dynamic web content


„ Simpler then Servlets (Java is not mandatory)
„ Editing tool friendly
„ Can be used to separate the coding of presentation and
business logic
JSP is based on Java and Servlet technologies
„ An HTML page with imbedded tags and Java Code.
„ At runtime the JSP page is translated into a Java Servlet
„ The runtime is usually encapsulated in a special JSP-Runtime
Servlet. www.interviewDuniya.com
Difference …
„ JSP is Java code embedded in HTML; the Java code
is compiled (if necessary) and run by the container on
the server and the client only sees the results of that
code's execution mixed in appropriately with the html.
„ Servlets are compiled pure Java class files (not
mixed with HTML) that get posts from the client and
send html to in return.
„ Both require a container on the server, such as
Tomcat, which provides the environment and VM for
the Java program.

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?

„ Servlets that generate HTML output hide the


HTML inside Java.
„ Makes it hard for an HTML expert (!= Java expert)
to fix stuff.
„ Lock the content in Java.

„ Makes look and feel changes to the site problematic.

„ The ability to script with Java and JavaBeans


makes generating dynamic content simpler.
www.interviewDuniya.com
How it works?
„ Unlike javaScript which is interpreted in the web
browser, JSP pages are translated into java
servlets and execute in the servlet engine..
„ All the work is done on the server and all the
browser see is ordinary HTML.

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.

„ There is a new <%@ page … %> “Tag” that has


nothing to do with HTML
„ This is a JSP directive

„ We have some Java code enclosed between <% and


%>
„ This is a JSP scriptlet

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.

„ Defining class wide variables and methods.


„ For initialization.
„ Commenting your scripts.

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.

„ The APIs provides:


„ Programmatic page control.
„ Access to information that was supplied by using
directives

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.

„ A Servlet is instantiated in the following manner:


„ The container loads the Servlet’s class.
„ Using Class.newInstance() the container instantiates a
new instance of the Servlet.

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

You might also like