UML Weekend Crash Course™ - To Parent Directory

UML Weekend Crash Course™ - To Parent Directory UML Weekend Crash Course™ - To Parent Directory

crnarupa.singidunum.ac.rs
from crnarupa.singidunum.ac.rs More from this publisher
01.01.2015 Views

Session 27—Introduction to Web Development with Java 281 need to have some programming logic that generated the HTML for the HTTP response. The same is true of any e-commerce site where you want to be able to do activities such as search for items, place items in a shopping cart, or make purchases. All those activities require some code on the Web server that reacts to the HTTP request by completing some processing and then returning Web content such as HTML or XML. In reality, almost all modern Web sites include at least a few pages of dynamically generated content. Consider further how a weather Web site might be implemented. For a moment, if you ignore the Web aspect of this system, imagine how you could write code in almost any programming language to query a database or other information source to gather data on the weather, calculate statistics, generate weather map images, and produce HTML of the results. The only problems that need to be solved are to find a way to trigger that code with an HTTP request arriving at the Web server, and then to have the HTML results placed in an HTTP response and have the HTTP response sent to the Web browser. CGI scripts and Java servlets are two technologies that solve this problem by hooking your code into the HTTP Web protocol. This general solution is shown in the UML Deployment diagram in Figure 27-3. The diagram refers to your weather reporting code as the Web application code. This code that you have written is just normal code that can do almost any of the normal things you can do in that language, usually including talking to resources such as databases, making calculations, sending e-mails, and outputting HTML, which is included in the HTTP response. You place this code on the Web server and map it to a particular URL. The HTTP protocol will be used in the same way that it was for static content: The Web browser generates an HTTP request object corresponding to the URL entered by the user, and sends that HTTP request to the Web server. The Web server recognizes that this URL was mapped to your Web application code and calls your code to go gather the data and output the Web content into the HTTP response, which is sent back to the Web browser. Client Web Browser Web Server Web Server Database Server Database Web Application Code Figure 27-3 UML Deployment diagram for dynamic Web content CGI Scripts were the original solution to the problem of generating dynamic Web content. They allow you to write scripts in a wide variety of languages, including Perl and C. They then provide the mechanism for hooking your script into the HTTP request/response mechanism. CGI Scripts were very commonly used and are still used frequently, although they are gradually being used less in favor of a variety of newer solutions I discuss in the next section. Java servlets Java servlets provide the same basic functionality as CGI scripts. They allow you to write a class in Java that will generate the dynamic Web content. Listing 27-2 shows how a Web page that displays the current date could be created as a Java servlet. If you haven’t

282 Sunday Afternoon studied Java programming, this example may seem a bit confusing. But the important thing to notice is not the syntax, but how the HTML is generated and placed into the HTTP response (the HTML is the bold text in Listing 27-2). Notice that the servlet is a class and, in this case, it only has one method, which is called doGet. The servlet container will call this method to service the request whenever a Web browser sends an HTTP request for this servlet. The doGet method always takes two parameters that represent the HTTP request coming in and the HTTP response going out. The variable named out is a reference that allows you to write content to the body of the HTTP response. Thus, all the HTML and other content in the out.println method calls is placed into the HTTP response and sent by the Web server back to the Web browser, which displays the HTML. In this case, the Web content is almost all static content, except that it places “new java.util.Date()” into the output. This puts the current date and time onto the Web page. Notice that all the handling of the HTTP response and HTTP request is done behind the scenes. All you have to write is how you want to service the request. Servicing the request could include querying or updating databases, sending e-mails, calling legacy code in other languages using CORBA, or a wealth of other activities that may be accomplished with Java. Listing 27-2 Java Servlet 1 import javax.servlet.http.*; 2 import java.io.*; 3 import java.util.*; 4 5 public class DateServlet extends HttpServlet { 6 public void doGet(HttpServletRequest request, 7 HttpServletResponse response) 8 throws IOException { 9 10 response.setContentType(“text/html”); 11 PrintWriter out = response.getWriter(); 12 13 out.println(“”); 14 out.println(“”); 15 out.println(“Current Date and Time”); 16 out.println(“”); 17 out.println(“”); 18 out.println(new Date()); 19 out.println(“”); 20 out.println(“”); 21 22 out.close(); 23 } 24} Java servlets offer a powerful technology for developing dynamic Web content and complete Web applications. Java servlets are simply regular Java classes that follow a few special rules. As a result, servlets have all the traditional benefits of Java. These benefits include cross-platform code that can run on any kind of computer with a Java Virtual Machine (JVM), a fully object-oriented language, and massive libraries of pre-written code to simplify tasks such as database access and remote method invocation.

Session 27—Introduction to Web Development with Java 281<br />

need to have some programming logic that generated the HTML for the HTTP response. The<br />

same is true of any e-commerce site where you want to be able to do activities such as<br />

search for items, place items in a shopping cart, or make purchases. All those activities<br />

require some code on the Web server that reacts to the HTTP request by completing some<br />

processing and then returning Web content such as HTML or XML. In reality, almost all<br />

modern Web sites include at least a few pages of dynamically generated content.<br />

Consider further how a weather Web site might be implemented. For a moment, if you<br />

ignore the Web aspect of this system, imagine how you could write code in almost any programming<br />

language to query a database or other information source to gather data on the<br />

weather, calculate statistics, generate weather map images, and produce HTML of the<br />

results. The only problems that need to be solved are to find a way to trigger that code with<br />

an HTTP request arriving at the Web server, and then to have the HTML results placed in an<br />

HTTP response and have the HTTP response sent to the Web browser. CGI scripts and Java<br />

servlets are two technologies that solve this problem by hooking your code into the HTTP<br />

Web protocol.<br />

This general solution is shown in the <strong>UML</strong> Deployment diagram in Figure 27-3. The diagram<br />

refers to your weather reporting code as the Web application code. This code that you<br />

have written is just normal code that can do almost any of the normal things you can do in<br />

that language, usually including talking to resources such as databases, making calculations,<br />

sending e-mails, and outputting HTML, which is included in the HTTP response. You<br />

place this code on the Web server and map it to a particular URL. The HTTP protocol will be<br />

used in the same way that it was for static content: The Web browser generates an HTTP<br />

request object corresponding to the URL entered by the user, and sends that HTTP request<br />

to the Web server. The Web server recognizes that this URL was mapped to your Web application<br />

code and calls your code to go gather the data and output the Web content into the<br />

HTTP response, which is sent back to the Web browser.<br />

Client<br />

Web<br />

Browser<br />

<br />

Web Server<br />

Web<br />

Server<br />

<br />

Database Server<br />

Database<br />

Web<br />

Application<br />

Code<br />

Figure 27-3 <strong>UML</strong> Deployment diagram for dynamic Web content<br />

CGI Scripts were the original solution to the problem of generating dynamic Web content.<br />

They allow you to write scripts in a wide variety of languages, including Perl and C. They then<br />

provide the mechanism for hooking your script into the HTTP request/response mechanism.<br />

CGI Scripts were very commonly used and are still used frequently, although they are gradually<br />

being used less in favor of a variety of newer solutions I discuss in the next section.<br />

Java servlets<br />

Java servlets provide the same basic functionality as CGI scripts. They allow you to write<br />

a class in Java that will generate the dynamic Web content. Listing 27-2 shows how a<br />

Web page that displays the current date could be created as a Java servlet. If you haven’t

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!