Servlets IST 421 Spring 2007 Servlets Java servlets

  • Slides: 12
Download presentation
Servlets IST 421 Spring 2007

Servlets IST 421 Spring 2007

Servlets ¡ Java servlets are server-side applications l l l Server that executes a

Servlets ¡ Java servlets are server-side applications l l l Server that executes a servlet is called servlet container or servlet engine Are multi-threaded and can share resources Since they are compiled, provide better performance than scripting interpreters like Perl

Servlet Technology ¡ ¡ Supported directly by most browsers All servlets must invoke the

Servlet Technology ¡ ¡ Supported directly by most browsers All servlets must invoke the Servlet interface: l ¡ Javax. servlet – in the servlet. jar libraries Application servers that support Servlets include: l l l Sun ONE Application Server Microsoft’s Internet Information Services (IIS) Apache HTTP Server BEA’s Web. Logic server IBM’s Web. Sphere server

Servlet Technology ¡ Communication between clients and servers using HTTP protocol: l l l

Servlet Technology ¡ Communication between clients and servers using HTTP protocol: l l l Client send an HTTP request to the server Servlet container receives the request Servlet container directs request to be processed by the appropriate servlet Servlet processes request: ¡ Interact with a database, JSPs, etc. Servlet returns results to the client ¡ HTML, XHTML, or XML document to display in a browser

Servlet Lifecycle Creation ¡ ¡ ¡ Init Service Destroy Servlet container loads servlet into

Servlet Lifecycle Creation ¡ ¡ ¡ Init Service Destroy Servlet container loads servlet into memory init ( ) is invoked by the container After init ( ) is complete, the servlet can respond to requests service ( ) receives requests, processes them, and responds to the client. destroy ( ) releases resources

Abstract classes Generic. Servlet from javax. servlet ¡ Http. Servlet from javax. servlet. http

Abstract classes Generic. Servlet from javax. servlet ¡ Http. Servlet from javax. servlet. http ¡ Most servlets extend one or the other of the above and override some of their methods ¡ WEB servlets usually extend Http. Servlet ¡

Servlet Methods ¡ void init (Servlet. Config config) l l ¡ Automatically called to

Servlet Methods ¡ void init (Servlet. Config config) l l ¡ Automatically called to initialize the servlet The argument is supplied by the servlet container that executes the init servlet Servlet. Config get. Servlet. Config ( ) l l Returns a reference to an object that implements the Servlet. Config interface The object provides configuration information and provides the servlet with access to its environment (i. e. the servlet container in which it executes)

Servlet Methods ¡ String get. Servlet. Info ( ) l l ¡ void service

Servlet Methods ¡ String get. Servlet. Info ( ) l l ¡ void service (Servlet. Request request, Servlet. Response response) l l ¡ This method is defined by the programmer Returns a string to servlet information such as the author and version The servlet container calls this method to respond to a client request Called once per request void destroy ( ) l Cleanup method when servlet is destroyed

Http. Servlet ¡ Defines processing for servlets that extend functionality of a WEB server.

Http. Servlet ¡ Defines processing for servlets that extend functionality of a WEB server. l In this class we will extend Http. Servlet l Key method in every servlet is service, first method called.

Http. Servlet ¡ Common request types: l HTTP get to supply data as part

Http. Servlet ¡ Common request types: l HTTP get to supply data as part of the request ¡ Parameters are passed to a servlet as name/value pairs ¡ Cache the data obtained for fast processing ¡ Limit of 255 characters for its parameters ¡ Request is in the form of a URL ¡ Only character data may be sent l HTTP post to post data to a WEB server ¡ Subtle difference – post normally will not cache the information ¡ Most often method used to obtain form information ¡ No limit to size of parameters ¡ May carry character or binary data

Http. Servlet ¡ The methods in Http. Servlet to accomplish these tasks are called

Http. Servlet ¡ The methods in Http. Servlet to accomplish these tasks are called by service( ): ¡do. Get ¡do. Post

Http. Servlet. Request and Http. Servlet. Response Are arguments of do. Post and do.

Http. Servlet. Request and Http. Servlet. Response Are arguments of do. Post and do. Get ¡ These methods make it easy to access data ¡