Servlets IST 411 Lecture 5 Spring 2004 Servlets

  • Slides: 13
Download presentation
Servlets IST 411 Lecture 5 Spring 2004

Servlets IST 411 Lecture 5 Spring 2004

Servlets § Java servlets are server-side applications § Are multi-threaded and can share resources

Servlets § Java servlets are server-side applications § Are multi-threaded and can share resources § Since they are compiled, provide better performance than scripting interpreters like Perl § Do not have the security restrictions of applets

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: § Javax. servlet – in the servlet. jar libraries

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. § § Method is called one time/request Each new request invokes a new thread § destroy ( ) releases resources

Abstract classes § Generic. Servlet from javax. servlet § Http. Servlet from javax. servlet.

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) § Automatically called to initialize the

Servlet Methods § void init (Servlet. Config config) § Automatically called to initialize the servlet § The argument is supplied by the servlet container that executes the init servlet § Servlet. Config get. Servlet. Config ( ) § 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 ( ) § This method is defined

Servlet Methods § String get. Servlet. Info ( ) § This method is defined by the programmer § Returns a string to servlet information such as the author and version § void service (Servlet. Request request, Servlet. Response response) § The servlet container calls this method to respond to a client request § Called once per request § void destroy ( ) § 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. § In this class we will extend Http. Servlet § Key method in every servlet is service, first method called.

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

Http. Servlet § Common request types: § 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 § 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

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

Let’s Practice

Let’s Practice

Servlet Projects: 1. Build a project in Eclipse 2. Include a class 3. Include

Servlet Projects: 1. Build a project in Eclipse 2. Include a class 3. Include the servlet. jar by: a. Right click on the project name b. Select Properties c. Select Java Build Path d. Select libraries e. Add External jars: Note: Here you must locate a file that is called servlet. jar and point to it. Normally it is included in the Tom. Cat 4. ncommon/lib. However, a search of the Tom. Cat structure should provide you with the location. f. The servlet. jar file is needed so that the system includes the set of servlet libraries necessary