Servlet APIs Every servlet must implement javax servlet

  • Slides: 17
Download presentation
Servlet APIs • Every servlet must implement javax. servlet. Servlet interface • Most servlets

Servlet APIs • Every servlet must implement javax. servlet. Servlet interface • Most servlets implement the interface by extending one of these classes – javax. servlet. Generic. Servlet – javax. servlet. http. Http. Servlet

Generic Servlet & HTTP Servlet Generic. Servlet Client request Server service ( ) response

Generic Servlet & HTTP Servlet Generic. Servlet Client request Server service ( ) response HTTPServlet Browser do. Get( ) request HTTP Server response service ( ) do. Post( )

Interface javax. servlet. Servlet • The Servlet interface defines methods – – – to

Interface javax. servlet. Servlet • The Servlet interface defines methods – – – to initialize a servlet Life to receive and respond to client requests Cycle Methods to destroy a servlet and its resources to get any startup information to return basic information about itself, such as its author, version and copyright. • Developers need to directly implement this interface only if their servlets cannot (or choose not to) inherit from Generic. Servlet or Http. Servlet.

Generic. Servlet - Methods • void init(Servlet. Config config) – Initializes the servlet. •

Generic. Servlet - Methods • void init(Servlet. Config config) – Initializes the servlet. • void service(Servlet. Request req, Servlet. Response 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. • Servlet. Config get. Servlet. Config() – Returns a servlet config object, which contains any initialization parameters and startup configuration for this servlet. • String get. Servlet. Info() – Returns a string containing information about the servlet, such as its author, version, and copyright.

HTTPServlet • A general servlet knows nothing about the Hyper. Text Transfer Protocol (HTTP),

HTTPServlet • A general servlet knows nothing about the Hyper. Text Transfer Protocol (HTTP), which is the major protocol used for Internet. • A special kind of servlet, HTTPServlet, is needed to handle requests from HTTP clients such as web browsers. • HTTPServlet is included in the package javax. servlet. http as a subclass of Generic. Servlet. 5

Hypertext Transfer Protocol • Hypertext Transfer Protocol (HTTP) is the network protocol that underpins

Hypertext Transfer Protocol • Hypertext Transfer Protocol (HTTP) is the network protocol that underpins the World Wide Web. • For example: – a) when a user enters a URL in a Web browser, the browser issues an HTTP GET request to the Web server – b) the HTTP GET method is used by the server to retrieve a document – c) the Web server then responds with the requested HTML document 6

HTTP Methods Useful for Web applications: Not useful for Web applications: • GET -

HTTP Methods Useful for Web applications: Not useful for Web applications: • GET - request information • PUT - place documents from a server directly to a server • TRACE - debugging • POST - sends an • DELETE - remove unlimited amount of documents from a server information over a socket connection as part of the • OPTIONS - ask a server what methods and other HTTP request options the server supports for the requested resource • HEAD - requests the header of a response 7

Get Versus Post • GET request : • provides a limited amount of information

Get Versus Post • GET request : • provides a limited amount of information in the form of a query string which is normally up to 255 characters • visible in a URL must only be used to execute queries in a Web application 8 • POST request : • sends an unlimited amount of information • does not appear as part of a URL

Http. Servlet

Http. Servlet

HTTPServlet • A general servlet knows nothing about the Hyper. Text Transfer Protocol (HTTP),

HTTPServlet • A general servlet knows nothing about the Hyper. Text Transfer Protocol (HTTP), which is the major protocol used for Internet. • A special kind of servlet, HTTPServlet, is needed to handle requests from HTTP clients such as web browsers. • HTTPServlet is included in the package javax. servlet. http as a subclass of Generic. Servlet. 10

Http. Servlet - Methods • void do. Get (Http. Servlet. Request request, Http. Servlet.

Http. Servlet - Methods • void do. Get (Http. Servlet. Request request, Http. Servlet. Response response) –handles GET requests • void do. Post (Http. Servlet. Request request, Http. Servlet. Response response) –handles POST requests • void do. Put (Http. Servlet. Request request, Http. Servlet. Response response) –handles PUT requests • void do. Delete (Http. Servlet. Request request, Http. Servlet. Response response) – handles DELETE requests

Servlet Request Objects • provides client request information to a servlet. • the 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 Servlet. Request interface define methods to retrieve data sent as client request: –parameter name and values – attributes – input stream • HTTPServlet. Request extends the Servlet. Request interface to provide request information for HTTP servlets

Http. Servlet. Request - Methods Enumeration get. Parameter. Names() an Enumeration of String objects,

Http. Servlet. Request - Methods Enumeration get. Parameter. Names() 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[] get. Parameter. Values (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 get. Parameter (java. lang. String name) Returns the value of a request parameter as a String, or null if the parameter does not exist.

Http. Servlet. Request - Methods Cookie[] get. Cookies() Returns an array containing all of

Http. Servlet. Request - Methods Cookie[] get. Cookies() Returns an array containing all of the Cookie objects the client sent with this request. java. lang. String get. Method() Returns the name of the HTTP method with whichthi request was made, for example, GET, POST, or PUT. java. lang. String get. Query. String() Returns the query string that is contained in the request URL after the path. Http. Session get. Session() Returns the current session associated with this request, or if the request does not have a session, creates one.

Servlet Response Objects • Defines an object to assist a servlet in sending a

Servlet Response Objects • Defines an object to assist a servlet in sending a response to the client. • The servlet container creates a Servlet. Response object and passes it as an argument to the servlet's service method.

Http. Servlet. Response - Methods java. io. Print. Writer get. Writer() Returns a Print.

Http. Servlet. Response - Methods java. io. Print. Writer get. Writer() Returns a Print. Writer object that can send character text to the client void set. Content. Type (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 get. Buffer. Size() Returns the actual buffer size used for the response

DEMO

DEMO