Top Banner
21
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Servlet
Page 2: Servlet

Disclaimer: This presentation is prepared by trainees of baabtra as a part of mentoring program. This is not official document of baabtra –Mentoring PartnerBaabtra-Mentoring Partner is the mentoring division of baabte System Technologies Pvt . Ltd

Page 3: Servlet

Servlet

Abhishek H [email protected]/

abhishekmenon.5twitter.com/abhishekmenon121989in.linkedin.com/in/abhishekhmenon9496519895

Page 4: Servlet

Servlet

• Is a java programming language class that used to extend the capabilities of a server.

• Can respond to any type of requests, but they are commonly used to extend the applications hosted by web servers.

Page 5: Servlet

Why Servlet is used?

• Process or store data that was submitted from an html form

• Provide dynamic content such as the results of a database query

• Manage state information that does not exist in the stateless HTTP protocol, such as filling the articles into the shopping cart of the appropriate customer.

Page 6: Servlet

Diagram

Page 7: Servlet

Servlet

• In the terms of Java, Servlet is a class in J2EE which provides all the functionalities of a server.

• Thus a developer can use Servlet inorder to add the dynamic content to a web server using Java platform.

• Servlet is used as a shorthand of HTTP Servlet

Page 8: Servlet

Servlet

• Can be generated automatically from java server pages (JSP) by the JavaServer Pages Compiler.

• Difference between servlet and JSP is that is that servlets typically embed HTML inside Java code, while JSPs embed Java code in HTML.

Page 9: Servlet

How Servlet works?

Receive request

Send response

Is servlet loaded

?

Is servlet current

?

Process request

Load Servlet

NO

YES

NO

YES

Page 10: Servlet

Servlet APIs

• Every servlet must implement javax.servlet.Servlet interface

• Most servlets implement the interface by extending one of these classes– javax.servlet.GenericServlet– javax.servlet.http.HttpServlet

Page 11: Servlet

Generic and HTTP Servlet

browser

browser

server

HTTP server

request

request

response

response

HTTP Servlet

Generic Servlet

service()

doGet()

doPost()service()

Page 12: Servlet

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, file handles, threads) and makes sure that any persistent state is synchronized with the servlet's current in-memory state.

• ServletConfig getServletConfig() – Returns a servlet config object, which contains any initialization

parameters and startup configuration for this servlet. • String getServletInfo()

– Returns a string containing information about the servlet, such as its author, version, and copyright.

Life cycle methods of Servlet

Page 13: Servlet

Servlet LifecycleInitialization

init()

Service service()

doGet()doPost()

doDelete()doHead()doTrace()

doOptions()

Destructiondestroy()

Concurrent Threadsof Execution

Page 14: Servlet

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

Page 15: Servlet

Servlet Request Objects

• provides client request information to a servlet. • the servlet container creates a servlet request object and

passes it as an argument to the servlet's service method. • the ServletRequest interface define methods to retrieve data

sent as client request:– parameter name and values– attributes– input stream

• HTTPServletRequest extends the ServletRequest interface to provide request information for HTTP servlets

Page 16: Servlet

HttpServletRequest - Methods Enumeration getParameterNames()

an Enumeration of String objects, each String containing the name of a request parameter; or an empty Enumeration if the request has no parameters

java.lang.String[] getParameterValues (java.lang.String name) Returns an array of String objects containing all of the values the given request parameter has, or null if the parameter does not exist

java.lang.String getParameter (java.lang.String name) Returns the value of a request parameter as a String, or null if the parameter does not exist.

Page 17: Servlet

HttpServletRequest - Methods

Cookie[] getCookies()Returns an array containing all of the Cookie objects the client sent with this request.

java.lang.String getMethod() Returns the name of the HTTP method with which\thi request was made, for example, GET, POST, or PUT.

java.lang.String getQueryString() Returns the query string that is contained in the request URL after the path.

HttpSession getSession() Returns the current session associated with this request, or if the request does not have a session, creates one.

Page 18: Servlet

Servlet Response Objects

• Defines an object to assist a servlet in sending a response to the client.

• The servlet container creates a ServletResponse object and passes it as an argument to the servlet's service method.

Page 19: Servlet

HttpServletResponse - Methods java.io.PrintWriter getWriter()

Returns a PrintWriter object that can sendcharacter text to the client

void setContentType (java.lang.String type) Sets the content type of the response being sent to the client. The content type may include the type of character encoding used, for example, text/html; charset=ISO-8859-4

int getBufferSize() Returns the actual buffer size used for the response

Page 20: Servlet

If this presentation helped you, please visit our page facebook.com/baabtra and like it.

Thanks in advance.

www.baabtra.com | www.massbaab.com |www.baabte.com

Page 21: Servlet

Contact Us

Emarald Mall (Big Bazar Building)Mavoor Road, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

NC Complex, Near Bus StandMukkam, Kozhikode,Kerala, India.Ph: + 91 – 495 40 25 550

Start up VillageEranakulam,Kerala, India.

Email: [email protected]