150 likes | 179 Views
Advanced Java. Presented by P. Ravindra Lecturer, Dept. of Computer Science. * Servlet life cycle methods.
E N D
Advanced Java Presented by P. Ravindra Lecturer, Dept. of Computer Science
* Servlet life cycle methods Servlet runs on wed server . The web server is responsible for initializing ; invoking & destroying each servlet instance A web server communicate with a servlet through a simple interface javax.servlet.Servlet the interface contains 3 main methods • init() • service() • destroy() Along with these 3 main methods. There are to more methods for supporting clients. • getServletConfig() • getServletInfo()
init():- • init() is used for initializing the servlets in this method we can write operations like opening a file, establishing database connections with servers& etc., the init() method executes only one’s when the servlet is first loaded. It won’t be called again & again. • service():- • the service() method is the heart of the servlet. Each request message from a client results in a single call to the servlet service() method. The service() method reads the request & produces the response message with from two parameters. • A ServletRequest object with data from the client. The data consist of name and values pairs as a parameter. • A ServletResponse reply back to the client . When preparing a response to the client the method setContentType() is called first for setting MIME type. • syntax:- • public void service(HttpServletRequest req, HttpServletResponse res) • { • // statements; • } • The methods doGet() & doPost() one used in the place of service() method.
Syntax:- doGet()public void doGet(HttpServletRequest req, HttpServletResponse res) { //statements; } Syntax:-doPost public void doPost(HttpServletRequest req, HttpServletResponse res) { //statements; } destroy():- The destroy() method is called to allow the servlet to cleanup any resources such as open files or open database connections before the servlet is unloaded. Syntax:- public void destroy() { //closing statements; }
*Servlet API packages Java servlet API define the interface between servlet & servers. They are two types of interfaces. • Javax.servlet • Javax.servlet.Http the API supports us in four catagories. • These are used for servlet life cycle management. • It is used to access servlet context. • These will provide a support for Http protocals. • These will provide utility classes.
*Creating servlet We can create a servlet in 3 ways • By implementing servlet interface • By inheriting the GenericServlet class • By inheriting the HttpServlet class the HttpServlet class is widely used to create servlet because it has the methods to handle HttpRequest such as doGet() & doPost()
* Directory Structure Web-app (context-Root) Web-INF classes Class files Web.xml HTML lib Static resources
*cookie A cookie is a named piece of data mentioned by a browser sent by web server n a communication. It is normally mentioned for session management. HTTP connections are stateless; a cookie across web server & web client; a cookie can store information across multiple HTTP connection. The advantages of maintaining cookie in the client computer is because if can store information & the user computer for later retrival .For example if the user doing online shopping & a cookie can store the users interesting searches & it will show the related items to the user with out his knowledge in the next communication. cookie constructor:- cookie(string name, string value)
* Session tracking The HTTP protocal is a stateless protocal; it doesn’t store any information. It is necessary to save the status information. This can be done by using session mechnism. It is available in HttpSession interface
* Servlet chaining • If a client request is processed by group of servlet are known as servlet chaining.In order to process a client request by many number of servlet we have two models. • forward model • include model • forward model:- • in this model we have to forward a request to a group of servlets; finally we get the result for destination servlets but not from the intermediary servlet. • Syntax:- • RequestDispatcher rd=request.getRequestDispatcher(“servlet2”); • rd.forward(request,response); • include model:- • if a single client request is passed to a servlet & than the servlet makes use of other group of servlet to process a request by including the group of servlets into a single servlet • Syntax:- • RequestDispatcher rd=request.getRequestDispatcher(“servlet2”); • rd.include(request,response);
* Components of JSP JSP page is a combination of the following components .They are *Directives Syntax:- <%@ directive attribute=“value” %> *JSP Declaratives Syntax:-<% //declare Variables %> *Scriplets Syntax:-<% //java code %> *Expressions Syntax:-<%= //any expressions %> *Standard Action Syntax:-<jsp:include page=“filename”/> *Custom tags Syntax:-<%@ tagliburi=“path” prefix=“value”%>