0% found this document useful (0 votes)
19 views

Java Web Technologies

Uploaded by

Chavini Hewage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views

Java Web Technologies

Uploaded by

Chavini Hewage
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 39

Object Oriented Programming

Object Oriented Programming

Week 09
Java Web Technologies

SLIIT - Faculty of Computing


Object Oriented Programming

Learning Outcomes

At the end of the Lecture students should be


able to get details of a few specific things
related to the Java web technologies.
– MVC architecture
– JSP
– Servlet
– Maven Building tool

2
SLIIT - Faculty of Computing
Object Oriented Programming

What is a Web Application


Framework?
A web application framework is a piece of
structural software that provides automation
of common tasks of the domain as well as a
built- in architectural solution that can be
easily inherited by applications implemented
on the framework.

SLIIT - Faculty of Computing


Object Oriented Programming

Common Tasks of the Web


Application
• Binding Request Parameters to Java Types
• Validating Data
• Making calls to business logic
• Making calls to the data layer
• Rendering presentation layer (HTML, …)
• Providing internationalization and localization
• Keep Presentation Layer Separate from Data Layer

SLIIT - Faculty of Computing


Object Oriented Programming

MVC

• The model view controller pattern is the


most used pattern for today’s world web
applications
• It has been used for the first time in
Smalltalk and then adopted and popularized
by Java
• At present there are more than a dozen PHP
web frameworks based on MVC pattern

SLIIT - Faculty of Computing


Object Oriented Programming

MVC Cont..
• The model does all the computational work
– It is input/output free
– All communication with the model is via
methods
• The controller tells the model what to do
– User input goes to the controller
• The view shows results; it is a “window” into
the model
– The view can get results from the controller, or
– The view can get results directly from the model
SLIIT - Faculty of Computing
Object Oriented Programming

SLIIT - Faculty of Computing


Object Oriented Programming

Applying MVC to Web


Applications
• View:
– HTML form; native Java interface; client-side
script; applet

• Controller:
– Java servlet; session Bean

• Model:
– Entity Bean or other business logic object
SLIIT - Faculty of Computing
Object Oriented Programming

Interchangeable Elements
• View:
– HTML form becomes touch-screen

• Controller:
– JSP becomes session bean

• Model:
– Entity Bean
• Views and Controllers closely interact (HTML/JSP)
• If HTML code is written out entirely through JSP, the Controller and
View (conceptually) merge
• A Controller-View pair works with one Model
• One Model may have multiple Controller-View pairs

SLIIT - Faculty of Computing


Object Oriented Programming

MVC Advantages

• Single point of entry to Model object


• Multiple-client support
• Design clarity
• Modularity
• Controlled growth
• Portable
• separation of concerns

SLIIT - Faculty of Computing


Object Oriented Programming

What is a Java Server Page (JSP)

• Java Server Pages (JSP)


– A simplified, fast way to create dynamic web content
– HTML or XML pages with embedded Java Code or Java Beans
– Can be a mix of template data in HTML/XML with some dynamic
content
• Java Server Pages are HTML pages embedded with snippets of
Java code.
– It is an inverse of a Java Servlet
• Four different elements are used in constructing JSPs
– Scripting Elements
– Implicit Objects
– Directives
– Actions

SLIIT - Faculty of Computing


Object Oriented Programming

JSP Example

SLIIT - Faculty of Computing


Object Oriented Programming

How JSP works

SLIIT - Faculty of Computing


Object Oriented Programming

Why to use JSP?

• High Performance
• Has access to all the powerful Enterprise
Java APIs, including JDBC, JNDI, EJB, JAXP
• can be used in combination with servlets

SLIIT - Faculty of Computing


Object Oriented Programming

Scripting Elements

• There are three kinds of scripting


elements
– Declarations
– Scriptlets
– Expressions

SLIIT - Faculty of Computing


Object Oriented Programming

Declarations

• Declarations are used to define methods & instance


