Professional Documents
Culture Documents
offline
Online(WebApplicaton)
Presentation Logic: -Listens to the User Input -Renders The Response -Process & Present It Bussiness Logic:
2-Tier Architecture
Client
Server
3-Tier Architecture
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:
Middleware server
Database Server
Let us look
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
CGI
in this approach ,the server creates and destroys the process for every request, Which increases the work load
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
MVC
M-MODEL (java Bean)
V-VIEW (JSP,HTML)
C-CONTROLLER (SERVLET,JSP)
Model-1 Architecture
Model-2 architecture
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
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
Application server
An application server is a server which provides software
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
Servlet API
Classes
Interfaces
into
javax.servlet javax.servlet.http
Very Important
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
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
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
servlet
ServletContainer Calls the Servlet.init(ServletCofnig)
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)
Servlet.destroy();
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 } }
Steps: Creating the home.html Creating the LifeCycleServlet Creating deployment descriptor Deploying 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.
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
Enumeration getParameterNames()
EXAMPLE:
Initialization parameters
<init-param> <param-name>xxxxx</param-name> <param-value>xxxxx</param-value> </init-param>
getServletConfig().getInitParameter(name);
getInitParameterNames();
<context-param>
<param-name>xxxxx</param-name> <param-value>xxxxx</param-value>
</context-param>
ServletContextObject.getInitParameter(name); getInitParameterNames();
Request Dispatching
Request Delegation
include
forward
URL Based
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)
Filter Config
FilterChain