SERVLET
Q.What is a Servlet?
Servlet can be described in many ways, depending on the context.
- Servlet is a technology which is used to create a web application.
- Servlet is an API that provides many interfaces and classes including documentation.
- Servlet is an interface that must be implemented for creating any Servlet.
- Servlet is a class that extends the capabilities of the servers and responds to the incoming requests. It can respond to any requests.
- Servlet is a web component that is deployed on the server to create a dynamic web page.
Q. How many types of servlet in Java?
Ans:-There are mainly two types of servlets -generic and HTTP:
1. Generic Servlet -
Generic servlets are sub classes of javax.servlet.GenericServlet . The service() method needs to be implemented to handle client requests. Generic servlet is protocol independent servlet. It implements the Servlet and Servlet Config interface. It may be directly extended by the servlet. Writing a servlet in in Generic Servlet is very easy. It has only init() and destroy() method of Servlet Config interface in its life cycle. It also implements the log method of Servlet Context interface.
2. Http Servlet -
HTTP servlets are sub classes of javax.servlet.HttpServlet. It has built-in HTTP protocol support. The doGet and doPost methods are used to handle client requests (GET or POST requests).
For both servlet types, we can implement the constructor method init() and the destructor method destroy() to initialize or deallocate resources.
All servlets must implement a service() method, which is responsible for handling servlet requests. For generic servlets, simply override the service method to provide routines for handling requests.
HTTP servlets provide a service method that automatically routes the request to another method in the servlet based on which HTTP transfer method is used. So, for HTTP servlets, override doPost() to process POST requests, doGet() to process GET requests, and so on
Http Servlet is HTTP (Hyper Text Transfer Protocol ) specific servlet. It provides an abstract class Http Servlet for the developers for extend to create there own HTTP specific servlets. The sub class of Http Servlet must overwrite at least one method given below-
There is no need to overrride service() method. All the servlet either Generic Servlet or Http Servlet passes there config parameter to the Servlet interface.
- Generic : In this service() method is available to handle client request.
- HTTP : In this doget() and dopost() methods are available to handle client request
Q. Advantage and disadvantage of servlet
Servlet Advantage
The advantages of Servlet are as follows:
- Better performance: because it creates a thread for each request, not process.
- Portability: because it uses Java language.
- Robust: JVM manages Servlets, so we don't need to worry about the memory leak, garbage collection, etc.
- Secure: because it uses java language.
- Servlets provide a way to generate dynamic documents that is both easier to write and faster to run.
- provide all the powerfull features of JAVA, such as Exception handling and garbage collection.
- Servlet enables easy portability across Web Servers.
- Servlet can communicate with different servlet and servers.
- Since all web applications are stateless protocol, servlet uses its own API to maintain session
- Designing in servlet is difficult and slows down the application.
- Writing complex business logic makes the application difficult to understand.
- You need a Java Runtime Environment on the server to run servlets. CGI is a completely language independent protocol, so you can write CGIs in whatever languages you have available (including Java if you want to).
Servlet API
The javax.servlet package contains many interfaces and classes that are used by the servlet or web container. These are not specific to any protocol.
The javax.servlet.http package contains interfaces and classes that are responsible for http requests only.
Let's see what are the interfaces of javax.servlet package.
Methods of Servlet interface
There are 5 methods in Servlet interface. The init, service and destroy are the life cycle methods of servlet. These are invoked by the web container.
Interfaces in javax.servlet package
There are many interfaces in javax.servlet package. They are as follows:- Servlet
- ServletRequest
- ServletResponse
- RequestDispatcher
- ServletConfig
- ServletContext
- SingleThreadModel
- Filter
- FilterConfig
- FilterChain
- ServletRequestListener
- ServletRequestAttributeListener
- ServletContextListener
- ServletContextAttributeListener
Classes in javax.servlet package
There are many classes in javax.servlet package. They are as follows:- GenericServlet
- ServletInputStream
- ServletOutputStream
- ServletRequestWrapper
- ServletResponseWrapper
- ServletRequestEvent
- ServletContextEvent
- ServletRequestAttributeEvent
- ServletContextAttributeEvent
- ServletException
- UnavailableException
Interfaces in javax.servlet.http package
There are many interfaces in javax.servlet.http package. They are as follows:- HttpServletRequest
- HttpServletResponse
- HttpSession
- HttpSessionListener
- HttpSessionAttributeListener
- HttpSessionBindingListener
- HttpSessionActivationListener
- HttpSessionContext (deprecated now)
Classes in javax.servlet.http package
There are many classes in javax.servlet.http package. They are as follows:- HttpServlet
- Cookie
- HttpServletRequestWrapper
- HttpServletResponseWrapper
- HttpSessionEvent
- HttpSessionBindingEvent
- HttpUtils (deprecated now)
Servlet Interface
Servlet interface provides commonbehaviorto all the servlets.Servlet interface defines methods that all servlets must implement.
Servlet interface needs to be implemented for creating any servlet (either directly or indirectly). It provides 3 life cycle methods that are used to initialize the servlet, to service the requests, and to destroy the servlet and 2 non-life cycle methods.
Servlet interface needs to be implemented for creating any servlet (either directly or indirectly). It provides 3 life cycle methods that are used to initialize the servlet, to service the requests, and to destroy the servlet and 2 non-life cycle methods.
Methods of Servlet interface
There are 5 methods in Servlet interface. The init, service and destroy are the life cycle methods of servlet. These are invoked by the web container.
Difference between Servlet and JSP
Brief Introduction:
A servlet is a Java class which is used to extend the capabilities of servers that host applications accessed by means of a request-response model. Servlets are mainly used to extend the applications hosted by webs servers, however, they can respond to other types of requests too. For such applications, HTTP-specific servlet classes are defined by Java Servlet technology.
A servlet is a Java class which is used to extend the capabilities of servers that host applications accessed by means of a request-response model. Servlets are mainly used to extend the applications hosted by webs servers, however, they can respond to other types of requests too. For such applications, HTTP-specific servlet classes are defined by Java Servlet technology.
A JSP is a text document which contains two types of text: static data and dynamic data. The static data can be expressed in any text-based format (like HTML, XML, SVG and WML), and the dynamic content can be expressed by JSP elements
0 comments:
Post a Comment