variables
– Do not produce any output that is sent to client
– Embedded in <%! and %> delimiters
Example:
<%!
Public void jspDestroy() {
System.out.println(“JSP Destroyed”);
}
Public void jspInit() {
System.out.println(“JSP Loaded”);
}
int myVar = 123;
%>
– The functions and variables defined are available to the JSP Page
as well as to the servlet in which it is compiled

SLIIT - Faculty of Computing


Object Oriented Programming

Scriptlets
• Used to embed java code in JSP pages.
• Contents of JSP go into _JSPpageservice()
method
• Code should comply with syntactical and
semantic constuct of java
• Embedded in <% and %> delimiters
– Example:
<%
int x = 5;
int y = 7;
int z = x + y;
%>

SLIIT - Faculty of Computing


Object Oriented Programming

Expressions

• Used to write dynamic content back to


the browser.
– If the output of expression is Java primitive
the value is printed back to the browser
– If the output is an object then the result of
calling toString on the object is output to
the browser
– Embedded in <%= and %> delimiters
Example:
– <%=“Fred”+ “ “ + “Flintstone %>
prints “Fred Flintstone” to the
browser
– <%=Math.sqrt(100)%> SLIIT - Faculty of Computing
Object Oriented Programming

JSP Actions

• Processed during the request processing phase.


– As opposed to JSP directives which are processed
during translation
• Standard actions should be supported by J2EE
compliant web servers
• Custom actions can be created using tag libraries
• The different actions are
– Include action
– Forward action
– Param action
– useBean action
– getProperty action
– setProperty action
– plugIn action

SLIIT - Faculty of Computing


Object Oriented Programming

JSP Actions - Include

• Include action used for including resources in a JSP


page
– Include directive includes resources in a JSP page at
translation time
– Include action includes response of a resource into
the response of the JSP page
– Same as including resources using
RequestDispatcher interface
– Changes in the included resource reflected while
accessing the page.
– Normally used for including dynamic resources
• Example
– <jsp:include page=“inlcudedPage.jsp”>
– Includes the the output of includedPage.jsp into the
page where this is included.
SLIIT - Faculty of Computing
Object Oriented Programming

JSP Actions

SLIIT - Faculty of Computing


Object Oriented Programming

JSP Actions - Param


• Used in conjunction with Include & Forward actions to
include additional request parameters to the included
or forwarded resource
• Example
<jsp:forward page=“Param2.jsp”>
<jsp:param name=“FirstName” value=“Sanjay”>
</jsp:forward>
– This will result in the forwarded resource having an
additional parameter FirstName with a value of
Sanjay

SLIIT - Faculty of Computing


Object Oriented Programming

Syntax
• Scriplet – nest Java code

• Declaration Tag – define variables

• Expression Tag – print java variables

• Comment Tag – add comments

SLIIT - Faculty of Computing


Object Oriented Programming

Simple JSP Loop

SLIIT - Faculty of Computing


Object Oriented Programming

What is a Servlet?
• Servlets are small programs that execute on the server
side of a web connection
• Servlets dynamically extend the functionality of a web
server
• Java Servlets/JSP are part of the Sun’s J2EE Enterprise
Architecture
– The web development part
• Java Servlet
– is a simple, consistent mechanism for extending the
functionality of a web server
– Are precompiled Java programs that are executed on
the server side.
– Require a Servlet container to run in
– Portable to any java application server

SLIIT - Faculty of Computing


Object Oriented Programming

Why to Use Servlets?


• Work well in a Heterogeneous Environments
– OS and platform neutral
– Work with all major web servers (IIS, Apache,etc..)
• Well defined Web Architecture framework
– Standard built in services such as:
• Standard Approach to Authentication using
declarative security vice programmatic security
• Database connection pooling
• Complete support for sessions via cookies and/or
URL re-writing
– Has automatic fallback to URL re-writing

SLIIT - Faculty of Computing


Object Oriented Programming

How servlet works

SLIIT - Faculty of Computing


Object Oriented Programming

