Get Upto 50% Offer | OFFER ENDING IN: 0 D 0 H 0 M 0 S

Log In to start Learning

Login via

Post By Admin Last Updated At 2020-06-15
Introduction to Servlet

Introduction to Servlet:

Servlet innovation is utilized to make web application (dwells at server side and produces dynamic website page). Servlet innovation is powerful and versatile due to java dialect. Before Servlet, CGI (Common Gateway Interface) scripting dialect was well known as a server-side programming dialect. In any case, there was many disservices of this innovation. We have examined these hindrances underneath. There are various interfaces and classes in the servlet API, for instance, HttpServlet ,Servlet, GenericServlet, ServletResponse ,ServletRequest,.

What is Servlet?

Servlet can be portrayed from numerous points of view, contingent upon the unique circumstance. Servlet is an innovation i.e. used to make web application. Servlet is an API that gives numerous interfaces and classes including documentations. Servlet is an interface that must be executed for making any servlet. Servlet is a class that broadens the capacities of the servers and react to the approaching solicitation. It can react to solicitations. Servlet is a web segment that is sent on the server to make dynamic website page.

MVC Architecture :

MVC Architecture

Servlet web over view:

A web application is an application open from the web. A web application is made out of web parts like Servlet, JSP, Filter and so on and different segments, for example, HTML. The web segments normally execute in Web Server and react to HTTP ask.

CGI(Commmon Gateway Interface)

CGI innovation empowers the web server to call an outside program and pass HTTP ask for data to the outer program to prepare the demand. For each demand, it begins another procedure.

Hindrances of CGI

There are numerous issues in CGI innovation:

  • On the off chance that number of customers builds, it sets aside more opportunity for sending reaction.
  • For each demand, it begins a procedure and Web server is constrained to begin forms.
  • It utilizes stage subordinate dialect e.g. C, C++, Perl.
  • Preferred standpoint of Servlet

Advantage of Servlet:

There are many points of interest of servlet over CGI. The web holder makes strings for dealing with the various solicitations to the servlet. Strings have a ton of advantages over the Processes, for example, they share a typical memory range, and lightweight, cost of correspondence between the strings are low. The essential advantages of servlet are as per the following:

Better execution: since it makes a string for each demand not prepare.

Performance: on the grounds that it utilizes java dialect.

Robust: Servlets are overseen by JVM so we don't have to stress over memory spill.

Secure: light of the fact that it utilizes java dialect.

Servlet API :

The javax.servlet and javax.servlet.http bundles speak to interfaces and classes for servlet programming interface.

  • The javax.servlet bundle contains numerous interfaces and classes that are utilized by the servlet or web holder. These are not particular to any convention.
  • The javax.servlet.http bundle contains interfaces and classes that are in charge of http ask for as it were.
  • We should perceive what are the interfaces of javax.servlet bundle.

Interfaces in javax.servlet package

  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

JAVA Tutorial Video

[embed]https://www.youtube.com/watch?v=Mb8nG-NH6gg&t=2835s [/embed]

Classes in javax.servlet package

  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

  1. HttpServletRequest
  2. HttpServletResponse
  3. HttpSession
  4. HttpSessionListener
  5. HttpSessionAttributeListener
  6. HttpSessionBindingListener
  7. HttpSessionActivationListener
  8. HttpSessionContext (deprecated now)

Classes in javax.servlet.http package

  1. HttpServlet
  2. Cookie
  3. HttpServletRequestWrapper
  4. HttpServletResponseWrapper
  5. HttpSessionEvent
  6. HttpSessionBindingEvent
  7. HttpUtils (deprecated now)

HttpServlet:

The Http Servlet  grows the GenericServlet and execute Serializable interface. It results the http particular frameworks.

Methods of HttpServlet class

  1. public void service(ServletRequest req,ServletResponse res)
  2.  protected void service(HttpServletRequest req, HttpServletResponse res
  3. protected void doGet(HttpServletRequest req, HttpServletResponse res)
  4. protected void doPost(HttpServletRequest req, HttpServletResponse res)
  5. protected void doHead(HttpServletRequest req, HttpServletResponse res
  6. protected void doOptions(HttpServletRequest req, HttpServletResponse res)
  7. protected void doPut(HttpServletRequest req, HttpServletResponse res)
  8. protected void doTrace(HttpServletRequest req, HttpServletResponse res)
  9. protected void doDelete(HttpServletRequest req, HttpServletResponse res)  protected long getLastModified(HttpServletRequest req)

Life Cycle of a Servlet:

The Life cycle of the servlet is maintained by the web container

  1. The class of Servlet is loaded.
  2. Servlet instance is created.
  3.  The init method is invoked.
  4. The service method is invoked.
  5. The destroy method is invoked.Servlet Life Cycle

    As showed in the above graph, there are three conditions of a servlet: new, prepared and end. The servlet is in new state if servlet example is made. Subsequent to conjuring the init() technique, Servlet comes in the prepared state. In the prepared state, servlet plays out every one of the undertakings. At the point when the web compartment conjures the annihilate() technique, it movements to the end state.