290 likes | 504 Views
Servlet. A servlet is a java program that programmatically extends functionality of a web server in request response programming model . Servlet are executed with in a web server and generate dynamic contents. Web application of web site can be of two types 1) static or 2) dynamic
E N D
Servlet A servlet is a java program that programmatically extends functionality of a web server in request response programming model . Servlet are executed with in a web server and generate dynamic contents. Web application of web site can be of two types 1) static or 2) dynamic A static web site contains information in the form of HTML page. Where as a dynamic web site contains HTML as well as program .these program are responsible for generating HTML,
At the time of a request.there are diffrerent technology that facilitates dynamic content generation in a web application . Most commonly use among them are • 1) Servlet & Jsp-sun micro system • 2) ASP & Asp .net-MicroSoft • 3) php- open source.
Working of a static website Static HTML page Client Web Server Request for a static resource Resource of search Its contents are sent as response or error code is returned Contents of requested Resource or an error code
Working of a dynamic web site Client Web server Request for a dynamic resource Program responsible for generating dynamic contents & executed contents are generated Dynamically generated contents are sent by the web server as response
Before servlet server side dynamic content were generated using CGI (Common gateway Interface)script. • CGI based web application had two problems:- • 1) platform dependent • 2) process based execution of CGI script( i. e. performance). • Servlet API solves both these problems of CGI API. Servlet API provides various Interfaces that determines the mode of communication between a servlet & web server .
Mode of interaction between CGI Script & webserver has determined by a standard interface named COMMON GATEWAY INTERFACE Web Server A program written in C,C++, perl that uses CGI to interact with web server CGI Script
Some of these interfaces are implemented by web server or application server vendor & others are implemented by the web application developer. • Servlet API is defined in javax.servlet and its sub packages. • Most commonly used interfaces of servlet API are: • 1) Servlet(is to implemented by application developer ) • 2)ServletRequest • 3)ServletResponse • 4)RequestDispatcher • 5) Servletconfig • 6) ServletContext (2-6) interface implemented by vendor
Life Cycle of Servlet • Servlet interface provide life cycle methods of a servlet .it must be implemented by each servlet class .it has following life cycle method: • Init(): Syntex: public void init(Servlet config) • Service(); Syntex: public void service(ServletRequest request, ServletResponse response)throws ServletException,IoException.
Webserver Request for dynamic resource Client 1.1 1.2 1.3 Servlet 1.7 1.5 1.6 ServletRequest ServletResponse 1.4 Service( , );
1.1 servlet request object is created for the request. • 1.2 servlet response object is created for the response. • 1.3 data received as part of request is stored in servlet request object. • 1.4 service method is invoked on the servlet , ,reference of servletrequest & servletresponse is provided as arguments of servlet. • 1.5 in service method ,servlet read data from servletRequest . • 1.6 servlet stores dynamically generated contents to the servletResponse object . • 1.7 when service method terminates web server sends the contents of servletresponse object & response to the client.
3) destroy(): public void destroy(); • Apart from these life cycle method servlet interface has two non-life cycle method that can be used by the application developer. • 1) getServletConfig(): • public servletConfiggetServletConfig(); • getservletInfo(): • Public String getservletinfo();
Development of Servlet • If you create a servlet class that implements servlet interface and provides implementation of all the life cycle methods is to be created. • Servlet • | • | • YourServlet
Javax.servlet.GenericServlet class provides default implementation of servlet interface that provide conveintmechanism to define a servlet. • servlet • | • GenericServlet • | • Your Servlet • (Implementation of servlet through generic servlet)
The problem with GenericServlet class is that it is protocal independent it does not provides methods to handle request specific to a protocal . • for example: on internet http: protocol is used for communication between clients & web server . • This protocal supports different type of request for different task . • javax.servlet.http.HttpServletRequest class extends GenericServlet class and provides methods to handle http specific request. Most coomonly methods used are • doGet(); doPost();
Which are used to handle http get & post request respectively. • Public void doGet(HttpServletRequest request ,HttpServletResponse response)throws servletException, IOException. • Public void doPost(HttpServletRequest request ,HttpServletResponse response)throws servletException, IOException.
servlet • | • GenericServlet • | • HttpServlet (service() • | doGet() • | doPost() • | doDelete()) • | • Your Servlet
HttpServletRequest & HttpservletResponse interfaces extends servletRequest & ServletResponse interfaces respectivly& add additional http specific Functionality to them such as support of cookies , session object. Difference between Http get & post request 1) conventionaly get request is used to request a static resource & post request is used to request a dynamic resource. 2) Technically in case of get request , request parameters are send as part of request header and size of header of http package being limited only limited of data can be sent as request parameter.
2) in case of post request request parameter are sent as part of request body the size of http packet body being unlimited ,i.e. unlimited amount of data can be sent as request parameter. • 3) in case of get request name and value of all request parameters are shown in the address bar of the browser becoze in case of case request ,request parameter are appended ,in case of post request name & value of request parameters are not shown in the address bar of the browser.
Java Beans • A java bean represent general purpose reusable software component developed in java. • A java bean is represented by a set of java classes . That have the following characteristic • 1) all the class representing the bean are defind in package. • 2) Bean class provides a default constructor. • 3) Bean class exposes getter & setter method to obtain or change value of its properties. • In a web application a jav bean encapsulate either
Busineess logic persistence logic in a component that can be reuse in the application. • Program • package beanpack; • Import java.sql.*; • Public class RegisterBean • { • Public String Name,Password,MaiLID; • Public String getName() • { • Return name; • }
Public void setName(String n ) • { • Name=n; • } • Public String getPassword() • { • Return password; • } • Public void setPassword(String p) • { • Password=p; • }
Public String getMailId() • { • Return mailID(); • } • Public void setMailId(String m) • { • mailId=m; • }
Public boolean register() • { • Try • { • Class.forName(“Oracle.jdbc.driver.OracvleDriver”); • Connection con=DriverManger.getConnection(“jdbc:oracle:thin:@localhost:1521:Xe”,”system”,”oracle”); • PreparedStatment stmt=con.preparedStatement(“insert into login details value(? ? ?)”); • Stmt.setString(1,name); • Stmt.setString(2,password); • Stmt.setString(3,mailID); • Stmt.executeUpdate(); • Con.close(); • Return true; • }catch(Exception e) • { • Return false; • } • } • }
EJB ,entity bean ,session bean • EJB are distributed components that represents either business logic or application data .EJB are executed with in specific run time environment called EJB container.main power of EJB lies in the services that are provided to EJB . By the EJB container provide the following services of EJB • 1. object management & pooling • 2. Transaction management • 3. state management • 4. Persistent management • 5. Authitication • 6. Auditing
TYPES OF EJB • EJB can be of following three types : • 1) Session bean 2) Entity Bean 3) Message Driven bean • 1) session bean represents business logic of a enterprise application as a distributed component ,for each client a distinct session bean object is maintained by the EJB container. • Session bean can be of two types : • 1) stateless 2) statefull • In stateless session bean state of the object is not maintained between different invacation by the client where as state full session bean maintain its
State through its usage by client. • ENTITY bean represents data of an application entity bean provides object representation of persistent data each entity bean object . Entity bean object represents a record stored in a relational data base system. An entity bean object is shared by all client that are interested in the data presented by the bean. Depending upon responsibility of providing persistent logic. • Entity bean can be of two types: • BMP(Bean Management Persistent) • CMP(Container Management persistent)
In case of BMP bean developer provides persistent logic where as in case of CMP it is provided by the container. • Disadvantages of servlet • 1) Dynamic content generation & presentation is intermixed that is all the HTML is return to the outputStream by servlet programmer. It causes maintenance problem that is if presentation is to be change servlet has to be modified and recompile .
JSP API • JSP API facilitate development & processing of Jsp pages following interfaces & classes are provided by sun microsystem . That are implemented by the vendor and are use by the application developer . Javax.servlet.jsp and its su packages provides JSP API javax.servlet.jsp interface provides life cycle methods of a JSP page. It declare two method • Public void jspInit(); • Public void jspDestroy();
servlet • | • Jsp page • | • Http JSpPage • | • JSP Base on Http JspBase • | • JSP servlet.class • generated .