The Life Cycle of a Servlet


• A servlet life cycle can be defined as the
entire process from its creation till the
destruction. The following are the paths
followed by a servlet
• init()
• service( )
• destroy( )
• doGet()
• doPost()

SLIIT - Faculty of Computing


Object Oriented Programming

The init() Method


• The init method is called only once
when the servlet is created
• When a user invokes a servlet, a single
instance of each servlet gets created,
with each user request resulting in a
new thread that is handed off to doGet
or doPost as appropriate.

SLIIT - Faculty of Computing


Object Oriented Programming

The service() Method


• The service() method is the main method to
perform the actual task
• The servlet container (i.e. web server) calls the
service() method to handle requests coming
from the client( browsers) and to write the
formatted response back to the client.
• Each time the server receives a request for a
servlet, the server spawns a new thread and
calls service
• The service() method checks the HTTP request
type (GET, POST, PUT, DELETE, etc.) and calls
doGet, doPost, doPut, doDelete, etc. methods
as appropriate.

SLIIT - Faculty of Computing


Object Oriented Programming

The destroy() Method


• The destroy() method is called only once at the
end of the life cycle of a servlet.
• method gives your servlet a chance to close
database connections, halt background
threads, write cookie lists or hit counts to disk,
and perform other such cleanup activities
• After the destroy() method is called, the servlet
object is marked for garbage collection.

SLIIT - Faculty of Computing


Object Oriented Programming

The HttpServlet class


• HttpServlet class servers client’s HTTP
request
• For each of the HTTP methods, GET,
POST and other corresponding methods
– doGet()…. – Serves HTTP GET request
– doPost()..- Servers HTTP POST
request

– The servelet usually must implement


one of the first two methods or the
service().. method
SLIIT - Faculty of Computing
Object Oriented Programming

The HTTPServeltRequest Object


• Contains the request data from the
client

– HTTP request headers

– Form data and Queary parameters

– Other client data(cookies, path, etc.)

SLIIT - Faculty of Computing


Object Oriented Programming

The HTTPServeltRespond Object


• Encapsulate data and send back to the
client
– HTTP response headers(Content type)

– Response Body

SLIIT - Faculty of Computing


Object Oriented Programming

The most important servlet


functionalities
• Retrieve the HTML Form parameters
from the request(Bothe get and post
parameters)

• Retrieve a servlet initialization


parameter

• Retrieve HTTP request header information


SLIIT - Faculty of Computing
Object Oriented Programming

Building tool- Maven


Maven is really a process of applying
patterns to a build infrastructure in
order to provide a coherent view of
software projects.

Provides a way to help with managing:


• Builds
• Documentation
• Reporting
• Dependencies
• Software Configuration Management
• Releases
• Distribution
SLIIT - Faculty of Computing
Object Oriented Programming

Why to use maven


• Make the development process visible
or transparent
• Provide an easy way to see the health
and status of a project
• Decreasing training time for new
developers
• Bringing together the tools required in a
uniform way
• Preventing inconsistent setups
• Providing a standard development
infrastructure across projects SLIIT - Faculty of Computing
Object Oriented Programming

Maven Architecture

Plugin
e.g. jar

Projects Plugin
Maven Core
to build e.g. surefire

Plugin
e.g. release

Local machine Remote repository or local install

SLIIT - Faculty of Computing


Object Oriented Programming

Common project metadata


format
• POM = Project Object Model = pom.xml
• Contains metadata about the project
• Location of directories,
Developers/Contributors, Issue tracking
system, Dependencies, Repositories to use,
etc
<project>
• Example:
<modelVersion>4.0.0</modelVersion>
<groupId>org.codehaus.cargo</groupId>
<artifactId>cargo-core-api-container</artifactId>
<name>Cargo Core Container API</name>
<version>0.7-SNAPSHOT</version>
<packaging>jar</packaging>
<dependencies/>
<build/>
[…]
SLIIT - Faculty of Computing

You might also like