Unitwwsbdsbsdbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb-4.pptx

VikasTuwar1 8 views 13 slides Jun 06, 2024
Slide 1
Slide 1 of 13
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13

About This Presentation

qgqgqgqgq


Slide Content

Unit-4 Introduction to Servlets Common Gateway Interface (CGI): The common gateway interface is the middleware in which the web servers execute the user request from an external program. Such programs are called CGI programs. CGI provides the framework for the programs on interaction with HTTP(Hyper text transfer Protocol) server. The user request through forms is passed on to the application program which processes the data and may give a confirmation message back. This process is called as Common Gateway Interface.

Overview The CGI(Common Gateway Interface) is an executable program that is specified on the URL and passes a parameter to CGI program. The program is processed by HTTP Server and passes standard input to access environment variables. The output such as HTML elements is passed back to the HTTP server Thus it is the process of the CGI program. When a user browses the web with a request using a URL to the server, the server parses the URL. The filename is searched which is kept in a pre-configured directory called CGI Directory and displays the file or error message back to the user.

CGI applications The interactive applications use CGI for collecting input from the user to the server. Forms The Form contains CGI program and HTML. The form is a visual representation for collecting static data and accepting commands which are processed by the CGI program. The HTML form consists of the button ‘submit’ and ‘action’ which push the URL to the server as a query string. After processing from the server it produces visual HTML pages for the user. The form consists of Checkbox, text field, radio button, button, and selection list.

CGI applications Gateway The Gateway scripts or program access database information in the form of SQL queries. In the database, the CGI program serves as a gateway to process the user query and display or retrieve virtual or dynamic information. Virtual or dynamic document The user request in the form of image, audio, text, or HTML to create a virtual document. The virtual document has a combination of gateways, forms, and graphics.

Lifecycle of a Servlets. Servlets: Servlets are server side programs that run on a web or applications server and acts as a middle layer between a request coming from a web browser and database or application on the server.

Lifecycle of a Servlets. The web container maintains the life cycle of a servlet instance. Let's see the life cycle of the servlet: 1. Servlet class is loaded. 2. Servlet instance is created. 3. init method is invoked. 4. service method is invoked. 5. destroy method is invoked.

Lifecycle of a Servlets. Servlet class is loaded The classloader is responsible to load the servlet class. The servlet class is loaded when the first request for the servlet is received by the web container. 2) Servlet instance is created The web container creates the instance of a servlet after loading the servlet class. The servlet instance is created only once in the servlet life cycle. 3) init method is invoked The web container calls the init method only once after creating the servlet instance. The init method is used initialize the servlet. It is the life cycle method of the javax.servlet.Servlet interface. 4 ) service method is invoked The web container calls the service method each time when request for the servlet is received. If servlet is not initialized, it follows the first three steps as described above then calls the service method. If servlet is initialized, it calls the service method.

Lifecycle of a Servlets. 5) destroy method is invoked The web container calls the destroy method before removing the servlet instance from the service. It gives the servlet an opportunity to clean up any resource for example memory, thread etc.

The Servlets API The Servlets API is part of the Java Enterprise Edition (Java EE) and provides a set of interfaces and classes for creating web applications . The API facilitates the development of web-based applications by enabling developers to create dynamic content that can respond to client requests, typically over HTTP . An   Interface in Java  programming language is defined as an abstract type used to specify the behavior of a class. An interface in Java is a blueprint of a behavior. A Java interface contains static constants and abstract methods.

Key Interfaces and Classes in the Servlets API 1. Servlet Interface Description : The fundamental interface that all servlets must implement. Key Methods : init( ServletConfig config ): Initializes the servlet. service( ServletRequest req , ServletResponse res): Handles client requests. destroy(): Cleans up resources before the servlet is destroyed. getServletConfig (): Returns the ServletConfig object. getServletInfo (): Returns information about the servlet . 2 . GenericServlet Class Description : An abstract class that implements the Servlet interface, providing basic implementation for the Servlet methods except service. Usage : Typically extended by servlets that do not use HTTP-specific functionality. Key Methods : init( ServletConfig config ): A convenience method to initialize the servlet. service( ServletRequest req , ServletResponse res): Must be overridden by subclasses. destroy(): Final cleanup.

Key Interfaces and Classes in the Servlets API 3. HttpServlet Class Description : Extends GenericServlet and provides methods specifically for handling HTTP requests. Usage : Commonly extended by most servlets to handle HTTP GET, POST, PUT, DELETE requests. Key Methods : doGet ( HttpServletRequest req , HttpServletResponse res): Handles HTTP GET requests. doPost ( HttpServletRequest req , HttpServletResponse res): Handles HTTP POST requests. doPut ( HttpServletRequest req , HttpServletResponse res): Handles HTTP PUT requests. doDelete ( HttpServletRequest req , HttpServletResponse res ): Handles HTTP DELETE requests. service( HttpServletRequest req , HttpServletResponse res): Dispatches requests to the appropriate doXXX method.

4. ServletRequest Interface Description : Encapsulates the request information from the client to the server. Key Methods : getParameter (String name): Retrieves a parameter value from the request. getParameterNames (): Returns an enumeration of parameter names. getInputStream (): Returns an input stream for reading request data. getReader (): Returns a reader for reading request data. getAttribute (String name): Retrieves an attribute from the request . 5. HttpServletRequest Interface Description : Extends ServletRequest to provide request information for HTTP servlets . Key Methods : getMethod (): Returns the HTTP method (e.g., GET, POST). getRequestURL (): Returns the URL of the request. getHeader (String name): Retrieves a specific header from the request. getCookies (): Returns an array of cookies sent with the request. getSession (): Returns the current session associated with the request.

6. ServletResponse Interface Description : Encapsulates the response information from the server to the client. Key Methods : setContentType (String type): Sets the content type of the response. getOutputStream (): Returns an output stream for writing response data. getWriter (): Returns a writer for writing response data. setCharacterEncoding (String charset ): Sets the character encoding for the response.
Tags