You are on page 1of 57

Applications

offline

Online(WebApplicaton)

Architectures 2-tier Traditional 3 Tier

WEB Architecture ( 3-Tier) Model-1


Model-2 MVC

Presentation Logic: -Listens to the User Input -Renders The Response -Process & Present It Bussiness Logic:

-Implementing the Business Rules


Persistence Logic: -The logic that persists the Application data (Persistence=The data which outlives the process in which it is created)

2-Tier Architecture
Client

Server

PROBLEMS with 2-tier Architecture:


Increasing the complexity of the application Difficulty in modifying the Presentation Logic as it Combines With Business Logic Providing services to different types of clients is difficult (re writing the business logic acc Security problems for Business logic

Problems with installing Business logic in Client environment


installing resources in Client Machine is increases the weight increases the cost Limits the usage of application as the resources not available at client side no of users restriction

3-Tier Architecture

Client Middleware Server

Server

BENEFITS: Reuse of Business logic with Different Types of Clients Better Management of Shared Business Data Fewer Resources Required in the Client environment Better performance and availability of the application by managing the access of back-end resources in the middleware server Problems: inability of the client application to be installed in the client environment For example VB client cannot be installed on non-windows applications inability of the client network to support the protocol used to Communicate between the client-server and middle ware server

WEB-Architecture
Requirements:

General purpose client applications, such as web browsers


also known as web clients Common protocols such as HTTP Servers known as Web Servers

Benefits of Web Applications:

Web Applications do not Require the installation of any Specific


Client software on client computers in case of offline applications if newer version is available we have update the installed applications Incase of web applications instead of providing upgrades to

individual users ,the latest version is available from the


internet directly unlike Traditional 3-tier applications , web applications are

accessible any time and any where through the web


web applications are platform independent

Technologies Used in Web Applications


Client

HTML Java Script AJAX J-Query


JAVA Servlet JSP JDBC ASP .NET PHP SQL,PL-SQL Oracle, DB2 MYSQL

Middleware server

Database Server

Let us look

JDBCspecification from SUN provides the abstraction in the form of APIs or

protocols to communicate with the


Relational Databases overcomes the problems with Native API and ODBC

Need for Servlet: The main requirement is Provide Dynamic Content to the Client Need to develop the applications that can integrated with web server , run , process and provide dynamic content to the client

These requirements initially taken care by web server providers Problems: vendor dependent migratining the server is difficult

need to learn vendor specific API to develop applications


these problems solved by CGI-(Common gate way of Interface)

CGI

Is an open abstraction between web server and external application


Benefits Simplicity Open Standards Problems:

in this approach ,the server creates and destroys the process for every request, Which increases the work load

Request processing time increases


Low performance

JAVA SERVLET An alternative to CGI Platform Independent Server Independent To develop the applications which extend the functionality of web servers

Benifits
Request processing time is less Migration to any server is easy Once loaded we can call as many times as needed

JSP-Java Server Pages Best useful to Presentation Logic

separates business logic from presentation logic


supports Tag libraries

Will see in detail later ::::

MVC
M-MODEL (java Bean)

V-VIEW (JSP,HTML)

C-CONTROLLER (SERVLET,JSP)

Model-1 Architecture

Model-2 architecture

Questions.???????? 1) What is a web application?

2) CGI stands for?


3) What is CGI? 4) State the benefit of CGI

5) What is Model-1,Model-2,MVC architectures


6) What lead to the introduction of java servlets

Servlet:
-simple java class which is dynamically loaded on a web server and therefore , Enhances The Functionality of the web server -Provides Vendor independent mechanism for extending the web server functionality

Need For Server Side Enhances:


-to provide dynamic content after processing the request -vendor API is allows us to become vendor dependent -CGI low performance & increases the server burden -Java Servlet application can run on any web server integrated with servlet container

WebServer
The primary function of a web server is to deliver web pages on the request to clients using the Hypertext Transfer Protocol (HTTP). This

means delivery of HTML documents and any additional content that


may be included by a document, such as images, style sheets and scripts.

Application server
An application server is a server which provides software

applications with services such as security, data services, transaction


support, load balancing, and management of large distributed systems

EXPLORING SERVLET THE SERVLET CONTAINER


-Provides Runtime Environment for Java Servlet Components Provides

Net Work Services over which the request and response sent

Manage Servlet Life Cycle Resource Management- Manages the static and Dynamic resource Html,servlet,jsp Security service-Handles authorization and authentication of resource Session management
access

Describing Request Processing Mechanism:


-Browser Webserver ServletContainer

Web.xml Processing Request & Generates the Response

Invokes appropriate Servlet Based on URL

Introducing the SERVLET API

Servlet API

Classes
Interfaces

into

javax.servlet javax.servlet.http

Implementing a Servlet Object

Very Important

Should Be a Public & Non-Abstract

Class
Should Be a subtype of

javax.servelt.Servlet interface
Should support a no-argument constructor

javax.servlet.Servlet Interface
Designed to describe the user defined java object to the Servlet Container Methods: Void init(servlet config) Void service(ServletRequest,ServletResponse) Void Destroy() ServletConfig getServletConfig()

Java.lang.String getServletInfo()

javax.servlet.ServletConfig Interface
Designed to describe a container object that represent environment details of the Servlet object

Provides an abstraction to the Servlet Object to get the environment


details from the ServletContainer. Gets the Initialization Parameters Configured in web Applications

configuration document(web.xml)
Gets the ServletContext object that describes the web applications runtime environment Gets the Servlet Name as configured in the web.xml for which the servlet object is created

Understanding Servlet LIFE CYCLE:


