An introduction to web programming with Java

14  Download (0)

Full text

(1)

Chapter 1

An introduction to

web programming

with Java

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

with Java

Objectives

Knowledge • 1DPHWKHVRIWZDUHFRPSRQHQWWKDWLVUHTXLUHGRQWKHFOLHQWRI DQ\,QWHUQHWDSSOLFDWLRQ • 1DPHWKHWZRVRIWZDUHFRPSRQHQWVWKDWDUHXVXDOO\UHTXLUHG RQWKHVHUYHURIDQ\,QWHUQHWDSSOLFDWLRQ • 'LVWLQJXLVKEHWZHHQ+70/DQG+773 'LVWLQJXLVKEHWZHHQVWDWLFZHESDJHVDQGG\QDPLFZHESDJHV

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

• 'LVWLQJXLVKEHWZHHQVWDWLFZHESDJHVDQGG\QDPLFZHESDJHV • 'HVFULEHWKHVRIWZDUHFRPSRQHQWVWKDWDUHUHTXLUHGIRU GHYHORSLQJVHUYOHWDQG-63DSSOLFDWLRQV • ,QJHQHUDOWHUPVGLVWLQJXLVKEHWZHHQWKHFRGHIRUDVHUYOHWDQG D-63 • ,QJHQHUDOWHUPVH[SODLQZK\\RXW\SLFDOO\XVHERWKVHUYOHWV DQG-63VLQD-DYDZHEDSSOLFDWLRQ

(2)

Objectives (continued)

• 'HVFULEHWKHWKUHHGHYHORSPHQWHQYLURQPHQWVWKDWFDQEHXVHG IRUGHYHORSLQJZHEDSSOLFDWLRQV • /LVWWKHVRIWZDUHFRPSRQHQWVWKDW\RXQHHGIRUUXQQLQJ VHUYOHWVDQG-63VRQ\RXURZQ3& • /LVWDQGGHVFULEHWKHWKUHHOD\HUVRIDW\SLFDO-DYDZHE DSSOLFDWLRQ

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

The first page of a shopping cart application

(3)

The second page of a shopping cart application

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

Components of a web application

Internet connection Client computer

Server computer

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

Web browser Web server

(4)

The components of a web application

• :HEDSSOLFDWLRQVDUHDW\SHRIFOLHQWVHUYHUDSSOLFDWLRQ • ,QDFOLHQWVHUYHUDSSOLFDWLRQDXVHUDWDFOLHQWFRPSXWHUDFFHVVHV DQDSSOLFDWLRQDWDVHUYHUFRPSXWHU • )RUDZHEDSSOLFDWLRQWKHFOLHQWDQGVHUYHUFRPSXWHUVDUH FRQQHFWHGYLDWKH,QWHUQHWRUDQLQWUDQHW • ,QDZHEDSSOLFDWLRQWKHZHEEURZVHUSURYLGHVWKHXVHULQWHUIDFH IRUWKHDSSOLFDWLRQ

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

IRUWKHDSSOLFDWLRQ

• $ZHEDSSOLFDWLRQUXQVRQWKHVHUYHUFRPSXWHUXQGHUWKHFRQWURO RIZHEVHUYHUVRIWZDUH

• )RUPRVWZHEDSSOLFDWLRQVWKHVHUYHUFRPSXWHUDOVRUXQVD GDWDEDVHPDQDJHPHQWV\VWHP '%06 

How a web server processes static web pages

Client Server

Browser Web server HTML file

HTTP request

HTTP response

(5)

How static web pages work

• +\SHUWH[W0DUNXS/DQJXDJHRU+70/LVWKHODQJXDJHWKDWWKH ZHEEURZVHUFRQYHUWVLQWRWKHZHESDJHVRIDZHEDSSOLFDWLRQ • $VWDWLFZHESDJHLVDQ+70/GRFXPHQWWKDW¶VVWRUHGLQDILOH DQGGRHVQRWFKDQJHLQUHVSRQVHWRXVHULQSXW • +\SHUWH[W7UDQVIHU3URWRFRORU+773LVWKHSURWRFROWKDWZHE EURZVHUVDQGZHEVHUYHUVXVHWRFRPPXQLFDWH • $ZHEEURZVHUUHTXHVWVDSDJHIURPDZHEVHUYHUE\VHQGLQJWKH

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

$ZHEEURZVHUUHTXHVWVDSDJHIURPDZHEVHUYHUE\VHQGLQJWKH VHUYHUDPHVVDJHNQRZQDVDQ+773UHTXHVW)RUDVWDWLFZHE SDJHWKH+773UHTXHVWLQFOXGHVWKHQDPHRIWKH+70/ILOH WKDW¶VUHTXHVWHG • $ZHEVHUYHUUHSOLHVWRDQ+773UHTXHVWE\VHQGLQJDPHVVDJH NQRZQDVDQ+773UHVSRQVHEDFNWRWKHEURZVHU)RUDVWDWLFZHE SDJHWKH+773UHVSRQVHLQFOXGHVWKH+70/GRFXPHQW

