Introduction to ServerSide Web Development Session II Introduction
Introduction to Server-Side Web Development Session II: Introduction to Server-Side Web Development with Servlets 26 th February 2004 Bogdan L. Vrusias b. vrusias@surrey. ac. uk 26 th February 2004 Bogdan L. Vrusias © 2004
Introduction to Server-Side Web Development Session II • Servlets Architecture • Lifecycle • Examples 26 th February 2004 Bogdan L. Vrusias © 2004 2
Introduction to Server-Side Web Development Servlet Architecture Overview • The central abstraction in the Servlet API is the Servlet interface. All servlets implement this interface, either directly or, more commonly, by extending a class that implements it such as Http. Servlet. The Servlet interface provides for methods that manage the servlet and its communications with clients. Servlet writers provide some or all of these methods when developing a servlet. • When a servlet accepts a call from a client, it receives two objects: one is a Servlet. Request and the other is a Servlet. Response. – The Servlet. Request class encapsulates the communication from the client to the server – The Servlet. Response class encapsulates the communication from the servlet back to the client. 26 th February 2004 Bogdan L. Vrusias © 2004 3
Introduction to Server-Side Web Development Servlet. Request • The Servlet. Request interface allows the servlet access to information such as: – the names of the parameters passed in by the client – the protocol (scheme) being used by the client – and the names of the remote host that made the request and the server that received it. • It also provides the servlet with access to the input stream, Servlet. Input. Stream, through which the servlet gets data from clients that are using application protocols such as the HTTP POST and PUT methods. Subclasses of Servlet. Request allow the servlet to retrieve more protocol-specific data. For example, Http. Servlet. Request contains methods for accessing HTTP-specific header information. 26 th February 2004 Bogdan L. Vrusias © 2004 4
Introduction to Server-Side Web Development Servlet. Response • The Servlet. Response interface gives the servlet methods for replying to the client. It allows the servlet to: – set the content length and mime type of the reply – and provides an output stream, Servlet. Output. Stream, and a Writer through which the servlet can send the reply data. • Subclasses of Servlet. Response give the servlet more protocol-specific capabilities. For example, Http. Servlet. Response contains methods that allow the servlet to manipulate HTTP-specific header information. 26 th February 2004 Bogdan L. Vrusias © 2004 5
Introduction to Server-Side Web Development Servlet Lifecycle I • When a server loads a servlet, it runs the servlet's init() method. Even though most servlets are run in multi-threaded servers, there are no concurrency issues during servlet initialization. This is because the server calls the init method once, when it loads the servlet, and will not call it again unless it is reloading the servlet. • The server can not reload a servlet until after it has removed the servlet by calling the destroy() method. • After the server loads and initializes the servlet, the servlet is able to handle client requests. It processes them in its service() method. Each client's request has its call to the service method run in its own servlet thread: the method receives the client's request, and sends the client its response. 26 th February 2004 Bogdan L. Vrusias © 2004 6
Introduction to Server-Side Web Development Servlet Lifecycle II • Servlets run until they are removed from the service, by calling the servlet's destroy() method. The method is run once; the server will not run it again until after it reloads and reinitializes the servlet. • NOTE: Servlets can run multiple service methods at a time. It is important, therefore, that service methods be written in a thread-safe manner. If, for some reason, a server should not run multiple service methods concurrently, the servlet should implement the Single. Thread. Model interface. 26 th February 2004 Bogdan L. Vrusias © 2004 7
Introduction to Server-Side Web Development Interacting with Clients I • Servlet writers who are developing HTTP servlets that specialize the Http. Servlet class should override the method or methods designed to handle the HTTP interactions that their servlet will handle. The candidate methods include: – – do. Get, for handling GET, conditional GET and HEAD requests do. Post, for handling POST requests do. Put, for handling PUT requests do. Delete, for handling DELETE requests 26 th February 2004 Bogdan L. Vrusias © 2004 8
Introduction to Server-Side Web Development Interacting with Clients II • An Http. Servlet. Request object provides access to HTTP header data, such as any cookies found in the request and the HTTP method with which the request was made. It allows the you to obtain the arguments that the client sent as part of the request: – For any HTTP method, you can use the get. Parameter. Values() method, which will return the values of a named parameter. The method get. Parameter. Names() provides the names of the parameters. • For responding to the client, an Http. Servlet. Response object provides ways of returning the response data to the user. You can use the writer returned by the get. Writer() method to return text data to the user. 26 th February 2004 Bogdan L. Vrusias © 2004 9
Introduction to Server-Side Web Development Servlet Example I import javax. servlet. *; import java. io. *; import java. util. *; public class Simple. Servlet extends Generic. Servlet { public void service(Servlet. Request request, Servlet. Response response) throws Servlet. Exception, IOException { response. set. Content. Type("text/html"); print. Writer pw = response. get. Writer(); pw. println("<HTML><BODY>"); for(int counter = 1; counter <= 10; counter++) { pw. println(counter + " Mississippi, <BR>"); } pw. println("<B>Ready or not, here I come!</B>"); pw. println("</BODY></HTML>"); pw. close(); } } 26 th February 2004 Bogdan L. Vrusias © 2004 10
Introduction to Server-Side Web Development Reading Form Data Example - GET <FORM ACTION="/servlets/aservlet" METHOD="GET"> <INPUT TYPE="HIDDEN" NAME="this. Is. AKey" Value="x"> <INPUT TYPE="HIDDEN" NAME="another. Key" VALUE="y"> <INPUT TYPE="SUBMIT" VALUE="Go. . "> </FORM> When submitted, the browser would execute the following URL: http: //www. someserver. com/servlets/aservlet? this. Is. AKey= x&another. Key=y Alternatively <A HREF='/servlets/aservlet? this. Is. AKey=x&another. Key=y'> 26 th February 2004 Bogdan L. Vrusias © 2004 11
Introduction to Server-Side Web Development Reading Form Data Example - POST The HTML <FORM ACTION="/servlet/My. Name. Servlet" METHOD="POST"> Please Enter your name <INPUT TYPE="TEXT" NAME="yourname"> <INPUT TYPE="SUBMIT" VALUE="Submit"> </FORM> 26 th February 2004 Bogdan L. Vrusias © 2004 12
Introduction to Server-Side Web Development Reading Form Data Example - POST The Servlet. . . public void do. Post(Http. Servlet. Request req, Http. Servlet. Response res) throws Servlet. Exception, IOException { String name=req. get. Parameter. Values("yourname")[0]; String reply="<HTML>n"+ "<HEAD><TITLE>My Name Servlet Response</TITLE></HEAD>n"+ "<BODY>n"+ "<CENTER><B>n"+ "Hello "+name+"n"+ "</B></CENTER>n</BODY>n</HTML>"; res. set. Content. Type("text/html"); Print. Writer out=res. get. Writer(); out. println(reply); out. close(); }. . . 26 th February 2004 Bogdan L. Vrusias © 2004 13
Introduction to Server-Side Web Development Demo • Some examples will now be demonstrated using the Tomcat server… 26 th February 2004 Bogdan L. Vrusias © 2004 14
Introduction to Server-Side Web Development Session: Closing • • Questions? ? ? Remarks? ? ? Comments!!! Evaluation! 26 th February 2004 Bogdan L. Vrusias © 2004 15
- Slides: 15