What is Servlet??
A servlet is a Java programming language class used to extend the capabilities of servers that host applications accessed by means of a request-response programming model. For such applications, Java Servlet technology defines HTTP-specific servlet classees. The javax.servlet and javax.servlet.http packages provide interfaces and classes for writing servlets. All servlets must implement the Servlet interface, which defines lifecycle methods. When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.
Java Servlets often serve the same purpose as programs implemented using the Common Gateway Interface (CGI) script. But Servlets offer several advantages in comparison with the CGI.
Advantages Of Servlet Over CGI:-
Initially, Common Gateway Interface (CGI) server-side scripts were the main technology used to generate dynamic content. Although widely used, CGI scripting technology had many shortcomings, including platform dependence and lack of scalability. CGI Technology enables webserver to call an external program and pass Http request information to external program to process the request .For Each request , it starts with new Process. To address these limitations, Java Servlet technology was created as a portable way to provide dynamic, user-oriented content.
In case of servlet. The web container create threads for handling the multiple request to the servlet.since threads have a lots of benefits over process. For more information about thread and process click here
Some Example Applications where servlets could be used:-
1-A few of the many applications for servlets include,Processing data POSTed over HTTPS using an HTML form, including purchase order or credit card data. A servlet like this could be part of an order-entry and processing system, working with product and inventory databases, and perhaps an on-line payment system.
2-Allowing collaboration between people. A servlet can handle multiple requests concurrently; they can synchronize requests to support systems such as on-line conferencing.
3- Forwarding requests. Servlets can forward requests to other servers and servlets. This allows them to be used to balance load among several servers that mirror the same content. It also allows them to be used to partition a single logical service over several servers, according to task type or organizational boundaries.
Architecture Overview:-
All servlets must implement the Servlet interface, which defines lifecycle methods. When implementing a generic service, you can use or extend the GenericServlet class provided with the Java Servlet API. The HttpServlet class provides methods, such as doGet and doPost, for handling HTTP-specific services.You can be clear by given below image:-
The Servlet interface provides the following methods that manage the servlet and its communications with clients.
1- destroy()
Cleans up whatever resources are being held and makes sure that any persistent state is synchronized with the servlet's current in-memory state.
2- getServletConfig()
Returns a servlet config object, which contains any initialization parameters and startup configuration for this servlet.
3- getServletInfo()
Returns a string containing information about the servlet, such as its author, version, and copyright.
4- init(ServletConfig)
Initializes the servlet. Run once before any requests can be serviced.
5- service(ServletRequest, ServletResponse)
Carries out a single request from the client.
Servlet writers provide some or all of these methods when developing a servlet.
How Servlet Works:-
1-When a servlet accepts a service call from a client, it receives two objects, ServletRequest and ServletResponse. The ServletRequest class encapsulates the communication from the client to the server, while the ServletResponse class encapsulates the communication from the servlet back to the client.
2-The ServletRequest interface allows the servlet access to information such as the names of the parameters passed in by the client, the protocol (scheme) being used by the client, and the names of the remote host that made the request and the server that received it. It also provides the servlet with access to the input stream, ServletInputStream, through which the servlet gets data from clients that are using application protocols such as the HTTP POST and PUT methods. Subclasses of ServletRequest allow the servlet to retrieve more protocol-specific data. For example, HttpServletRequest contains methods for accessing HTTP-specific header information.
3-The ServletResponse interface gives the servlet methods for replying to the client. It allows the servlet to set the content length and mime type of the reply, and provides an output stream, ServletOutputStream, and a Writer through which the servlet can send the reply data. Subclasses of ServletResponse give the servlet more protocol-specific capabilities. For example, HttpServletResponse contains methods that allow the servlet to manipulate HTTP-specific header information.
The classes and interfaces described above make up a basic Servlet. HTTP servlets have some additional objects that provide session-tracking capabilities. The servlet writer can use these APIs to maintain state between the servlet and the client that persists across multiple connections during some time period.
Servlet API
Two packages:
1-javax.servlet-represents interfaces and classes for the Servlet API.This is not specific for any protocol.
2-javax.servlet.http -represents interfaces and classes for the Servlet API.But this is specific for only http request .
Servlet API Package as per jdk1.6
javax.servlet package:--
The javax.servlet package contains a number of classes and interfaces that describe and define the contracts between a servlet class and the runtime environment provided for an instance of such a class by a conforming servlet container.
Interface
AsyncContext
AsyncListener
Filter
FilterChain
FilterConfig
RequestDispatcher
Servlet
ServletConfig
ServletContext
ServletRequest
ServletResponse
Class
GenericServlet
ServletContextEvent
ServletInputStream
ServletOutputStream
ServletRequestAttributeEvent
ServletRequestWrapper
ServletSecurityElement
javax.servlet.http package:--
Interface
HttpServletRequest
HttpServletResponse
HttpSession
HttpSessionAttributeListener
HttpSessionContext
Class
Cookie
HttpServlet
HttpServletRequestWrapper
HttpServletResponseWrapper
HttpSessionBindingEvent
HttpSessionEvent
Interface
AsyncContext
AsyncListener
Filter
FilterChain
FilterConfig
RequestDispatcher
Servlet
ServletConfig
ServletContext
ServletRequest
ServletResponse
Class
GenericServlet
ServletContextEvent
ServletInputStream
ServletOutputStream
ServletRequestAttributeEvent
ServletRequestWrapper
ServletSecurityElement
Interface
HttpServletRequest
HttpServletResponse
HttpSession
HttpSessionAttributeListener
HttpSessionContext
Class
0 comments:
Post a Comment