Published on

Published in: Education, Technology
  • Be the first to comment

  • Be the first to like this

No Downloads
Total views
On SlideShare
From Embeds
Number of Embeds
Embeds 0
No embeds

No notes for slide


  1. 1. Disclaimer: This presentation is prepared by trainees ofbaabtra as a part of mentoring program. This is not officialdocument of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt .Ltd
  2. 2. ServletAbhishek H
  3. 3. Servlet• Is a java programming language class thatused to extend the capabilities of a server.• Can respond to any type of requests, but theyare commonly used to extend the applicationshosted by web servers.
  4. 4. Why Servlet is used?• Process or store data that was submitted froman html form• Provide dynamic content such as the results ofa database query• Manage state information that does not existin the stateless HTTP protocol, such as fillingthe articles into the shopping cart of theappropriate customer.
  5. 5. Diagram
  6. 6. Servlet• In the terms of Java, Servlet is a class in J2EEwhich provides all the functionalities of aserver.• Thus a developer can use Servlet inorder toadd the dynamic content to a web serverusing Java platform.• Servlet is used as a shorthand of HTTP Servlet
  7. 7. Servlet• Can be generated automatically from javaserver pages (JSP) by the JavaServer PagesCompiler.• Difference between servlet and JSP is that isthat servlets typically embed HTML inside Javacode, while JSPs embed Java code in HTML.
  8. 8. How Servlet works?ReceiverequestSendresponseIsservletloaded?Isservletcurrent?Process requestLoad ServletNOYESNOYES
  9. 9. Servlet APIs• Every servlet must implementjavax.servlet.Servlet interface• Most servlets implement the interface byextending one of these classes–javax.servlet.GenericServlet–javax.servlet.http.HttpServlet
  10. 10. Generic and HTTP ServletbrowserbrowserserverHTTPserverrequestrequestresponseresponseHTTP ServletGeneric Servletservice()doGet()doPost()service()
  11. 11. Generic servlet methods• void init(ServletConfig config)– Initializes the servlet.• void service(ServletRequest req, ServletResponse res)– Carries out a single request from the client.• void destroy()– Cleans up whatever resources are being held (e.g., memory, filehandles, threads) and makes sure that any persistent state issynchronized with the servlets current in-memory state.• ServletConfig getServletConfig()– Returns a servlet config object, which contains any initializationparameters and startup configuration for this servlet.• String getServletInfo()– Returns a string containing information about the servlet, such as itsauthor, version, and copyright.Life cyclemethodsof Servlet
  12. 12. Servlet LifecycleInitializationinit()Serviceservice()doGet()doPost()doDelete()doHead()doTrace()doOptions()Destructiondestroy()ConcurrentThreadsof Execution
  13. 13. HttpServlet - Methods• void doGet (HttpServletRequest request,• HttpServletResponse response)– handles GET requests• void doPost (HttpServletRequest request,• HttpServletResponse response)– handles POST requests• void doPut (HttpServletRequest request,• HttpServletResponse response)– handles PUT requests• void doDelete (HttpServletRequest request,• HttpServletResponse response)– handles DELETE requests
  14. 14. Servlet Request Objects• provides client request information to a servlet.• the servlet container creates a servlet request object andpasses it as an argument to the servlets service method.• the ServletRequest interface define methods to retrieve datasent as client request:– parameter name and values– attributes– input stream• HTTPServletRequest extends the ServletRequest interface toprovide request information for HTTP servlets
  15. 15. HttpServletRequest - MethodsEnumeration getParameterNames()an Enumeration of String objects, eachString containing the name of a requestparameter; or an empty Enumeration ifthe request has no parametersjava.lang.String[] getParameterValues (java.lang.Stringname)Returns an array of String objectscontaining all of the values the givenrequest parameter has, or null if theparameter does not existjava.lang.String getParameter (java.lang.String name)Returns the value of a requestparameter as a String, or null if theparameter does not exist.
  16. 16. HttpServletRequest - MethodsCookie[] getCookies()Returns an array containing all of the Cookieobjects the client sent with this getMethod()Returns the name of the HTTP method withwhichthi request was made, for example,GET, POST, or getQueryString()Returns the query string that is contained inthe request URL after the path.HttpSession getSession()Returns the current session associated withthis request, or if the request does not have asession, creates one.
  17. 17. Servlet Response Objects• Defines an object to assist a servlet in sendinga response to the client.• The servlet container creates aServletResponse object and passes it as anargument to the servlets service method.
  18. 18. HttpServletResponse - getWriter()Returns a PrintWriter object thatcan sendcharacter text to the clientvoid setContentType(java.lang.String type)Sets the content type of theresponse being sent to theclient. The content type mayinclude the type of characterencoding used, for example,text/html; charset=ISO-8859-4int getBufferSize()Returns the actual buffer sizeused for the response
  19. 19. If this presentation helped you, please visit ourpage and like it.Thanks in | |
  20. 20. Contact UsEmarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550Start up VillageEranakulam,Kerala, India.Email: