presearch
SERVLET | Tech Hindi Sagar
Rajnish kumar

Tech Hindi Sagar

Tech Hindi Sagar website Is Stunning Website Created For Educational Purposes. it Archive And Support Student And Teacher Learning,Facilitating, Questioning And By Providing Contexts For Engaging In Higher-Order Thinking.The Use Of Blogs Has Become Popular In Education Institutions Including Public Schools And Colleges. Blogs Can Be Useful Tools For Sharing Information And Tips Among Co-Workers, Providing Information For Students, Or Keeping In Contact With Parents.
Home Project Tutorial Videos Quiz
All Tutorial will be uploaded as soon as posible .Our Vision is Fun and Free Education for ALL Our Mission To bring all feasible courses , online..

SERVLET

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.
  1. Generic : In this service() method is available to handle client request.
  2. 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:
  1. Better performance: because it creates a thread for each request, not process.
  2. Portability: because it uses Java language.
  3. Robust: JVM manages Servlets, so we don't need to worry about the memory leak, garbage collection, etc.
  4. 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
Servlet Disadvantage

  • 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 and javax.servlet.http packages represent interfaces and classes for 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.

Interfaces in javax.servlet package

There are many interfaces in javax.servlet package. They are as follows:
  1. Servlet
  2. ServletRequest
  3. ServletResponse
  4. RequestDispatcher
  5. ServletConfig
  6. ServletContext
  7. SingleThreadModel
  8. Filter
  9. FilterConfig
  10. FilterChain
  11. ServletRequestListener
  12. ServletRequestAttributeListener
  13. ServletContextListener
  14. ServletContextAttributeListener

Classes in javax.servlet package

There are many classes in javax.servlet package. They are as follows:
  1. GenericServlet
  2. ServletInputStream
  3. ServletOutputStream
  4. ServletRequestWrapper
  5. ServletResponseWrapper
  6. ServletRequestEvent
  7. ServletContextEvent
  8. ServletRequestAttributeEvent
  9. ServletContextAttributeEvent
  10. ServletException
  11. UnavailableException

Interfaces in javax.servlet.http package

There are many interfaces in javax.servlet.http package. They are as follows:
  1. HttpServletRequest
  2. HttpServletResponse
  3. HttpSession
  4. HttpSessionListener
  5. HttpSessionAttributeListener
  6. HttpSessionBindingListener
  7. HttpSessionActivationListener
  8. HttpSessionContext (deprecated now)

Classes in javax.servlet.http package

There are many classes in javax.servlet.http package. They are as follows:
  1. HttpServlet
  2. Cookie
  3. HttpServletRequestWrapper
  4. HttpServletResponseWrapper
  5. HttpSessionEvent
  6. HttpSessionBindingEvent
  7. 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.


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:
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.
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


SHARE

About Admin of the Blog:

Rajnish kumar is the CEO/founder of Tech Hindi Sagar .He is a Computer Science Engineer ,Web Designer,Web Developer and a Pro Blogger..Inspired to make things looks better.

  • Image
  • Image
  • Image
  • Image
  • Image
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment