UML Weekend Crash Course⢠- To Parent Directory
UML Weekend Crash Course⢠- To Parent Directory UML Weekend Crash Course⢠- To Parent Directory
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.
- Page 253 and 254: 230 Sunday Morning Figure 22-4 repr
- Page 255 and 256: 232 Sunday Morning A substate is a
- Page 257 and 258: 234 Sunday Morning Monitor when tem
- Page 260 and 261: SESSION 23 Applying the Extended St
- Page 262 and 263: Session 23—Applying the Extended
- Page 264 and 265: Session 23—Applying the Extended
- Page 266 and 267: Session 23—Applying the Extended
- Page 268 and 269: SESSION 24 Modeling the Development
- Page 270 and 271: Session 24—Modeling the Developme
- Page 272 and 273: Session 24—Modeling the Developme
- Page 274 and 275: Session 24—Modeling the Developme
- Page 276 and 277: Session 24—Modeling the Developme
- Page 278 and 279: SESSION 25 Modeling the Static View
- Page 280 and 281: Session 25—Modeling the Static Vi
- Page 282 and 283: Session 25—Modeling the Static Vi
- Page 284 and 285: Session 25—Modeling the Static Vi
- Page 286 and 287: SESSION 26 Modeling the Static View
- Page 288 and 289: Session 26—Modeling the Static Vi
- Page 290 and 291: Session 26—Modeling the Static Vi
- Page 292 and 293: Session 26—Modeling the Static Vi
- Page 294 and 295: Session 26—Modeling the Static Vi
- Page 296 and 297: PART # V Sunday Morning Part Review
- Page 299 and 300: PART VI Sunday Afternoon Session 27
- Page 301 and 302: 278 Sunday Afternoon design, and mo
- Page 303: 280 Sunday Afternoon :User :Web Br
- Page 307 and 308: 284 Sunday Afternoon and time. A JS
- Page 310 and 311: SESSION 28 Analysis and Architectur
- Page 312 and 313: Session 28—Analysis and Architect
- Page 314 and 315: Session 28—Analysis and Architect
- Page 316 and 317: Session 28—Analysis and Architect
- Page 318 and 319: Session 28—Analysis and Architect
- Page 320 and 321: SESSION 29 Design of a Web Applicat
- Page 322 and 323: Session 29—Design of a Web Applic
- Page 324 and 325: Session 29—Design of a Web Applic
- Page 326 and 327: Session 29—Design of a Web Applic
- Page 328 and 329: Session 29—Design of a Web Applic
- Page 330 and 331: SESSION 30 UML Modeling Tools Sessi
- Page 332 and 333: Session 30—UML Modeling Tools 309
- Page 334 and 335: Session 30—UML Modeling Tools 311
- Page 336 and 337: Session 30—UML Modeling Tools 313
- Page 338 and 339: PART VI # Sunday Afternoon Part Rev
- Page 340 and 341: APPENDIX A Answers to Part Reviews
- Page 342 and 343: Answers to Part Reviews 319 20. I a
- Page 344 and 345: Answers to Part Reviews 321 26. It
- Page 346 and 347: Answers to Part Reviews 323 22. Dra
- Page 348 and 349: Answers to Part Reviews 325 Sunday
- Page 350 and 351: Answers to Part Reviews 327 It maps
- Page 352 and 353: APPENDIX B What’s on the CD-ROM T
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