How a web server processes dynamic web pages

Client Server

Browser Web server applicationWeb

HTTP request

HTTP response

(6)

How dynamic web pages work

• $G\QDPLFZHESDJHLVDQ+70/GRFXPHQWWKDW¶VJHQHUDWHGE\D ZHEDSSOLFDWLRQ2IWHQWKHZHESDJHFKDQJHVDFFRUGLQJWR SDUDPHWHUVWKDWDUHVHQWWRWKHZHEDSSOLFDWLRQE\WKHZHE EURZVHU • :KHQDZHEVHUYHUUHFHLYHVDUHTXHVWIRUDG\QDPLFZHESDJHWKH VHUYHUSDVVHVWKHUHTXHVWWRWKHZHEDSSOLFDWLRQ7KHQWKH DSSOLFDWLRQJHQHUDWHVDUHVSRQVHZKLFKLVXVXDOO\DQ+70/ GRFXPHQWDQGUHWXUQVLWWRWKHZHEVHUYHU

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

GRFXPHQWDQGUHWXUQVLWWRWKHZHEVHUYHU • 7KHZHEVHUYHULQWXUQZUDSVWKHJHQHUDWHG+70/GRFXPHQWLQ DQ+773UHVSRQVHDQGVHQGVLWEDFNWRWKHEURZVHU • 7KHEURZVHUGRHVQ¶WNQRZRUFDUHZKHWKHUWKH+70/ZDV UHWULHYHGIURPDVWDWLF+70/ILOHRUZDVG\QDPLFDOO\JHQHUDWHG E\WKHZHEDSSOLFDWLRQ(LWKHUZD\WKHEURZVHUGLVSOD\VWKH +70/GRFXPHQWWKDWLVUHWXUQHG

The components of a Java web application

Client

Browser

Server

Web server

HTTP request HTTP response

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

Web server

Database server Servlet/JSP engine Java Development Kit (JDK)

(7)

Components needed for Java web application

• -DYDZHEDSSOLFDWLRQVFRQVLVWRI-DYD6HUYHU3DJHVDQGVHUYOHWV • $VHUYOHW-63HQJLQHRUVHUYOHW-63FRQWDLQHULVWKHVRIWZDUH WKDWDOORZVWKHZHEVHUYHUWRZRUNZLWKVHUYOHWVDQG-63V • 7KH-DYD(QWHUSULVH(GLWLRQ -DYD(( VSHFLILFDWLRQGHVFULEHV KRZZHEVHUYHUVFDQLQWHUDFWZLWKVHUYOHW-63HQJLQHV • )RUDVHUYOHW-63HQJLQHWRZRUNLWPXVWKDYHDFFHVVWR-DYD¶V -DYD'HYHORSPHQW.LW -'. ZKLFKFRPHVDVSDUWRIWKH-DYD

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

-DYD'HYHORSPHQW.LW -'. ZKLFKFRPHVDVSDUWRIWKH-DYD 6WDQGDUG(GLWLRQ -DYD6(  • $PRQJRWKHUWKLQJVWKH-'.FRQWDLQVWKHFRUH-DYDFODVV OLEUDULHVWKH-DYDFRPSLOHUDQGWKH-DYD5XQWLPH(QYLURQPHQW -5(  • -DYDZHEDSSOLFDWLRQVWKDWXVH(QWHUSULVH-DYD%HDQV (-%V  UHTXLUHDQDGGLWLRQDOVHUYHUFRPSRQHQWNQRZQDVDQ(-%VHUYHU RU(-%FRQWDLQHU

A JSP that displays three parameters entered by

the user

(8)

An introduction to JavaServer Pages

• $-DYD6HUYHU3DJHRU-63FRQVLVWVRI-DYDFRGHWKDWLVHPEHGGHG ZLWKLQ+70/FRGH • :KHQD-63LVILUVWUHTXHVWHGWKH-63HQJLQHWUDQVODWHVLWLQWRD VHUYOHWDQGFRPSLOHVLW7KHQWKHVHUYOHWLVUXQE\WKHVHUYOHW HQJLQH

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

The code for the JSP

<!DOCTYPE HTML PUBLIC

"-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html>

<head>

<title>Murach's Java Servlets and JSP</title> </head>

<body> <%

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

// get parameters from the request String firstName =

request.getParameter("firstName");

String lastName = request.getParameter("lastName"); String emailAddress =

request.getParameter("emailAddress"); %>

(9)

The code for the JSP (continued)

<p>Here is the information that you entered:</p> <table cellspacing="5" cellpadding="5" border="1"> <tr> <td align="right">First name:</td> <td><%= firstName %></td> </tr> <tr> <td align="right">Last name:</td> <td><%= lastName %></td> </tr>

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

</tr> <tr> <td align="right">Email address:</td> <td><%= emailAddress %></td> </tr> </table>

The code for the JSP (continued)

<p>To enter another email address, click on the Back <br> button in your browser or the Return button shown <br> below.</p>

<form action="join_email_list.html" method="get"> <input type="submit" value="Return">

</form> </body> </html>

(10)

Code for a servlet that works the same as the JSP

package email;

import java.io.*; import javax.servlet.*; import javax.servlet.http.*;

public class DisplayEmailListServlet extends HttpServlet {

protected void doGet(

HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

throws ServletException, IOException {

// get parameters from the request String firstName =

request.getParameter("firstName");

String lastName = request.getParameter("lastName"); String emailAddress =

request.getParameter("emailAddress");

Code for the servlet (continued)

// return response to browser

response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); out.println(

"<!doctype html public \"

+ "-//W3C//DTD HTML 4.0 Transitional//EN\">\n"" + "<html>\n"

+ "<head>\n"

+ " <title>Murach's Java Servlets and JSP</title>\n" + "</head>\n"

+ "<body>\n"

+ "<h1>Thanks for joining our email list</h1>\n"

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

+ "<h1>Thanks for joining our email list</h1>\n" + "<p>Here is the information that you entered:</p>\n" + " <table cellspacing=\"5\" cellpadding=\"5\" " + "border=\"1\">\n" + " <tr><td align=\"right\">First name:</td>\n" + " <td>" + firstName + "</td>\n" + " </tr>\n" + " <tr><td align=\"right\">Last name:</td>\n" + " <td>" + lastName + "</td>\n" + " </tr>\n" + " <tr><td align=\"right\">Email address:</td>\n" + " <td>" + emailAddress + "</td>\n" + " </tr>\n"

(11)

Code for the servlet (continued)

+ " </table>\n"

+ "<p>To enter another email address, click on the Back <br>\n" + "button in your browser or the Return button shown <br>\n" + "below.</p>\n"

+ "<form action=\"join_email_list.html\" >\n" + " <input type=\"submit\" value=\"Return\">\n" + "</form>\n" + "</body>\n" + "</html>\n"); out.close(); } }

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

} JDK Tomcat MySQL Stand-alone development JDK

Java EE JAR files JDK Tomcat

Local Area Network development

Three environments for servlet and JSP development

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

LAN connection

Tomcat MySQL

JDK

Java EE JAR files

JDK Tomcat Apache MySQL Internet development Internet connection Server Server Client Client

(12)

The architecture for a typical Java web application

Presentation layer

HTML files JSP files

Business rules layer

Servlets JavaBeans Other Java classes

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH

Data access layer

Database Data access

classes

Text files Binary files XML files

The architecture for a typical Java web application

(continued)

• 7KHSUHVHQWDWLRQOD\HUIRUDW\SLFDO-DYDZHEDSSOLFDWLRQFRQVLVWV RI+70/SDJHVDQG-63V • 7KHEXVLQHVVUXOHVOD\HUIRUDW\SLFDO-DYDZHEDSSOLFDWLRQ FRQVLVWVRIVHUYOHWV7KHVHVHUYOHWVPD\FDOORWKHU-DYDFODVVHV LQFOXGLQJDVSHFLDOW\SHRI-DYDFODVVNQRZQDVD-DYD%HDQ,Q FKDSWHUVDQG\RX¶OOOHDUQKRZWRXVHVHYHUDOVSHFLDOW\SHVRI WDJVZLWKLQD-63WRZRUNZLWK-DYD%HDQV

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH

WDJVZLWKLQD-63WRZRUNZLWK-DYD%HDQV • 7KHGDWDDFFHVVOD\HUIRUDW\SLFDO-DYDZHEDSSOLFDWLRQFRQVLVWV RIFODVVHVWKDWUHDGDQGZULWHGDWDWKDW¶VVWRUHGRQWKHVHUYHU¶V GLVNGULYH • )RUDVHULRXVZHEDSSOLFDWLRQWKHGDWDLVXVXDOO\VWRUHGLQD UHODWLRQDOGDWDEDVH+RZHYHULWPD\DOVREHVWRUHGLQELQDU\ILOHV LQWH[WILOHVRULQ([WHQVLEOH0DUNXS/DQJXDJH RU;0/ ILOHV

(13)

The NetBeans IDE

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

Popular IDEs for Java web development

• 1HW%HDQV • (FOLSVH • -%XLOGHU • ,QWHOOL-,'($

Description

• $Q,QWHJUDWHG'HYHORSPHQW(QYLURQPHQW ,'( LVDWRROWKDW

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

• $Q,QWHJUDWHG'HYHORSPHQW(QYLURQPHQW ,'( LVDWRROWKDW SURYLGHVDOORIWKHIXQFWLRQDOLW\WKDW\RXQHHGIRUGHYHORSLQJZHE DSSOLFDWLRQV

(14)

An ISP that provides web hosting that supports

servlets and JSPs

0XUDFK¶V-DYD6HUYOHWV-63 QG(G & ‹0LNH0XUDFK $VVRFLDWHV,QF 6OLGH6OLGH

The FileZilla program

Figure

Updating...

References