The servlet Life Cycle Mainly goes through four stages:

Loading a servlet Initializing the servlet

Request Handling
Destroying the servlet

loading

Loading a Servlet
initialising

Loading

Initializing context 0 or +ve value to Load-on-Startup in web.xml on configure Webserver determines that this servlet is needed to process request
Not hosted in distributive 1-instance Not implemented SingleThreadModel hosted in distributive Implemented SingleThreadModel hosted in distributive & Implemented SingleThreadModel 1-instance per JVM Multiple instances Multiple instances per JVM

Instantiation

Initializing the Servlet

ServletContainer creates one ServletConfig object per one

servlet
ServletContainer Calls the Servlet.init(ServletCofnig)

If fails to initialize throws


UnavailableException(String msg) UnavailableException(String msg,int seconds)

Handling Request
Creates ServletRequest,ServletResponse objects Incase if it is HTTP request webcontainer creates Creates HttpServletRequest, HttpServletResponse objects (req,obj encapsulates the request header,user data) (res.obj allows servlet to write response data)

Then it invokes the


Servlet.service(ServletRequest,ServletResponse) method

Destroying the Servlet


It allows all threads currently running in the service method of the Servlet Instance to complete their jobs and get released Then it calls the destroy()

Servlet.destroy();

Exploring the servlet Life Cycle Methods The init() method :


Invoked immediately after the servlet object is Created

This method can throw ServletException to intimate the container that the initialization process has failed.

Code snippet:
public class MyServlet implements Servlet { public void init(ServletConfig config) throws ServletException { //initialisation code } }

The service() method : This method is invoked only on servlet instance is initialized successfully The servlet container invoked this method only when a client request mapped to the servlet ServletRequest and ServletResponse objects are used to process the request and send the response Code snippet: public class MyServlet implements Servlet { public void service (ServletRequest req,ServletResponse res) throws ServletException,IOException { //initialisation code } }

Getting Started with first Example:


//Develop a Servlet Application which demonstrates the Lifecycle of the Servlet

Steps: Creating the home.html Creating the LifeCycleServlet Creating deployment descriptor Deploying the Application

Running the Application

Generic Servlet
As subtype of javax.servlet.Servlet interface it must implement all the 5 methods. Servlet API provides an adapter class, javax.servlet.GenericServlet which implements all the methods in Servlet interface Except service() It provides init() with no arguments.

Getting Started with Example:


Steps:
Creating the home.html Creating the HelloServlet, deployment descriptor

Deploying the Application


Running the Application

Questions?????
1) What is servlet? 2) Define servlet container? 3) Which packages are used to define servlet API? 4) Which method is used to initialize the servlet? 5) Name the Servlet Life cycle methods? 6) What is Generic servlet?

Client uses request message to send a request to the server Request message contains data such as headers and message content Server process the request and sends it back as a response http is used to exchange the request and response ServletRequest Interface: As a throwaway object Servlet Object pool

Methods: String getProtocol String getScheme() . . . . . . . .

Working with Request Parameters:


String getParameter(String name) String []getParameter/values(String name)

Enumeration getParameterNames()
EXAMPLE:

Create the login form


Create the loginservlet Create deployment descriptor Run the application

Working with Initialization Parameters


Values may not available at the development time Hard coding in servlet leads to frequent changes(scott , tiger) Solution

Initialization parameters
<init-param> <param-name>xxxxx</param-name> <param-value>xxxxx</param-value> </init-param>

getServletConfig().getInitParameter(name);
getInitParameterNames();

Getting Started with Example:InitParams


Steps: Creating the home.html Creating the InitParamsServlet Creating deployment descriptor Deploying the Application Running the Application

Context Initialization parameters


If more than one servlet requires the same info

<context-param>
<param-name>xxxxx</param-name> <param-value>xxxxx</param-value>

</context-param>
ServletContextObject.getInitParameter(name); getInitParameterNames();

Getting Started with Example:ContextParams


Steps: Creating the home.html Creating the login.html Creating the register.html RegistrationServlet LoginServlet Creating deployment descriptor Deploying the Application

Running the Application

Request Dispatching

Request Navigation (locating the web resource)

Request Delegation

include

forward

URL Based

Using Servlet Name

Context Relative Path Request Relative Path

Getting Started with Example:RequestDispatching


Steps: Create the home.html Create the ValidateServlet Create the AddServlet Create the SubServlet Create the ResultServlet Create deployment descriptor Deploying the Application

Running the Application

HTTP: Hyper Text Transfer Protocol


Error Codes

1xx-Informational 2xx-Success 3xx-Redirectional 4xx-Client error 5xx-Server error

Work-out Get all the HTTP Status Codes From NET


Some imp codes:

404-Not found 500-Internal server Error 503-service unavailable 505-HTTP Version not supported

HttpServlet
Servlet container provides support for HTTP Protocol HttpServletRequest object HTTP protocol based info Headers request methods HttpServletResponseobject HTTP protocol based info

Methods Protected void service(HttpServletRequest,HttpServletResponse) Protected void doGet(HttpServletRequest,HttpServletResponse) Protected void doPost(HttpServletRequest,HttpServletResponse) Protected void doPut(HttpServletRequest,HttpServletResponse) Protected void doHead(HttpServletRequest,HttpServletResponse) Protected void doDelete(HttpServletRequest,HttpServletResponse) Protected void doOptions(HttpServletRequest,HttpServletResponse)

Session Collection of HTTP requests over the period of time


Session tracking is means of keeping session data

Filters Listeners - Wrappers


Filter

Filter Config

FilterChain

You might also like