Presentation is loading. Please wait.

Presentation is loading. Please wait.

Distributed Web Systems Java Servlets Lecturer Department University.

Similar presentations


Presentation on theme: "Distributed Web Systems Java Servlets Lecturer Department University."— Presentation transcript:

1 Distributed Web Systems Java Servlets Lecturer Department University

2 Outline Motivation Previous technologies Java servlets, advantages Details

3 Example application scenario Booking flights on-line: Airline web server User’s PC Airline reservations database Hotel reservation server VISA payment processing

4 Motivation Have to build Web pages dynamically: –Web page is based on data sent by the client E.g. your travel dates and preferences –Web page is derived from data that changes frequently E.g. seats availability, special hotel offers –Web page uses information from corporate databases or other server-side sources Reservations databases, banking systems, etc

5 Overall idea Simply put, writing servlets and JSPs is about making programs that make Web pages: call a program and pass it request details return web page (HTML) servlet request a web page return HTML Web server User’s PC Server-side resources call necessary services, obtain necessary data

6 Previous technologies CGI (common gateway interface) –One of the most common of the server-side web technologies –A CGI program can be written in just about any language, though the most popular language for CGI programming is Perl –Web servers as a gateway between the user request and the data that it requires: create a new process in which the program will be run load any required runtime environments as well as the program itself Finally, pass in a request object and invoke the program –When the program is finished, the web server will read the response from standard output

7 Previous technologies (contd.) ASP.NET (active server pages): –Microsoft production: combines HTML, scripting, and server-side components in one file called an Active Server Page (ASP) –When the server receives a request for an ASP file, it will first look for the compiled page and then execute it –If the file has not yet been compiled, the server will go ahead and compile and run it –An Active Server Page can be written using HTML, JScript, and VBScript –Through scripting, the Active Server Page can access server-side components –These components can be written in any language as long as it presents a COM (Microsoft's component specification) interface

8 Problems Doesn't scale well (CGI) –Each time a request is received by the web server, an entire new process is created Platform-dependent (ASP) –Can only be used with Microsoft web servers –There are ports to other platforms and web servers, but the lack of wide COM support reduces their effectiveness Maintenance nightmare (ASP) –mix of script and HTML, basically two sets of information threaded together

9 Java Servlets … and then the Sun shone –Servlet – a Java program executed on the Web server (within a servlet container) Servlet’s job: –Read explicit data sent by client (form data) –Read implicit data sent by client (request headers) –Generate the results –Send the explicit data back to client (HTML) –Send the implicit data to client (status codes and response headers)

10 Advantages of servlets Efficient –Threads instead of OS processes, one servlet copy, persistence Convenient –Lots of high-level utilities Powerful –Sharing data, pooling, persistence Portable –Run on virtually all operating systems and servers Secure –No shell escapes, no buffer overflows Inexpensive –There are plenty of free and low-cost servers.

11 Advantages of servlets (contd.) Supports the Model-View-Controller design pattern –Separate data (model) and user interface (view) concerns –Better suited for large-scale application development Also adopted by more recent web programming technologies, like Ruby on Rails –Next-generation web applications framework –Even shorter application development time using modern ideas like “scaffolding”, “convention over configuration”, “simple installation”, etc.

12 Web application Servlets: detailed scenario URL: http://mywebsite.com:8080/myapp/hello Web server User’s PC Servlet container Web application servlet Web application Static web pages and other resources (images etc)

13 Recall HelloWorld in Java… import java.io.*; public class HelloWorld { public static void main(String args[]) { System.out.println(“Hello World !!!”) } This class links your program to the terminal (console)

14 Input/output in servlets Servlets communicate with servlet containers (and hence with the user) via two objects: –HttpServletRequest (for input data) –HttpServletResponse (for output data) HttpServletResponse User’s PC Servlet container Servlet HttpServletRequest PrintWriter

15 Basic servlet structure import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class ServletTemplate extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Use "request" to read incoming HTTP headers // (e.g. cookies) and HTML form data (query data) // Use "response" to specify the HTTP response status // code and headers (e.g. the content type, cookies). PrintWriter out = response.getWriter(); // Use "out" to send content to browser } the actual logic is somewhere here

16 Now HelloWorld servlet … import java.io.*; import javax.servlet.*; import javax.servlet.http.*; public class HelloWorld extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { PrintWriter out = response.getWriter(); out.println("Hello World"); }

17 Really, should output HTML… public class HelloWWW extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html"); PrintWriter out = response.getWriter(); out.println(" \n" + " Hello WWW \n" + " \n" + " Hello WWW \n" + " "); } Always do this BEFORE printing out HTML!

18 Servlets: terminology Servlet = Java program Servlet container –software responsible for providing execution environment for servlets (forward requests to the appropriate servlets, get back the results, etc) –Examples: Caucho Resin, Macromedia JRun, Apache Tomcat Web application –a set of related servlets, JSPs, and other resources like HTML pages, images, etc Context path –part of the URL that tells the servlet container which Web application to forward the request to (e.g. “/myapp” in our example below) –http://mywebsite.com:8080/myapp/hello

19 Installing Web applications How to install a new Web application into a servlet container? Need to specify three things: –What servlets, JSPs and other files it consists of –What context path the container should use for it –Mapping – how to map URLs to particular servlets within application, e.g. specify that http:// / /my/great/servlet should call servlet class HelloWorld mapping

20 Servlet life-cycle Basically, the lifecycle of a servlet is as follows: –The servlet container creates an instance of the servlet –The container calls the instance's init( ) method –If the container has a request for the servlet, it creates a new thread and calls the instance's service( ) method in that thread –Before destroying the instance, the container calls its destroy( ) method –The instance is destroyed and marked for garbage collection And that's it! (Well, actually for efficiency reasons containers usually do not create a thread for each new request, but have a pool of threads to use…)

21 The service() method Dispatcher of HTTP requests: –Determines the type of request (GET, POST, HEAD, OPTIONS, DELETE, PUT or TRACE) and calls the associated method: doGet( ), doPost( ), doHead( ), doOptions( ), doDelete( ), doPut( ) or doTrace( ) Overwrite the doXxx() methods as needed, NOT the service() method! Input parameters are the same for all doXxx(): –HttpServletRequest request, HttpServletResponse response Standard (not overridden) doXxx() methods will return an HTTP error saying that that method is not valid for this resource

22 doGet() and doPost() Since GET and POST are the main types of HTTP requests, normally need to worry only about –doGet(): processes GET requests –doPost(): processes POST requests What’s the difference? –the way input request parameters are passed (more in the “Handling client request & response data” lecture!) If want to do the same for both, call doGet() from doPost() (or vice versa)

23 Debugging servlets Unfortunately, unavoidable for sizeable complex applications Where to get more debugging information? –User debugging print statements –Look at the HTML source (use “View Source” in the browser) –Use the container’s log files –Return error pages to the client – plan ahead for missing or malformed data –Look at the request data separately (see EchoServer at www.coreservlets.com)

24 Summary Servlets – yet another technology for generating dynamic Web pages –But, with some advantages over CGI, ASP, PHP, etc Servlet = Java program that –Receives HTTP request –… –Prints out HTML in HTTP response Questions


Download ppt "Distributed Web Systems Java Servlets Lecturer Department University."

Similar presentations


Ads by Google