JSP Syntax MVC Design Pattern Dynamic DB Connection: Java Server Pages
JSP Syntax MVC Design Pattern Dynamic DB Connection: Java Server Pages
JSP Syntax
Techniques:
MVC Design Pattern
Dynamic DB Connection
Web Server
Container
1. Send request C 2. Call
3. Query
6. Response DAO DB
5. Render/Send
4. Store data into
7. Display V get (if any)
DTO
Objectives
• How to build web application applying MVC
model using Servlet, JSP + Scripting Element
– MVC Model
– JSP vs. Servlet
– JSP mechanism, syntax
– How to use JSP combining the Servlets and Java
objects
– How to connect DB using Dynamic connection or
DataSource
Objectives
Day 1, 2, 3, 4, 5, 6 – Login
Servlet, JDBC
Day 7, 8, 9 – Search
Break Down
User
View Controller
Model
This is a MVC Model
MVC Design Pattern
Model – View – Controller
www.netbeans.org
MVC Design Pattern
Model – View – Controller
www.netbeans.org
MVC Design Pattern
No MVC
Web Server
MVC Design Pattern
MVC Model 1
Page-centric application
MVC Design Pattern
MVC Model 1 – Example
Page-centric Scenario
MVC Design Pattern
MVC Model 1 – Generalization
• Purpose
– Separate “business logic” from “presentation logic”
– Need for centralized security control or logging, changes little over time.
– Apply to applications that have very simple page flow.
• Advantages
– Lightweight design – for small, static application
– Separation of presentation from content
• Limitations
– Navigation Problem – Changing name of JSP file must change in many
location
– Difficult to maintain an application – large java code being embedded in JSP
page
– Inflexible
– Performance is not high
– Not scale up over a period time
– Not Suitable for large and complex application
MVC Design Pattern
MVC Model 2
Container
MVC Design Pattern
MVC Model 2
• Separates the “Business Logic” from the “Presentation Logic” and
has an additional component – a Controller
• Use Servlet and JSP together
• JSP pages
– Are used only for presentation
– Retrieve the objects created by the Servlet
– Extract dynamic content for insertion within a template for display
• Servlet
– Handles initial request, partially process the data, set up beans, select suitable
business logic to handle request , then forward the results to one of a number of
different JSP pages
– Serves as a gatekeeper provides common services, such as authentication
authorization, login, error handling, and etc
– Servlet serves as a central controller that act as a state machine or an event
dispatcher to decide upon the appropriate logic to handle the request
– Act as a Controller that controls the way Model and View layer interacts
MVC Design Pattern
MVC Model 2 – Generalization
Servlet-centric Scenario
MVC Design Pattern
MVC Model 2 – Generalization
• Purpose
– Separation of presentation logic and business logic
– A Model represents information specific to a particular domain on
which the application operates
– A View is typically a user interface element. A single Model may
be presented as multiple views
– A controller component responds to events and processes request
and may invoked changes on the Model
• Advantages
– Easier to build, maintain and extend
– Provide single point of control (Servlet) for security & logging
– Encapsulate incoming data into a form usable by the backend
– Can reusable code
• Limitations
– Increase Design Complexity
MVC Design Pattern
MVC Model 1 & Model 2 Comparison
Criteria Model 1 Model 2
Processing requests and
Are used for the
JSP page responsibility sending back replies to
presentation layer
clients
Are used for processing
Servlets responsibility N/A
task
Developing an application
Nature of Developer’s Task Quick prototyping that can be modified and
maintained
View and Controller is View and Controller is
Who is doing the work? developed by the same developed by the different
team teams
Java Server Pages
Need for JSP
• Servlet
– Is a java class that must be compiled to deploy on server
– Low level HTML format that does not focus on the
presentation logic (very complex) of the web
application
– Is not flexible in modify with editor program
→ Need the replaced thing that is easily focus on the
presentation logic and approaches the non-
experience presentation developer
Java Server Pages
JSP
• Java Server Page (JSP) is a server side script language
running web (application) server (Tomcat, Sun, JBoss …)
• Saved with .jsp extension
• A simple, yet powerful Java technology for creating and
maintaining dynamic-content webs pages (embedded)
• JSP page are converted by the web container into a Servlet
instance
• It focus on the presentation logic of the web application
• JSP page contains HTML tags
• JSP page contains tags (standard & custom), which are used
to generate dynamic content and invoke the operations on
Javabeans components and processing requests.
• A combination of HTML, XML, Servlet (extends from
Servlet), and Java Code to create dynamic Web content.
Java Server Pages
• Benefits JSP
– Segregation of the work profiles of a Web designer and a Web
developer (separating presentation logic and
content/business/processing logic)
– Emphasizing Reusable Components (JavaBeans)
– Simplified Page Development (easy to use JSP through tag,
flexibility, scalability)
– Access & instantiate JavaBeans component (support tag element
with get/set functions)
– High secure
• Choosing Servlet or JSP
– Servlet are well suited for handling binary data dynamically
• Ex: for uploading files or for creating dynamic images, since they need not
contain any display logic
– JSP is easy to create the presentation logic with dynamic
generated data combine template and do not compile before
executing or running time
Java Server Pages
JSP – Example
• Simple JSP page displays current date.
<html>
<head>
<title>A simple date</title>
</head>
<body>
The time on the server is <%= new java.util.Date() %>
</body> View Source
</html>
• Server processes JSP components converting static data on
HTML which can be displayed by Web browser
• To testing JSP page, the JSP page should be copied to the
ROOT of web server – Tomcat
Java Server Pages
JSP – In Nature
public void
_jspService(HttpServletRe
quest request,
HttpServletResponse
response)
throws
java.io.IOException,
ServletException {
Java Server Pages
JSP – In Nature
• When the JSP page is requested to server, the JSP page is
converted to java file as filename_jsp.java (filename.jsp)
• The filename_jsp.java file is complied if it is correct syntax
• Ex:
– Omit the “)” of Date function in the simpleDate.jsp
– Correct above mistake, run the file, then checking the result at
• C:\Documents and Settings\LoggedUser\Application Data\NetBeans\
7.4\apache-tomcat-7.0.41.0_base\work\Catalina\localhost
• C:\Users\LoggedUser\AppData\Roaming\NetBeans\7.4\
apache-tomcat-7.0.41.0_base\work\Catalina\localhost
Java Server Pages
JSP
Java Server Pages
JSP Life Cycle
HTTP
WEB JSP
Client SERVER ENGINE Database
JSPs are
processed here
JSP Files
Java Server Pages
JSP Life Cycle
Java Server Pages
JSP Life Cycle
Servlet from JSP
Request
Response _jspService()
Declarations
jspDestroy
JSP Elements Example
JSP Elements
Example
Expression!!!
Day 7, 8, 9 – Search
Break Down
• Complication
– The JSP page is automatically compiled and
executed again by JSP/ Servlet Engine
• Execution
– Is carried out with the help of page directives
controlling various execution parameters and
are used for buffering output and handling
errors
JSP Elements
Overview
• Enables to create dynamic JSP pages
• The JSP server translates and executes JSP elements
Elements Description
Root Classifies standard elements and attributes of namespaces in
tag library
Comment Used in JSP file documentation
Declaration Declares variables and methods in a scripting language page.
Expression Includes expression in a scripting language page
include Directive Includes content of one JSP page into the current JSP page
JSP Elements
Overview
Elements Description