You are on page 1of 97

o an tot nghiep Gvhd: Ts.

Nguyen Thuc Hai

Phan I:
Chng I:

JSP TECHNOLOGY
CAN BAN VE JSP

JavaServer Pages (JSP) la mot ky thuat server-side do o chung ta khong the thc hien bat ky hanh ong nao client. JSP cho phep chung ta tach thanh phan ong cua trang ra khoi thanh phan tnh HTML. Rat n gian, chung ta ch can viet mot tai lieu HTML bnh thng roi sau o bao quanh ma cua thanh phan ong trong cac tag ac biet, hau het cac tag bat au vi < % va ket thuc vi %>. V du, ay la mot phan cua trang JSP, co ket qua tra ve la Thanks for reading jsp book. vi URL la http://www.jsp.com/thank.jsp?title=jsp Thanks for reading <i><%=request.getParameter(title) %><i>book. Ky thuat JSP la mot thanh phan trong ai gia nh Java; no s dung ngon ng kch ban da vao ngon ng lap trnh Java, va cac trang JSP c bien dch thanh servlets. T o chung ta cung nhan biet c, JSP th khong phu thuoc bat ky nen (platform) nao. No ap ng c khuynh hng cua Sun MicroSystem la write one, run anywhere. Cac trang JSP co the goi cac thanh phan JavaBeans, Enterprise JavaBeans (EJB) hoac custom tags e thc hien cac x ly tren server. Va nh the, ky thuat JSP la thanh phan chu chot trong kien truc kha chuyen cua Java cho nhng ng dung da vao Web. Nh a biet, JSPs se bien dch thanh servlets nhng JSP khong the thay the servlet v cac ly do sau: Mot so tac vu c giai quyet rat tot bang servlet. V du, cac ng dung xuat ra d lieu nh phan hoac ch xac nh ni gi tr lai cho ngi dung (bang cach dung response.sendRedirect) c dung servlet th tot nhat. Mot so tac vu khac lai c giai quyet rat tot bang JSP nh cac tnh huong ma cau truc nen tang cua trang HTML la co nh nhng cac gia tr trong no lai thay oi.

Svth: inh Le Giang Trang 1

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

Con cac tac vu con lai can s ket hp ca servlet va JSP. V du, trong yeu cau goc c tra li bang mot servlet ma thc hien moi cong viec, lu tr cac ket qua trong cac Beans va ieu phoi yeu cau nay en mot trong nhng trang JSP co the hien th no.

Ca ba nh hng nay eu co cho ng cua no. Chang co nh hng nao ho tr ay u cho moi ng dung . Trc khi i vao chi tiet cong nghe JSP, chung toi co mot vai so sanh cac cong nghe c s dung trong ng dung vi cac cong nghe khac.

I.Mot so so sanh cac cong nghe c s dung vi cong nghe khac?


1. MySQL Server. MySQL la he quan tr c s d lieu quan he. C s d lieu la mot tap d lieu co cau truc va c lu tr trong cac bang rieng biet, moi bang c lu trong ba files vi ten c s d lieu la ten th muc cha cac files o. SQL c viet tat t Structured Query Language, la ngon ng chuan pho bien nhat e truy cap c s d lieu. Sau ay la mot so u va nhc iem cua MySQL vi cac he quan tr c s d lieu khac: a) u diem. Chay c tren rat nhieu nen khac nhau nh Unix, Windows, MacOS, . MySQL la he quan tr nhanh, nho gon. Cac script files co the chay tren mot so he quan tr khac nh MS SQL Server, Oracle. Theo mot so web site thong ke, viec thc hien cac lenh insert, update, delete nhanh nhat trong cac he quan tr. Mien ph va ma nguon m (open source code). b) Khuyet iem.
Svth: inh Le Giang Trang 2

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

Cha ho tr mot so chuan ANSI SQL92 nh khong cho cac cau select long nhau, select into table, khoa ngoai, triggers, stored procedures, Khong co moi trng o hoa. JSP vi ASP.

a)

2. So sanh JSP vi cac cong nghe khac. ASP la cong nghe tng ng t Microsoft. JSP co ba li the so vi ASP. Phan ong c viet bang Java, ch khong phai bang cac ngon ng script nh VBScript, JavaScript. V the no manh me hn tot hn oi vi cac ng dung phc tap can cac thanh phan s dung lai. JSP chay c tren nhieu he ieu hanh va web servers khac nhau ngay ca vi IIS cua Microsoft (can co plugins t Webphere, JRun, ...) Ho tr s m rong tag vi custom tag. b)JSP vi PHP. Li iem cua JSP vi PHP cung nh vi ASP. JSP c viet bang Java ma chung ta a biet vi cac API m rong cho mang, truy cap c s d lieu, cac oi tng phan tan, trong khi vi PHP oi hoi chung ta phai hoc ca mot ngon ng mi. 3. Tam quan trong cua XML cung vi XSLT. a)XML. XML la tap con cua SGML, no ket hp tnh linh ong va sc manh cua SGML cung vi mot so tnh nang hu ch cua HTML. V XML la tap con cua SGML nen no cung tng thch vi cac he thong da vao SGML a co. XML la lc ay cho cac nghi thc tren internet va cac phan mem e de dang x ly va truyen d lieu.
SGML a noi XML la tap con HTML tren cua SGML va HTML la mot s cai at cua SGML, mo hnh sau ay CDF dien ta moi quan he gia cac ngon ng va cac sieu ngon ng: SMIL XML

Svth: inh Le Giang Trang 3


MML

CML

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai Metalanguages


subset

Implementation

Languages

Implementation

CDF : Channel Difinition Format cho phep cac tac gia cua cac Web sites cho phep cac ngi ang ky biet khi nao Web site nay thay oi, CDF a c gii thieu trong IE4 v vay no ch lam viec vi IE cua Microsoft. SMIL : Synchronized Multimedia Integration Language c s dung e ong bo hoa cac dong d lieu a truyen thong c truyen qua internet. CML : Chemical Markup Language mo ta cac cong thc hoa hoc. MML: Mathematical Markup language- mo ta cac phng trnh, bieu thc toan hoc.

Tnh t mo ta d lieu cua XML : cac tags mo ta cac van e, cac oi tng gan gui vi the gii thc. Chung ta se thay ro hn vi hai v du sau, ay la v du minh hoa nen no con thieu nhieu thuoc tnh trong the gii thc : V du vi HTML tags <table> <tr> <td>Giang</td> <td>Le</td> <td>Dinh</td> </tr> <tr> <td>Number id </td> <td>0750</td> </tr> <tr> <td>Age :</td> <td>22</td> Svth: inh Le Giang </tr> Trang 4 </table> V du vi XML tags <student> <name> <first>Giang</first> <middle>Le</middle> <last>Dinh</last> </name> : <numberid>0750</numberid> <age>22</age> </student>

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

- XML co moi quan he chat che vi JSP, at ta JSP ho tr cu phap XML, chung ta co the tron lan ma chung vi nhau de dang nh JSP vi HTML nh the chung ta co the dung JSP t ong phat sinh trang XML. - Hien nay co rat nhieu san pham quan tr c s d lieu ma moi san pham eu co cac ac tnh rieng no, do o khi cac c s d lieu khac nhau can chuyen oi d lieu vi nhau lai khong tng thch. V vay ngi lam cong viec nay can phai biet nhieu san pham quan tr c s d lieu khac nhau. Do o XML la mot nh dang chuan ma cac he quan tr can ho tr. Hien nay co nhieu he quan tr ln ho tr XML nh MS SQL Server, Oracle, b) XSLT. Extensible Stylesheet Language Transformation (XSLT) la mot ngon ng c tach ra t ngon ng XSL, XSLT la mot ngon ng dung e chuyen oi cac XML document thanh cac XML document khac. No c viet bang XML, ieu nay co ngha rang s chuyen oi trong XSLT c trnh bay nh mot well-formed XML document. XSLT ong vai tro quan trong trong hng phat trien phat sinh ngon ng markup hng ngi dung.

II. JSP scripting elements.


Cac scripting elements trong JSP cho phep chung ta chen ma vao servlet ma se c phat sinh t trang JSP. Co ba dang sau: - Bieu thc co dang <%= expressions %>, c nh gia tr va chen vao luong xuat cua servlet. - Scriptlet co dang <% code %>, c chen vao phng thc _jspService cua servlet (c goi la service). - Khai bao co dang <%! code %>, c chen vao than cua lp servlet, nh la cac field cua lp thong thng. Template text

Svth: inh Le Giang Trang 5

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

Trong nhieu trng hp, phan ln cac trang JSP ch bao gom HTML tnh, c biet nh la template text. Co hai ngoai le phu cho quy tac template text c chuyen thang sang HTML tnh. au tien, neu chung ta muon co <% trong luong xuat th chung ta can phai at <\% trong template text. Th hai, neu chung ta muon co chu thch trong JSP ma khong co trong tai lieu ket qua, dung: <%-- JSP comments --%> Chu thch HTML co dang: <!-- HTML comments --> th c chuyen qua tai lieu HTML thong thng. Cu phap XML Trong JSP co rat nhieu element co cu phap XML nh jsp:useBean, jsp:include, jsp:setProperty, ... Tuy nhien scripting elements lai co hai dang cu phap sau: JSP Syntax XML Syntax

<% = Expressions %> <jsp:expression> Java code </jsp:expression> <% Scriptlets %> <jsp:scriptlet> Java code </jsp:scriptlet>

<%! Declarations %> <jsp:declaration> Java code </jsp:declaration> 1. Cac bien c nh ngha san trong JSP e n gian hoa ma trong cac bieu thc hay scriptlets trong JSP, ngi ta cung cap cho chung ta chn oi tng a c nh ngha trc, co ngi con goi la cac oi tng ngam nh. Do cac khai bao trong JSP nam ngoai phng thc _jspService (c goi bi service) nen cac oi tng nay khong cho phep cac khai bao truy cap vao. request Bien nay co kieu la javax.servlet.http.HttpServletRequest, co pham vi
Svth: inh Le Giang Trang 6

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

trong mot yeu cau (request). No cho phep chung ta truy cap vao cac tham so cua request nh loai request (GET, POST, ) va cac incoming HTTP header (cookies). response Co kieu la javax.servlet.http.HttpServletResponse, co pham vi toan trang (page). Chu y rang v luong xuat th thng lam vat em cho nen viec gan ma tnh trang cua HTTP va response header th hp ly trong JSP, mac du ieu nay th khong c phep trong servlet mot khi a co luong xuat nao c gi en client. out Co kieu javax.servlet.jsp.JspWriter va pham vi trong mot trang (page). Dung e gi cac thong xuat en client. oi tng out c dung thng xuyen trong scriptlets, cac bieu thc t ong c a vao luong xuat nen hiem khi can tham chieu en oi tng nay. session Co pham vi trong mot phien truyen (session) va kieu tng ng la javax.servlet.http.HttpSession. Goi ve cac phien truyen c tao t ong v the bien nay van con ket noi ngay ca chang co mot tham chieu incoming session nao. Mot ngoai le la neu chung ta s dung thuoc tnh session cua page directive e tac cac phien truyen, ma lai co tham chieu en bien session th se gay ra cac loi vao luc trang JSP c dch thanh servlet. application Bien nay co kieu la javax.servlet.ServletContext, co pham vi trong toan ng dung (application). ServletContext lay t mot oi tng cau hnh servlet la getServletConfig().getContext(). Cac trang JSP co the lu tr d lieu persistent trong oi tng ServletContext tot hn la trong cac bien the hien. ServletContext co cac phng thc
Svth: inh Le Giang Trang 7

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

setAttribute va setAttribute ma cho phep chung ta lu tr d lieu config Bien nay co pham vi trang (page) va co kieu tng ng la javax.servlet.ServletConfig. pageContext Bien nay co kieu la javax.servlet.jsp.PageContext va co pham vi la trang (page). pageContext cho phep mot iem truy cap duy nhat ti nhieu thuoc tnh cua trang va cung cap mot ni thuan tien e lu tr d lieu dung chung. Bien pageContext lu tr gia tr cua javax.servlet.jsp.PageContext cung vi trang hien hanh. Co the xem chi tiet trong chng II ve JavaBeans. page Bien nay ong ngha vi this va ieu nay th khong hu ch trong ngon ng lap trnh Java, co kieu la java.lang.Object va co pham vi trang (page). exception Trong mot trang loi, chung ta co the truy cap vao oi tng exception. Bien nay co kieu la java.lang.Throwable va pham v la trang (page). 2. Bieu thc trong JSP Bieu thc trong JSP c dung e chen cac gia tr trc tiep vao luong xuat. No co dang sau: <%= Java expressions %> Bieu thc nay c nh tr, c chuyen thanh chuoi, va c chen vao trong trang. S nh tr dien ra thi gian runtime (khi trang c yeu cau) va do o co ay u quyen truy cap cac thong tin cua yeu cau nay. V du sau ay mo ta ngay/gi ma trang c yeu cau: Current time: <%= new java.util.Date() %> Trong Java moi cau lenh eu co dau ; ket thuc dong. Tai sao bieu thc trong JSP lai khong co dau ;? V bieu thc nay c a vao luong xuat chang han nh
Svth: inh Le Giang Trang 8

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

PrintWriter. Vi v du tren co the chuyen vao servlet nh sau: PrintWriter out = response.getWriter(); out.println(Current time: + new java.util.Date()); a)Bieu thc nh la gia tr trong elements khac. Bieu thc co the c s dung trong cac thuoc tnh cua cac elements khac. Gia tr t cac bieu thc nay se c tnh vao thi gian yeu cau (request time). Cac elements cho phep s dung bieu thc trong cac thuoc tnh cua chung la: Ten Element jsp:setProperty jsp:include jsp:forward jsp:param page page value Ten thuoc tnh name va value

b) V du expression.jsp. ay la mot v du hoan chnh au tien cua JSP. No ch hien th thi gian hien thi, ten may,.. khi file nay c truy cap.
Code 1: expression.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <!-- Example of JSP Expressions. --> <html> <head> <title>JSP Expressions</title> <meta name="author" content="Giang" /> <meta name="keywords" content= "JSP,expressions,JavaServer,Pages,servlets" /> <meta name="description" content=
Code 1: expression.jsp (tiep theo)

"A quick example of JSP expressions." /> <link rel="stylesheet" href="JSP-Styles.css" type="text/css" /> Svth: inh Le Giang Trang 9

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai </head> <body> <h2>JSP Expressions</h2> <ul> <li> Current time: <%= new java.util.Date() %> </li> <li> Your hostname:< %=request.getRemoteHost()%> </li> <li>Your session ID: <%= session.getId() %></li> <li>The <code>testParam</code> form parameter: <%= request.getParameter("testParam") %> </li> </ul> </body> </html>

3. JSP scriptlets Scriptlets la nhng oan ma co cha bat ky ma Java nao nam gia <% va %>. Neu chung ta muon thc hien th g o phc tap hn la ch chen vao mot bieu thc n gian th JSP scriptlets cho phep chung ta them bat ky oan ma Java nao vao trang JSP. Cac scriptlets nay c a vao phng thc _jspService (ma c goi bi service) cua servlet. Co cu phap: <% Java Code %>
Code 2: scriptlet.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <!-- Example of JSP Scriptlets. --> <html>
Code 2: scriptlet.jsp (tiep theo)

<head> <title>JSP Scriptlets</title> </head> <% String bgColor = request.getParameter("bgColor"); Svth: inh Le Giang Trang 10

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai boolean hasExplicitColor; if (bgColor != null) { hasExplicitColor = true; } else { hasExplicitColor = false; bgColor = "WHITE"; } %> <body bgcolor="<%= bgColor %>"> <h2 align="center">Color Testing</h2> <% if (hasExplicitColor) { out.println("You supplied an explicit background color of " + bgColor + "."); } else { out.println("Using default background color of WHITE. " + "Supply the bgColor request attribute to try " + "a standard color, an RRGGBB value, or to see " + "if your browser supports X11 color names."); } %> </body> </html>

4. Khai bao trong JSP Mot khai bao trong JSP cho phep chung ta nh ngha cac phng thc hoac cac trng (bien) co pham vi toan trang. Khai bao se c chen vao trong lp cua servlet (ben ngoai phng thc _jspService c goi bi service e x ly mot yeu cau). Mot khai bao co dang sau: <%! Java Code %> Cac khai bao trong JSP th khong phat sinh ra bat ky thong xuat nao, chung thng c dung e lien ket vi cac bieu thc hoac scriptlets. Chang han, ay la mot trang JSP ma in ra so lan truy cap vao trang.
Code 3: declaration.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <!-- Example of JSP Declarations. --> <html> Svth: inh Le Giang Trang 11

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai <head> <title>JSP Declarations </title> <link rel="styleshhet" href="JSP-Styles.css" type="text/css" /> </head> <body> <h1>JSP Declarations</h1> <%! private int accessCount = 0; %> <h2>Accesses to page since server reboot: <%= ++accessCount %></h2> </body> </html>

III.JSP directives.
Directives la cac lenh (ch th) en JSP container e x ly trang JSP, chung tac ong len toan bo cau truc cua lp servlet. Chung co dang sau: <%@ directive {attr=value} %> Cu phap nay th de e soan thao, ngan gon, suc tch nhng khong tng thch vi XML (Cu phap XML di). Directives th khong xuat bat ky th g ra luong xuat out. Ch co ba directives trong JSP: page, include va taglib. Cu phap XML Tng t nh scripting elements, cac directives cung co hai dang cu phap:
JSP Syntax
<%@ page attr_list %> : attr_list se c mo ta phan sau trong chng nay.

XML Syntax <jsp:directive.page attr_list />

<%@ include file=URL %> <jsp:directive.include file=URL /> <%@ taglib attr_list %> : directive nay c trnh bay trong chng III Tag Libraries. <jsp:root attr_list> <!-- other elements--> </jsp:root>

1. The page directive page directive cho phep chung ta ieu khien cau truc cua servlet bang cach a vao cac lp, at MIME
Svth: inh Le Giang Trang 12

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

type, . Mot page directive co the c at bat c ni nao trong trang JSP. <%@ page import=java.util.*, com.legiang.* %> page directive nh ngha mot so thuoc tnh phu thuoc vao trang va giao tiep gia cac thuoc tnh nay vi JSP container. language nh ngha ngon ng kch ban c s dung trong scriptlets, bieu thc va khai bao. Trong JSP 1.2 la phien ban mi nhat ch co ho tr java cho thuoc tnh nay do o java nay cung la gia tr mat nh. <%@ page language=java %> extends Thuoc tnh nay ch nh lp cha (superclass) cua servlet se c phat sinh cho trang JSP, co dang sau: <%@ page extends=package.class %> Chu y rang khi s dung thuoc tnh nay phai het sc can than v tai server co the a dung lp cha mat nh. import Thuoc tnh nay mo ta cac kieu san dung trong moi trng kch ban (theo java). Chung ta co the import vao tng lp hoac ca mot goi (package). <%@ page package.* %> import=package.class |

Neu import vao nhieu lp hay packages th cach nhau dau ,. session Thuoc tnh session kiem soat lieu co hay khong mot trang tham gia vao HTTP session. Co hai gia tr true va false. Mat nh la true, ch ra rang bien ngam nh session nen c noi ket vi session hien hanh. Gia tr false co ngha rang chang co session nao c s
Svth: inh Le Giang Trang 13

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

dung t ong va neu co truy cap vao bien session se tra loi vao luc trang c dch thanh servlet. buffer Thoc tnh nay xac nh kch thc cua vung em c s dung bi bien out. Co dang sau: <%@ page buffer=none | sizekb %> Thuoc tnh nay co gia tr mat nh la 8kb, gia tr none chang co vung em nao c cap va tat ca d lieu xuat se c ghi trc tiep qua ServletResponse, PrintWriter. autoFlush Xac nh lieu luong xuat co vung em c t ong flush (gia tr true mat nh) khi vung em ay hay se tung ra mot ngoai le khi vung em tran (false). Chu y rang se khong hp ly neu at autoFlush=false khi buffer=none. isThreadSafe Xac nh mc o an toan cua tieu trnh (thread) c cai at trong trang (vi giao tiep SingleThreadModel), true la gia tr mat nh. info nh ngha mot chuoi co the truy xuat t servlet bang phng thc getServletInfo(). <%@ page info=Some Message %> errorPage Thuoc tnh nay xac nh mot trang JSP nen bat ky cac ngoai le nao c tung ra nhng khong bat ngoai le nay trong trang hien hanh. <%@ page errorPage=Ralative URL %> Ngoai le tung ra se co san trong bien ngam nh exception. isErrorPage
Svth: inh Le Giang Trang 14

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

Xac nh trang JSP hien thi co the hoat ong nh mot trang loi cho mot trang JSP khac. Gia tr false la mat nh cho thuoc tnh nay. contentType Thuoc tnh nay gan Content-Type cho response header, mo ta kieu MIME cua tai lieu c gi en client. Thuoc tnh contentType co mot trong hai dang sau: <%@ page contentType=MIME-Type %> <%@ page contentType=MIME-Type; charset=Character-Set %> Mot so kieu MIME-Type thng dung: text/html, text/xml, text/plain, image/gif, image/jpeg. pageEncoding nh ngha bo ma hoa ky t cho trang JSP (character encoding), thuoc tnh nay ho tr cho bo ma Unicode va Latin-1, co gia tr mat nh la ISO-8859-1
Code 4: pagedirective.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html> <head> <title>The page Directive</titlle> <link rel="styleshhet" href="JSP-Styles.css" type="text/css" /> </head> <body> <h2>The page Directive</h2>
V du 4: pagedirective.jsp (tiep theo)

<%-- JSP page directive --%> <%@ page import="java.util.*, com.legiang.*" contenType="text/html" %> <%-- JSP Declaration (xem phan I.II.4) --%> <%! private String randomID() { int num = (int)(Math.random()*10000000.0); return("id" + num); Svth: inh Le Giang Trang 15

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai } private final String NO_VALUE = "<i>No Value</i>"; %> <%-- JSP Scriptlet (xem phan I.3) --%> <% Cookie[] cookies = request.getCookies(); String oldID = ServletUtilities.getCookieValue(cookies, "userID", NO_VALUE); String newID; if (oldID.equals(NO_VALUE)) { newID = randomID(); } else { newID = oldID; } LongLivedCookiecookie = new LongLivedCookie ("userID" , newID); response.addCookie(cookie); %> <%-- JSP Expressions (xem phan I.II.2) --%> This page was accessed at <%= new Date() %> with a userID cookie of <%= oldID %>. </body> </html>

2.

The include directive

Chung ta s dung include directive e a mot file vao tai lieu JSP chnh vao luc tai lieu dch thanh servlet (ma thng vao lan au tien truy cap vao trang nay). Co cu phap nh sau: <%@ include=Relative URL %> That s JSP chia lam hai nhanh e include mot file vao mot tai lieu chnh. File c include th c chen vao trang vao luc bien dch, Con nhanh con lai th c chen vao tai thi gian yeu cau (request) vi element la jsp:include se c trnh bay trong muc IV. Cac action chuan. File c include vao khong nhng cha HTML tnh ma con cha c ma JSP ong. ac tnh nay cho phep chung ta tao cac thanh nh hng, cac phan thong tin can tng tac, em so trang, ... rieng tren mot file khi chung ta co nhieu trang s dung lai chung. Neu file
Svth: inh Le Giang Trang 16

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

c include thay oi th tat ca JSP files s dung no can phai c cap nhat lai. V du ve include directive File au tien la mot oan cua trang cha cac thong tin tng tac can thiet cho file th hai dung lai bang include directive.
Code 5: contact.jsp

<%@ page import="java.util.Date" %> <%-- Cac bien sau se tr thanh cac fields trong servlet khi JSP file goi file nay. --%> <%! private int accessCount = 0; private Date accessDate = new Date(); private String accessHost = "<i>No previous access</i>"; %> <hr/> This page &copy; 2000 <a href="http//www.LG.com/">my-company.com</a>. This page has been accessed <%= ++accessCount %> times since server reboot. It was last accessed from <%= accessHost %> at <%= accessDate %>. <% accessHost = request.getRemoteHost(); %> <% accessDate = new Date(); %>
Code 6: includedirective.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd">
Code 6: includedirective.jsp

<!-- Example of including files at page translation time. --> <html> <head> <title>The include Directive</title> <link rel="styleshhet" href="JSP-Styles.css" type="text/css" /> </head> <body> <table boder=5 align="center"> <tr> <th class="title">Some Random Page</th> </tr> </ table > Svth: inh Le Giang Trang 17

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai <p>Information about our products services.</p> <p>Le, Le, Le.</p> <p>Giang, Giang, Giang.</p> <%@ include file="contact.jsp" %> </body> </html>

and

3. The taglib directive Directive nay nh ngha th vien tag va tiep au ng cho nhng custom tag c s dung trong trang JSP. Co cu phap sau: <%@ taglib uri=URIToTagLib prefix=tagPrefix%> taglib directive se c trnh bay chi tiet trong chng III.

IV.Cac action chuan.


Actions chang qua la elements c nh ngha trong ban ac ta JSP va luon luon co san trong cac JSP file ma khong can import vao bat ky th g va chung co cu phap XML. Co chn actions chuan: jsp:include, jsp:useBean, jsp:setProperty, jsp:getProperty, jsp:plugin, jsp:forward, jsp:fallback, jsp:params, jsp:param se c gii thieu trong muc nay. 1. Action chen vao files thi gian request. Vi include directive cho phep chung ta them vao cac tai lieu cha ma JSP vao nhieu trang khac nhau nhng lai co van e la oi hoi chung ta phai cap nhat lai ngay sa oi cua trang khi file c include thay oi. e loai bo van e nay at ta JSP cung cap cho chung ta mot action la <jsp:include> e include cac file vao thi gian yeu cau (request). Mac khac, do trang c bien dch thanh servlet vao thi gian yeu cau v the cac file c include khong the cha ma JSP. Cu phap cua action nay la: <jsp:include page=Relative URL flush=true> V du sau s dung jsp:include action e include bon file html, v du nay ch hien th cac thong tin cua cac web sites noi tieng.
Svth: inh Le Giang Trang 18

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai


Code 7: includerequest.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <!-- Example of including files at request time. --> <html> <head> <title>Whats New</title> <link rel="stylesheet" href="JSP-Styles.css" type="text/css" /> </head> <body> <center> <table boder="5"> <tr> <th class="title">What's New at JspNews.com </th> </tr> </table> </center> <p>Here is a summary of our four most recent news stories:</p> <ol> <li><jsp:include page="item1.html" flush="true" /></li> <li><jsp:include page="item2.html" flush="true" /></li> <li><jsp:include page="item3.html" flush="true" /></li> <li><jsp:include page="item4.html" flush="true" /></li> </ol> </body> </html>
Code 8: item1.html

<b>Bill Gates acts humble.</b> In a startling and unexpected development, Microsoft big wig Bill Gates put on an open act of humility yesterday. <a href="http://www.microsoft.com/Never.html">More details...</a>

Svth: inh Le Giang Trang 19

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai


Code 9: item2.html

<b>Scott McNealy acts serious.</b> In an unexpected twist, wisecracking Sun head Scott McNealy was sober and subdued at yesterday's meeting. <a href="http://www.sun.com/Imposter.html">More details...</a>
Code 10: item3.html

<b>Larry Ellison acts conciliatory.</b> Catching his competitors off guard yesterday, Oracle prez Larry Ellison referred to his rivals in friendly and respectful terms. <b href="http://www.oracle.com/Mistake.html">More details...</a>
Code 11: item4.html

<b>Sportscaster uses "literally" correctly.</b> In an apparent slip of the tongue, a popular television commentator was heard to use the word "literally" when he did <i>not</i> mean "figuratively." <a href="http://www.espn.com/Slip.html">More details...</a>

2. Action chen vao Applets cho Java Plug-In. Vi JSP, chung ta khong can co bat ky cu phap ac biet nao e include vao cac applet; ch dung APPLET tag cua HTML bnh thng. Tuy nhien, cac applet nay phai s dung t JDK 1.1 tr xuong v ca hai trnh duyet pho biet nhat la Netscape 4.x va Internet Explorer 5.x van cha ho tr JDK 1.2. Do o applet co mot so gii han sau: - e s dung Swing, chung ta phai gi cac Swing files qua mang. Tien trnh nay ton rat nhieu thi gian va cung that bai trong Internet Explorer 4 va Netscape 3.x (ch ho tr JDK 1.02) ma Swing lai phu thuoc vao JDK 1.1. - Chung ta khong the dung Java 2D.

Svth: inh Le Giang Trang 20

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

- Chung ta khong the dung goi collection (tap hp) cua Java 2. - Ma cua chung ta chay cham hn v hau het cac trnh bien dch cho nen Java 2 c cai tien rat ang ke so vi cac trnh bien dch t JDK 1.1 tr xuong. Hn the na, cac phien ban cua cac trnh duyet co mot so mau thuan trong cach thc ma chung ho tr AWT component khac nhau, lam cho viec th nghiem va ieu phoi cac giao tiep ngi dung tr nen phc tap va nang ne hn. e giai quyet cac van e nay, Sun a phat trien Java Plug-In cho Netscape va Internet Explorer ma cho phep chung ta s dung Java 2 platform cho cac applet trong nhieu trnh duyet khac nhau. Tuy nhien, lai that khong may man la APPLET tag bnh thng se khong lam viec vi Plug-In v cac trnh duyet c thiet ke rieng ch s dung may ao (Virtual Machine) co san cua chung khi chung gap APPLET. Thay v the, chung ta phai s dung OBJECT tag oi vi IE va EMBED tag oi vi Netscape. Hn na v chung ta khong biet loai trnh duyet nao se truy cap vao trang chung ta nen chung ta phai hoac la include ca OBJECT lan EMBED (at EMBED trong phan COMMENT cua OBJECT) hoac la xac nh loai trnh duyet vao thi gian yeu cau e dung tag ung cho loai trnh duyet o. Qua trnh nay th hien nhien nhng nham chan va mat thi gian. Action jsp:plugin ch dan server xay dng mot tag thch hp cho cac applet ma s dung Plug-In. a) jsp:plugin action. Phng cach n gian nhat e s dung jsp:plugin la cung cap cho no bon thuoc tnh: type, code, width, height. Chung ta nh gia tr applet cho thuoc tnh type va ba thuoc tnh con lai s dung giong nh APPLET tag bnh thng. Vi ngoai le la cac action th co cu phap XML nen cac thuoc tnh trong no cung phai theo qui c XML. V du vi APPLET tag trong HTML <APPLET CODE=MyApplet.class WIDTH=457 HEIGHT=350> </APPLET>
Svth: inh Le Giang Trang 21

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

s dung jsp:plugin action nh sau: <jsp:plugin type=applet code=MyApplet.class width=457 height=350 /> Ngoai bon thuoc tnh nay jsp:plugin con co cac thuoc tnh sau va hau het giong (nhng khong phai la tat ca) nh cac thuoc tnh cua APPLET tag. codebase, align, archive, hspace, name, vspace, title Giong nh APPLET tag. jreversion Xac nh so phien ban cua Java Runtime Environment, cac gia tr co the nhan la 1.1, 1.2. nspluginurl URL cho Netscape ma co the download Plug-In. Gia tr mat nh se hng ngi dung en web site cua Sun, nhng vi intranet chung ta co the muon ch dan ngi dung en mot ban sao cuc bo. iepluginurl URL cho Internet Explorer ma co the download Plug-In. b) jsp:param va jsp:params action. jsp:param c dung trong cac actions khac nh: jsp:include, jsp:forward, jsp:plugin. Action nay dung e cung cap cap ten va gia tr (name/value) cho cac actions tren. Cu the vi jsp:plugin, th action nay se nh ro ten va gia tr ma c truy cap t trong applet bi getParameter. Tat ca jsp:param actions eu phai nam trong jsp:params action. V du:
Code 12: S dung Applet

<APPLET CODE=MyApplet.class WIDTH=457 HEIGHT=350> Svth: inh Le Giang Trang 22

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai <PARAM NAME=PARAM1 VALUE=VALUE1> <PARAM NAME=PARAM2 VALUE=VALUE2> </APPLET>

trong JSP s dung nh sau:


<jsp:plugin type=applet code=MyApplet.class width=457 height=350 > <jsp:params> <jsp:param name=PARAM1 value=VALUE1 /> <jsp:param name=PARAM2 value=VALUE2 /> </jsp:params> </jsp:plugin>

c) jsp:fallback action. jsp:fallback cung cap van ban thay the oi vi cac trnh duyet khong ho tr OBJECT hay EMDEB. Chung ta s dung action nay giong nh la dung van ban thay the c at trong APPLET tag. V du chung ta co the thay the
Code 13: S dung jspfallback

<APPLET CODE=MyApplet.class WIDTH=457 HEIGHT=350> <B> Error: this example requires Java. </B> </APPLET> vi <jsp:plugin type=applet code=MyApplet.class width=457 height=350 > <jsp:fallback> <b> Error: this example requires Java. </b> </jsp:fallback> </jsp:plugin> 3. Actions dung e Components. forward va s dung

a) Chuyen cac request t cac trang JSP. Tnh huong chuyen request pho bien nhat la request o, au tien, bat nguon t servlet va servlet o chuyen request nay en trang JSP. Ly do e servlet
Svth: inh Le Giang Trang 23

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

thng x ly request goc la e kiem tra cac tham so trong request va thiet lap Beans nen oi hoi nhieu cong viec lap trnh va no thuan tien e lap trnh trong servlet hn la trong tai lieu JSP. Nguyen nhan ma trang ch thng lai la tai lieu JSP la JSP rat n gian trong tien trnh tao ra tai lieu HTML. Tuy nhien ieu nay ch la nh hng thng dung ch khong co ngha la ch co mot cach e thc hien. Do o e n gian va de s dung hn trong viec nhung ma RequestDispatcher trong mot scriptlet chung ta co the s dung jsp:forward action. V du:
Code 14: S dung jspforward

<% String dest; if (Math.random() > 0.5) { dest = page1.jsp; } else { dest = page2.jsp; } %> <jsp:forward page=<%= dest %> />

Action nay cho phep ieu phoi mot request hien hanh vao luc runtime en mot tai nguyen tnh, trang JSP hay lp servlet trong cung mot ng dung. b) S dung component trong JSP. Chung ta co the s dung cac actions sau e s dung lai cac component (Beans) trong JSP: jsp:useBean, jsp:setProperty, jsp:getProperty. Cac action nay c trnh bay trong chng II.

Svth: inh Le Giang Trang 24

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

Chng II :
1.

JSP JAVABEANS

I.Khai niem va cac quy c cua Bean.


Khai niem. Mo hnh thanh phan (component) trong JSP c tap trung vao cac component phan mem cua Java c goi la Bean. nh ngha chnh thc cua JavaSoft ve Bean: JavaBean la mot component phan mem co the dung lai c, co the c thc hien trc quan bang moi trng phat trien tch hp IDE (Integrated Development Environment). JavaBean API: tuan theo cac quy c c xac nh bi JavaBean API, cho phep JSP container tng tac vi Beans mc lap trnh mac du JSP container thc s chang hieu Bean thc hien nhng g va hoat ong ra sao. oi vi JSP, chung ta ch quan tan en cac kha canh API roi ra cac dau hieu cho nhng ham tao cua Bean va cac phng thc truy cap thuoc tnh x ly. Giong nh bat ky lp Java nao, cac the hien cua Bean n thuan ch la cac oi tng Java. Chung ta thng co s la chon hoac la tham chieu ti Beans va cac phng thc cua chung trc tiep qua ma Java trong cac lp khac hoac la thong qua cac scripting element trong trang JSP. V cac scripting element cung theo cac quy c cua Bean nen chung ta co the lam viec vi Beans ma khong can phai viet mot oan ma Java nao. Bean container nh JSP container, co the cung cap s truy cap de dang vao Beans va cac thuoc tnh cua chung. 2. Cac quy c cua Bean. Khi nh ngha Bean chung ta phai tuan theo cac quy tac sau: Ten lp cua Bean phai co tiep v ng la Bean chang han UserBean, DataAccessBean, ...That s th quy tac nay khong la yeu cau bat buoc nhng no la mot nh hng thong dung va cho phep nhng nha phat trien khac hieu ngay lap tc vai tro cua lp nay. Mot Bean phai co mot ham tao khong co tham so.

Svth: inh Le Giang Trang 25

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

Bean khong nen co bat ky bien the hien (field) nao la public. Cac gia tr ben vng nen c truy cap thong qua cac phng thc goi la getXxx va setXxx. oi vi cac server-side Bean khong nen dung th vien o hoa. Cac JSP element dung cho Bean.

II.
1.

JSP s dung Beans.


a)jsp:useBean element

Cho phep chung ta tai mot Bean vi ten va pham vi xac nh vao trang JSP. Cu phap: <jsp:useBean id = beanName scope = value typeSpec> <!-- cac element khac --> </jsp:useBean> Neu khong co body, tc la khong co tag nao trong than cua jsp:useBean th co cu phap la empty tag. TypeSpec co the hoac (|) trong cac thuoc tnh sau: TypeSpec ::= class = className | class = className type = typeName | type = typeName class = className | beanName = beanName type = typeName | type = typeName beanName = beanName | type = typeName Cac gia tr cua thuoc tnh scope: page ay la gia tr mac nh cua scope. Bean vi gia tr nay se co san t javax.servlet.jsp.PageContext trong trang hien hanh. Bean se c loai bo khi mot response gi tr ve client hoac request c chuyen ti mot trang mi. request
Svth: inh Le Giang Trang 26

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

Bean co gia tr nay th co san t oi tng ServletRequest cua trang hien hanh va bat ky trang c include hay forward nao bang cach s dung phng thc getAttribute(name). Bean se b huy bo khi mot response gi tr ve client. session Bean c s dung trong bat ky trang nao co tham gia vao mot session cua client. Co pham vi trong ca mot session cua client. Bean nay c lu tr trong oi tng HttpSession. application Bean c s dung trong bat ky trang nao trong ng dung hien hanh (ca request hien tai va tng lai). b)jsp:setProperty element Gan mot gia tr hoac nhieu gia tr (thuoc tnh mang) cua thuoc tnh vao Bean. Cu phap: <jsp:setProperty name = beanName options /> options co the hoac (|) trong cac thuoc tnh sau: options::=property =* | property =propertyName | property =propertyName param =paraName | property =propertyName value ={<%= expr%> | string} c) jsp:getProperty element Lay gia tr cua thuoc tnh Bean e hien th gia tr nay trong trang ket qua. Cu phap: <jsp:getProperty name = beanName property = propertyName /> 2. Phng phap chuyen kieu t ong JSP Bean Cac thuoc tnh cua mot thanh phan JSP th khong gii han gia tr chuoi (string) nhng rat quan trong e hieu rang tat ca gia tr cua thuoc tnh c truy cap
Svth: inh Le Giang Trang 27

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

qua <jsp:getProperty> tag se c chuyen thanh kieu chuoi. Tuy nhien, phng thc getter khong can tra ve kieu String mot cach tng minh v JSP container se t ong chuyen gia tr tra ve nay thanh kieu String neu can thiet. oi vi cac kieu d lieu c ban cua Java, s chuyen oi c the hien trong bang sau: Property Type boolean byte char double int float long Conversion to String Java.lang.Boolean.toString(boolean) Java.lang.Byte.toString(byte) Java.lang.Character.toString(char) Java.lang.Double.toString(double) Java.lang.Integer.toString(int) Java.lang.Float.toString(float) Java.lang.Long.toString(long)

Tng t, tat ca phng thc setter cua thuoc tnh c truy cap vi <jsp:setProperty> tag se c t ong chuyen t String sang mot kieu goc thch hp bi JSP container. ieu nay c thc hien qua cac phng thc cua cac lp vo boc Java. Property Type boolean or byte or char or int or
Boolean

Conversion from String java.lang.Boolean.valueOf(string) java.lang.Byte.valueOf(string) java.lang.Character.valueOf(string) java.lang.Double.valueOf(string) java.lang.Integer.valueOf(string) java.lang.Float.valueOf(string) java.lang.Long.valueOf(string)

Byte Character Double

double or float or long or 3. a)

Integer Float Long

Tm hieu cach INTROSPECTOR lam viec. Introspector la g?

Introspector la bo phan tch ma qua o Java xem xet cac oan ma cua chng trnh e tm ra nhng
Svth: inh Le Giang Trang 28

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

phng thc dung e xay dng va ap dung cho tng thuoc tnh cung nh tnh huong cu the. Chung ta khong can quan tam en cac khai bao phc tap khac. V du thiet lap hai phng thc sau: public void setAge(int age) public int getAge() Theo cach nay Bean container se hieu la chng trnh muon tao ra thuoc tnh la age co kieu int Bean container se chuyen cac thuoc tnh theo quy tac:
FoorBar foorBar Z z URL URL

b) Thiet ke cac phng thc mau dung cho thuoc tnh. oi vi thuoc tnh mang nhng tr n, hai phng thc getter va setter c viet theo mau sau (vi getter va setter xem 4.Thuoc tnh cua Bean): public void setAttributeName(AttributeType param) public AttributeType getAttributeName() oi vi thuoc tnh mang nhng tr kieu boolean, phng thc getter c dung vi tiep au ng la is thay cho get: public void setAttributeName(AttributeType param) public boolean isAttributeName() oi vi thuoc tnh gom nhieu phan t (index attribute) cac phng thc getter va setter cung c thiet lap tng t. Chung ta co the s dung mot cap phng thc hay ca hai cap phng thc cung c. public void setAttributeName(int index, AttributeType value) public AttributeType getAttributeName() va/hoac public void setAttributeName(AttributeType[] value) public AttributeType[] getAttributeName() c) Khai bao cac phng thc mot cac tng minh.
Svth: inh Le Giang Trang 29

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

Neu khong muon bo phan tch Introspector ngam nh, chung ta co the khai bao va x ly cac phng thc mot cach tng minh nh vao mot lp phu dung e mo ta cac phng thc c goi la BeanInfo. Khi Beans co ten la xxx c bien dch, trc tien Bean container se i tm lp co ten la xxxBeanInfo. Neu lp nay ton tai, cac thong tin trong lp xxxBeanInfo se c x ly trc va ap dung cho cac phng thc c tm thay. Cac phng thc khong c mo ta trong xxxBeanInfo se c ap dung cach phan tch kieu Introspector mac nh. Xem them III. Cac giao tiep bo tr cho Bean. 4. Thuoc tnh cua Bean. a) Cac phng thc truy xuat. Thuoc tnh that ra ch la nhng bien nam trong mot lp, tuy nhien nhng bien nay khong nen s dung trc tiep ma nen thong qua cac phng thc e truy xuat chung. Thong thng bien c dung vao hai muc ch o la gan d lieu va lay d lieu ra e tnh toan. Java a ra hai loai phng thc e ap ng cho nhu cau truy xuat bien o la cac phng thc getter mang y ngha oc d lieu t bien va cac phng thc s etter mang y ngha gan d lieu cho bien. b) Cac loai thuoc tnh. Chung ta hay xem cac v du sau: Thuoc tnh mang nhng gia tr n. Bean sau ay lay thi gian cua he thong.
Code 15: Java file

package com.legiang.bean; import java .util.*; public class CurrentTimeBean { /*Data members*/ private int hours; private int minutes; /* Methods */
Code 15: Java file (tiep theo)

public CurrentTimeBean() { Date now = new Date(); Svth: inh Le Giang Trang 30

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai this.hours = now.getHours(); this.minutes = now.getMinutes(); } public int getHours() { return hours; } public int getMinutes() { return minutes(); } }
Code 16: JSP file

<jsp:useBean id="time" class="com.legiang.bean.CurrentTimeBean "/> <html> <body> It is now <jsp:getProperty name="time" property="minutes"/> minutes past the hour. </body> </html>

Thuoc tnh mang gia tr boolean.


Code 17: Java file

package com.legiang.bean; public class LogicBean { /*Data members*/ private boolean authorized; /*Methods*/ public LogicBean() { this.authorized = false; } public void setAuthorized(boolean authorized) { this.authorized = authorized; } public boolean isAuthorized() {
Code 17: Java file (tiep theo)

return authorized; } }
Code 18: JSP file

<jsp:useBean id="logic" Svth: inh Le Giang Trang 31

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai class="com.legiang.bean.LogicBean"/> <html> <body> Do you authorize?<br/> Answer:<jsp:getProperty name="logic" property="authorized"/> </body> </html>

Thuoc tnh mang gia tr mang. V du nay se xay dng mot component ma co the thc hien cac tnh toan tnh tren mot day so.
Code 19: Java file

package com.legiang.bean; import java.util.*; public class StatBean { /*Data members*/ private double[] numbers; /*Methods*/ public StatBean() { numbers = new double[0]; } public double getAverage() { double sum = this.getSum(); if (sum == 0) return 0; else return sum/numbers.length; } public double getSum() {
Code 19: Java file (tiep theo)

double sum = 0; for (int i=0; i < numbers.length; i++) sum += numbers[i]; return sum; } public double[] getNumbers() { return numbers; } public double getNumbers(int index) { return numbers[index]; } Svth: inh Le Giang Trang 32

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai public void setNumbers(double[] numbers) { this.numbers = numbers; } public void setNumbers(int index, double value) { numbers[index] = value; } public int getNumbersSize() { return numbers.length; } }
Code 20: JSP file

<jsp:useBean id="stat" class="com.lg.bean.StatBean"> <% double[] mynums = {100, 250, 150, 50, 450}; stat.setNumbers(mynums); %> </jsp:useBean> <html> <body> The average of <% double[] numbers = stat.getNumbers(); for (int i=0; i < numbers.length; i++) { if (i != numbers.length) out.print(numbers[i] + ","); else
Code 20: Java file (tiep theo)

out.println("" + numbers[i]); } %> is equal to <jsp:getProperty name="stat" property="average" /> </body> </html>

Ngoai cac loai thuoc tnh tren, Java con cung cap mot so thuoc tnh nang cao khac dung cho cac Bean o hoa.

Svth: inh Le Giang Trang 33

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

III.Cac giao tiep bo tr cho Bean.


Trong khi ieu nay khong phai la yeu cau ac biet th co mot so giao tiep cho phep chung ta cai at vao Beans. Cac giao tiep nay co the c dung e m rong tnh nang cac Bean cua chung ta oi vi cac tnh huong khac nhau. 1. Giao tiep BeanInfo Chung ta a biet ve c che tng phan (reflection) trong muc trc, nhng co phng phap khac ma mot lp Bean co the bao cho Bean container (nh JSP container) biet ve cac thuoc tnh cua no bang cach cai at giao tiep BeanInfo. Giao tiep BeanInfo cho phep chung ta tao ra mot lp ong hanh vi Bean cua chung ta ma nh ngha cac thuoc tnh va cac cap o truy cap cua no. Giao tiep co the c dung e thch ng vi cac lp Java a co oi vi viec s dung cua Bean ma khong can thay oi giao tiep a tao. No cung c s dung e che dau cac thuoc tnh nao c truy cap thng xuyen t client, v thnh thoang c che reflection chuan cua Java co the phi bay nhieu thong tin ma chung ta lai khong muon nh the. e tao lp BeanInfo chung ta ch can at ten lp cung vi tiep v ng BeanInfo (nh SimpleBeanBeanInfo) va cai at giao tiep java.beans.BeanInfo. Quy tac at ten nay la cach bao cho Bean container (nh JSP container) biet e xac nh lp BeanInfo thch hp cho Bean chung ta. 2. Giao tiep Serializable JavaBeans sau khi c thiet ke va em vao s dung ngi dung se tuy y thay oi cac thuoc tnh ma Bean cung cap. Tuy nhien khi cham dt chng trnh th cac thuoc tnh nay cung mat i. Lan sau khi dung cac thuoc tnh nay phai c khi tao va at lai gia tr mi. Nh vay chu trnh song cua Bean ch han hep trong qua trnh hoat ong cua chng trnh, khong s dung lai sau nay c. Java cung cap mot kha nang lu lai cac oi tng JavaBeans len a bang c che Serializable. Khi mot oi tng hay Bean c lu xuong tap tin nh phan th tat ca bien hay cac oi tng con nam ben trong lp cha eu c lu theo.
Svth: inh Le Giang Trang 34

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

Mot so server ho tr c che persistent session bang cach ghi bat ky d lieu nao cua session vao a gia cac lan server ngng hoat ong. Khi server hoat ong tr lai th d lieu c serialization c phuc hoi tr lai. Mot nguyen nhan tng t a vao cac server e ho tr cho viec nhom cac session lai trong cac moi trng truyen thong cham chap (do hoat ong nhieu, ...). Nhieu server s dung c che serialization e sao lap d lieu cua session gia cac web server trong nhom. Neu Beans cua chung ta khong cai at giao tiep Serializable th server khong the lu tr hoac chuyen Beans cua chung ta trong cac tnh huong nay. 3. Giao tiep HttpSessionBindingListener Viec cai at giao tiep HttpSessionBindingListener cua Servlet API trong Beans chung ta se cho phep cac the hien nhan c thong bao cua cac s kien session. Giao tiep nay kha n gian, ch can nh ngha hai phng thc: public void valueBound(HttpSessionBindingListener event) public void valueUnBound(HttpSessionBindingListener event) Phng thc valueBound() c goi khi Bean c lu tr trong session cua ngi dung. Trong trng hp cua JSP, ieu nay se thng xay ra ngay khi Bean c khi tao bi <jsp:useBean> tag ma co xac nh pham vi session trong thuoc tnh scope. Phng thc valueUnBound() c goi khi oi tng b xoa khoi session. Co vai tnh huong lam cho Bean cua chung ta se b xoa khoi session. Khi JSP container d nh cham dt mot session cua ngi dung v hoat ong kem, trc tien JSP container xoa tng muc d lieu trong session, roi kch hoat valueUnBound(). Th hai, S kien nay se c kch hoat neu servlet, scriptlet, hay oan ma Java nao o xoa bo Bean khoi session v mot so ly nguyen nhan. Moi s kien ket hp vi giao tiep HttpSessionBindingListener th co the s dung e at c s truy cap vao oi tng session. Viec cai at giao tiep nay se cho phep chung ta tac ong tr lai cac s
Svth: inh Le Giang Trang 35

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai

kien cua session. V du ong cac noi ket khong can thiet, tao cac giao tac hoac thc hien cac hoat ong bao tr khac.

Svth: inh Le Giang Trang 36

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Chng III :
I. Tag library la g ?

TAG LIBRARIES

Trong ky thuat JavaServer Pages, cac action la cac element co the tao ra va truy cap vao cac oi tng cua ngon ng lap trnh va xuat vao nhng luong xuat chuan nh man hnh, may in, JSP nh ngha ra chn action chuan ma phai c cung cap bi bat ky engine nao. Ngoai cac action chuan nay , t JSP v1.1 tr len cho phep xay dng va phat trien cac module co the dung lai c goi la custom action. Trong trang JSP, mot custom action c trieu goi bang cach dung custom tag . Mot tag library la mot tap cac custom tag. Cac ng dung co the c thc hien bang custom action bao gom x ly form, truy cap c s d lieu va cac dch vu mc x nghiep khac nh email, quan ly th muc va ieu khien luong. Trc khi custom action xuat hien th JSP co mot c che dung lai ma la cac thanh phan JavaBean ket hp vi scriplet. Tuy nhien vi JavaBean co mot bat li la xay dng phc tap va kho quan ly. Cac custom action lam du c van e nay bang cach mang lai cac li ch di goc o khac cua viec module hoa tnh s dung lai cho cac trang JSP. Cac custom action goi gon lai cac tac vu e chung co the c dung lai trong hn mot ng dung va tang hieu xuat bang cach khuyen khch phan chia cong viec gia cac nha phat trien th vien va ngi s dung th vien. Cac tag library cua JSP c tao bi cac nha phat trien, la ngi thanh thao ngon ng lap trnh Java va la chuyen gia trong giao tiep vi c s d lieu va cac dch vu khac. Cac tag library c dung bi cac nha thiet ke ng dung web, la ngi co the ch tap trung vao cach thc hien th hn la quan tam en cach truy cap c s d lieu va cac dch vu khac. Mot so at tnh cua cac custom tag la: Chung co the c tuy bien thong qua cac thuoc tnh t mot trang ang goi.

Svth: inh Le Giang Trang 33

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Chung c phep truy cap vao tat ca oi tng co san trong trang JSP. Chung co the thay oi li ap ma c phat sinh bi mot trang ang goi. Chung co the c long vi nhau va cho phep cac tng tac phc tap trong mot trang. Chung co the truyen thong vi nhau. Chung ta co the tao va khi tao mot thanh phan JavaBean, tao mot bien tham chieu en Bean o trong mot tag va sau o s dung Bean nay trong tag khac.

II. Cach dung cac tags trong JSP.


Phan nay trnh bay cach thc ma tac gia cua trang web s dung tag library trong JSP va gii thieu cac loai tag khac nhau. Cac v du trong phan nay se c nh ngha trong phan III. 1. Khai bao cac tag library. Trang JSP se s dung cac tag c nh ngha trong mot tag library bang cach a vao taglib directive trong trang trc bat ky custom tag c s dung: <%@ taglib uri = /tlt prefix = tlt %> Thuoc tnh uri tham chieu ti URI o la nh danh duy nhat cho tag library. URI co the la URI tng oi hay tuyet oi. Neu URI tng oi th no phai c anh xa en mot URI tuyet oi trong taglib element cua bo mo ta trien khai ng dung Web, tap tin cau hnh nay cung vi ng dung Web c phat trien theo ban at ta Java Servlet va JavaServer Pages. Thuoc tnh prefix nh ngha mot tiep au ng e phan biet vi cac tag library khac, do o thuoc tnh nay giai quyet c van e xung ot ve cac ten tag. 2. Cac loai tag. Nhng custom action c dien at bang cu phap XML. Chung co start tag, noi dung va end tag: <tlt:tag> body </tlt:tag>
Svth: inh Le Giang Trang 34

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

sau:

Mot tag ma khong co noi dung c bieu dien nh <tlt:tag /> a)Cac tag n gian.

Mot tag n gian sau trieu goi mot action va xuat ra li chao Hello World.. <tlt:helloWorld /> b)Cac tag co thuoc tnh. Mot start tag cua custom action co the cha cac thuoc tnh di dang attr = value. Cac thuoc tnh nay tuy bien cac tham so dung trong cac phng thc cua mot lp java. Cac thuoc tnh co the c gan mot hay nhieu tham so trong mot oi tng request hoac t mot hang chuoi. Ch co cac kieu thuoc tnh trong bang sau mi co the c gan t cac gia tr tham so cua request va cua cac hang chuoi Kieu thuoc tnh Phng thc valueOf chuyen chuoi

boolean or Boolean java.lang.Boolean.valueOf(String) byte or Byte char or Character double or Double int or Integer float or Float long or Long java.lang.Byte.valueOf(String) java.lang.Character.valueOf(String) java.lang.Double.valueOf(String) java.lang.Integer.valueOf(String) java.lang.Float.valueOf(String) java.lang.Long.valueOf(String)

Gia tr thuoc tnh co dang <%= expression %> c tnh vao thi gian yeu cau. Gia tr cua bieu thc tuy thuoc vao kieu thuoc tnh c xac nh trong oi tng cai at tag con goi la tag handler (tag handler trong phan nh ngha cac tag).
Svth: inh Le Giang Trang 35

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

<tlt:helloWorld name=DHBK Ha Noi/> c) Cac tag co noi dung. Tag co the bao ham cac custom tag, scripting element, HTML, va noi dung nam gia start tag va end tag. Trong v du sau se a ra thong tin li chao c cung cap trong than tag thay v trong thuoc tnh cua no. <tlt:helloWorld> DHBK Ha Noi </tlt:helloWorld> d) Cac tag nh ngha cac bien kch ban. Mot tag co the nh ngha ra mot bien ma bien nay c dung trong cung mot trang. V du sau ay minh hoa cach thc nh ngha va s dung mot bien cha mot oi tng c tra ve. e) Ket hp cac tag. Cac tag ket hp vi nhau bang cac phng phap cua cac oi tng dung chung. Trong v du sau, tag1 tao mot oi tng co ten la obj1, sau o c s dung lai bi tag2. Qui tac nay c khuyen khch bi bang at ta JSP, mot tag tao ra mot ten vi thuoc tnh id va tag th hai co thuoc tnh name e dung lai ten nay. <tlt:tag1 id=obj1 attr2=value/> <tlt:tag2 name=obj1/> Trong v du ket tiep, dung cac tag long nhau tng t nh trong XML va HTML. <tlt:outerTag> <tlt:innerTag /> <tlt:outerTag>

III.nh ngha cac tag.


e s dung cac custom tag trong trang JSP, chung ta can phai nh ngha ba thanh phan rieng biet: Phat trien tag handler va cac lp tr giup cho tag. Khai bao tag trong bo mo ta th vien tag (tag library descriptor) di dang tap tin XML. Tao tap tin JSP co s dung custom tag.

Svth: inh Le Giang Trang 36

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Phan nay mo ta cac thuoc tnh cua tag handler va cac bo mo ta th vien tag, cung trnh bay cach phat trien tag handler va cac element cua bo mo ta th vien oi vi moi loai tag c gii thieu trong phan trc. 1. Tag handler. Tag handler la mot oi tng c goi len bi JSP container e nh tr cho custom tag trong xuot qua trnh thc thi cua trang JSP co tham chieu ti mot tag nao o. Cac phng thc cua tag handler c goi bi lp cai at trang JSP vao cac thi iem khac nhau trong xuot qua trnh nh tr cho tag. Khi gap start tag cua custom tag th lp cai at trang JSP goi cac phng thc e khi tao cac gia tr thch hp va sau o trieu goi phng thc doStartTag. Khi gap end tag cua custom tag th phng thc doEndtag c goi. Tuy theo muc ch cua tng custom tag ma chung ta x ly cac phng thc thch hp trong tng giao tiep cai at. Bang sau mo ta cac loai tag co cac phng thc tng ng e x ly.

Loai tag handler Simple Attributes Body, No Interaction Body, Interaction

Cac phng thc can x ly doStartTag, doEndTag, release doStartTag, doEndTag, set/getAttribute1...N doStartTag, doEndTag, release doStartTag, doEndTag, release, doInitBody, doAfterBody

Svth: inh Le Giang Trang 37

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Tag handler co s truy cap vao API ma cho phep no truyen thong vi trang JSP. Con ng vao API nay la oi tng pageContext, qua o tag handler co the truy cap vao moi oi tng ngam nh khac nh request, session, application t trang JSP. Cac oi tng ngam nh co the co cac thuoc tnh ket hp vi chung. Cac thuoc tnh nh the c truy cap qua cac phng thc [get/set]Attribute thch hp. Neu tag c long nhau th tag handler cung co the truy cap handler nay (c goi la parent) cung vi toan bo tag. Cac tag handler phai c cai at hai giao tiep hoac Tag hoac BodyTag. Cac giao tiep co the c dung e lay oi tng Java a ton tai va tao ra mot tag handler. e nh ngha cac tag handler mi chung ta co the dung cac lp TagSupport va BodyTagSupport nh la cac lp c s (xem III. 4. Tag handler c trieu goi nh the nao?). 2. Tag library descriptor. Tag library descriptor (TLD) la mot tai lieu XML dung e mo ta th vien tag. Mot TLD cha toan bo thong tin ve th vien va moi tag cha trong th vien. JSP container va cac cong cu phat trien JSP s dung TLD e xac nh cac tag. Cac element cua TLD sau dung e nh ngha th vien tag:
Code 21: HelloWorldTag.java

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib

PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library1.1//EN"

"http://java.sun.com/j2ee/dtds/webjsptaglibrary_1_1.dtd"> <!-- a tag library descriptor --> <taglib> <tlibversion> <!-- Phien ban cua th vien tag --> <!-- Phien ban cua JSP ma tag handler phu thuoc --> <jspversion> <!--Ten mat nh n gian co the dung bi cong cu tao trang JSP e tao ten vi gia tr gi Svth: inh Le Giang Trang 38

o an tot nghiep Gvhd: Ts. Nguyen Thuc Hai nh; chang han shortname co the c dung nh gia tr tiep au ng thch hp trong taglib directive va/hoac tao ra cac tiep au ng cho cac id --> <shortname> <uri> <!-- URI duy nhat xac nh tag library --> <info> <!-- Thong tin mo ta ve tag library --> <tag> ... </tag> ... </taglib>

Tag element yeu cau oi vi moi tag e xac nh mot lp co trong th vien tag. <tag> <tagclass>classname</tagclass> ... </tag> 3. Cac v du. e tao cac custom tag chung ta can hai import goi c ban: import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*; // tag extension a)Cac tag n gian. Tag handler oi vi cac tag n gian ch can cai at hai phng thc cua giao tiep Tag la doStartTag va doEndTag. doStartTag c trieu goi khi JSP container gap start tag, no tra ve SKIP_BODY v than tag chang co noi dung nao ca. Ngc lai khi gap end tag th JSP container goi doEndTag, tra ve EVAL_PAGE neu phan con lai cua trang can phai nh tr, neu khong tra ve SKIP_PAGE. Lp tag handler se c cai at nh sau:
Code 22: HelloWorldTag.java

public HelloWorldTag extends TagSupport { public int doStartTag() throws JspException { try { pageContext.getOut().print("Hello World."); } catch (Exception ex) { throw new JspTagException("HelloWorldTag: " + e.getMessage()); Svth: inh Le Giang Trang 39

o an tot nghiep Gvhd: Ts. Nguyen Thuc Hai } return SKIP_BODY; } public int doEndTag() { return EVAL_PAGE; } }

TLD element Cac tag ma khong co than phai khai bao noi dung cua than la rong.
Code 23: helloworld.tld

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE taglib

PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN"

"http://java.sun.com/j2ee/dtds/webjsptaglibrary_1_1.dtd"> <!--Neu co JSP v1.2 th oi lai "http://java.sun.com/j2ee/dtds/jsptaglibrary_1_2.dtd" --> <!-- a tag library descriptor --> <taglib> <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>legiang</shortname> <uri></uri> <info> A tag library from authors at http://www.legiang.com/. </info> <tag> <name>helloWorld</name> <tagclass>com.legiang.HelloWorldTag</tagcl ass> <info>V du vi tag n gian nhat</info> <!-- EMPTY is insensitive case --> <bodycontent>EMPTY</bodycontent> </tag> <!-- Other tags defined later... --> </taglib>

JSP page
Code 24: hello.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html Svth: inh Le Giang Trang 40

o an tot nghiep Gvhd: Ts. Nguyen Thuc Hai PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html> <head>
Code 24: hello.jsp (tiep theo)

<%@ taglib uri = "helloworld.tld" prefix = "legiang" %> <title> <legiang:helloWorld /> </title> <!-- stylesheet e hien th cac tag nh h1, --> <link rel = "stylesheet" href ="JSP-Styles.css" type = "text/css"/> </head> <body> <h1><legiang:helloWorld /></h1> <legiang:helloWorld /> </body> </html>

b) Cac tag co thuoc tnh. nh ngha cac thuoc tnh trong tag handler.

oi vi moi thuoc tnch cua tag, chung ta phai nh ngha theo phong cach cua JavaBean, tc la phai co phng thc get/set[Attribute] trong tag handler. Hay nh rang, neu thuoc tnh c at ten la id va tag handler ket tha t lp TagSupport th chung ta khong can nh ngha cac phng thc get/set[Attribute] bi v nhng th nay a c TagSupport lam san. Lp tag handler se c cai at nh sau:
Code 25: HelloWorldTag.java (sa lai trong Code 22 )

public HelloWorldTag extends TagSupport { private String name = World.; public void setName(String name) { this.name = name; } public int doStartTag() throws JspException { try { pageContext.getOut().print("Hello " + name); } catch (Exception ex) { Svth: inh Le Giang Trang 41

o an tot nghiep Hai +

Gvhd: Ts. Nguyen Thuc throw new JspTagException("HelloWorldTag: " e.getMessage());

Code 25: HelloWorldTag.java (sa lai trong Code 22) (tiep theo)

} return SKIP_BODY; } public int doEndTag() { return EVAL_PAGE; } }

TLD element

oi vi moi thuoc tnh chung ta phai xac nh thuoc tnh nao c yeu cau (bat buoc) va gia tr nao co the c xac nh bi bieu thc. Neu thuoc tnh cua tag khong bat buoc th tag handler nen cung ap gia tr mat nh. TLD cho tag handler tren nh sau:
Code 26: TLD file (overwrite helloworld.tld)

... <!-- oan nay tng t nh muc 1) --> <tag> <name>helloWorld</name> <tagclass>com.legiang.HelloWorldTag</tagclass > <info>V du vi tag n gian nhat</info> <attribute> <name>name</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> <!-- EMPTY is insensitive case --> <bodycontent>EMPTY</bodycontent> </tag>

JSP page
Code 27: paramtag.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> Svth: inh Le Giang Trang 42

o an tot nghiep Hai <html>

Gvhd: Ts. Nguyen Thuc

Code 27: paramtag.jsp (tiep theo)

<head> <%@ taglib uri = "helloworld.tld" prefix = "legiang" %> <title> Tag With Attributes </title> <!-- stylesheet e hien th cac tag nh h1, --> <link rel = "stylesheet" href ="JSP-Styles.css" type = "text/css"/> </head> <body> <h1><legiang:helloWorld name = "BKHN University" /></h1> <legiang:helloWorld name = "DHBK Ha Noi" /> </body> </html>

c) Cac tag co noi dung. Tag handler Tag handler cho loai nay c cai at khac nhau phu thuoc vao tag handler co tng tac vi than hay khong. Tng tac co ngha la tag handler oc hoac sa oi noi dung cua than hoac tao ra gia tr lap lai cho than tag. Cac tag co tng tac vi phan than: neu tag handler khong can tng tac vi phan than th tag handler nay nen cai at giao tiep Tag hoac dan xuat t lp TagSupport. Neu than cua tag can nh tr th doStartTag can phai tra ve EVAL_BODY_INCLUDE, ngc lai tra ve SKIP_BODY. Cac tag co tng tac vi phan than : tag handler phai cai at giao tiep BodyTag hoac BodyTagSupport, thng cai at phng thc doInitBody va doAfterBody. Hai phng thc nay tng tac vi noi dung cua than c chuyen vao tag handler bi lp cai at trang JSP. oi tng BodyContent cung cap vai phng thc e oc va ghi noi dung cua no. Tag handler s dung cac phng thc getString, getReader cua BodyContent e trch thong tin t phan than va phng thc writeOut(out) e ghi cac noi dung cua than vao luong xuat. Phng thc getPreviousOut thng c s
Svth: inh Le Giang Trang 43

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

dung e bao am rang ket qua tra ve cua tag handler th co san oi vi toan bo tag handler. Cac phng thc chnh c dung trong loai tag nay: doInitBody: c goi sau khi noi dung cua than c gan nhng trc khi c nh gia tr, thng c dung e thc hien khi tao gia tr ma tuy thuoc vao noi dung cua than. doAfterBody: c goi sau khi noi dung cua than c nh tr. Neu than c tr nhieu lan lap i lap lai nen tra ve EVAL_BODY_TAG, ngc lai tra ve SKIP_BODY. release: tag handler nen xac lap lai trang thai cua no va giai phong cac tai nguyen danh rieng bang phng thc release. V du sau oc noi dung cua than, chuyen noi dung o, va sau o ghi lai nhng thay oi vao luong xuat.
Code 28: TransformTag.java

public class TransformTag extends BodyTagSupport { public int doAfterBody() throws JspTagException { BodyContent bc = getBodyContent(); String body = bc.getString(); bc.clearBody(); try { getPreviousOut().print(body.transform()); } catch (IOException e) { throw new JspTagException("TransformTag: " + e.getMessage()); } return SKIP_BODY; } }

TLD element

TLD ay ch sa oi lai cac v du tren trong bodycontent tag va at ten file la transform.tld <tag> ... <bodycontent>JSP<bodycontent>
Svth: inh Le Giang Trang 44

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

<tag> Vi cac tag co phan than th bodycontent tag co hai gia tr. Mot la JSP, phan than bao gom custom tag, scripting element va van ban HTML. Tat ca cac th con lai co gia tr la tagdependent. Chu y rang gia tr cua element nay khong anh hng ti tien trnh thong dch cua than. JSP page
Code 29: transformtag.jsp

<?xml version="1.0" encoding="ISO-8859-1" ?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "DTD/xhtml1-strict.dtd"> <html> <head> <%@ taglib uri = "transform.tld" prefix = "legiang" %> <title> Tag With Body </title> <!-- stylesheet e hien th cac tag nh h1, --> <link rel = "stylesheet" href ="JSP-Styles.css" type = "text/css"/> </head> <body> <h1> Tag With Body </h1> <legiang:transform> Hi! BKHN University </legiang:transform> </body> </html>

d) Cac tag nh ngha cac bien kch ban. Tag handler Tag handler am nhan viec tao va gan oi tng c tham chieu bi bien kch ban vao ng canh co the truy cap t mot trang. e Lam c ieu nay bang cach dung hoac phng thc pageContext.setAttribute(name, value, scope) hoac pageContext.setAttribute(name, value). Thong thng mot thuoc tnh chuyen vao custom tag phai xac nh ten cua bien oi tng; Ten nay co the truy xuat bang
Svth: inh Le Giang Trang 45

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

cach goi phng thc get[Attribute] cua thuoc tnh (xem 2) Cac tag co thuoc tnh). Neu gia tr cua bien kch ban phu thuoc vao oi tng hien tai trong tag handler th no co the truy xuat oi tng nay bang cach dung pageContext.setAttribute(name, scope). Cac gia tr cua tham so scope c tom tac trong bang sau: Pham vi Co the truy cap t Thi gian song Cho ti khi li ap gi tr lai user hoac yeu cau c chuyen ti trang mi

page

Trang hien hanh

request

Trang hien hanh Cho ti khi li va bat ky trang ap gi tr lai c include hay user forward nao Yeu cau hien hanh va bat ky i song cua yeu cau tiep mot phien lam theo trong cung viec (session) browser Bat ky yeu cau hien tai va tng i song cua lai nao t cung mot ng dung mot ng dung

session

application

Ngoai viec cai at gia tr cua bien trong tag handler, chung ta con phai nh ngha mot lp dan xuat t lp TagExtrInfo. Lp nay cung cap thong tin cho JSP container ve at tnh cua cua bien kch ban, phai cai at phng thc getVariableInfo e tra ve mot mang
cac oi tng VariableInfo cha cac thong tin sau: ten bien, lp cua bien, lieu bien nay co tham khao ti mot gia tr oi tng mi hay a co, tnh san dung cua bien nay.

Bang sau mo ta tnh san dung cua bien kch ban va cac phng thc e xac lap va xac lap lai gia tr cua bien.
Svth: inh Le Giang Trang 46

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Value

Availability Gia start tag va end tag.

Methods Trong doInitBody va doAfterBody oi vi tag handler cai at BodyTag; ngc lai trong doStartTag. Trong doInitBody, doAfterBody, va doEndTag oi vi tag handler cai at BodyTag; ngc lai trong doStartTag va doEndTag.

NESTED

AT_BEGIN

T start tag cho en cuoi trang.

Sau end tag AT_END cho ti cuoi Trong doEndTag. trang. Lp tag handler sau lay d lieu t JNDI lookup c lu tr nh la thuoc tnh cua oi tng pageContext cung vi ten cua bien kch ban.
Code 30: LookupTag.java

public LookupTag extends TagSupport { private String type; private String name; public void setName(String name) { this.name = name; } public void setType(String type) { this. type = type; } public int doStartTag() { return SKIP_BODY; } public int doEndTag() throws JspException { try { InitialContext context = new InitialContext(); Object obj = (Object)context.lookup(name); pageContext.setAttribute(getId(), obj);
Code 30: LookupTag.java (tiep theo)

} catch(javax.naming.NamingException e) { throw new JspException("Unable to look up " + name Svth: inh Le Giang Trang 47

o an tot nghiep Hai } return EVAL_PAGE; } }

Gvhd: Ts. Nguyen Thuc + " due to " + e.getMessage());

Bien kch ban se c nh ngha trong lp dan xuat t lp TagExtraInfo. Do bien c truyen vao nh cac thuoc tnh cua tag nen se c truy xuat bi phng thc getAttributeString cua lp TagData va dung e lap ay ham tao cua VariableInfo. e cho phep bien kch ban c dung trong phan con lai cua trang th pham vi cua bien nen gan bang AT_END.
Code 31: LookupTagTei.java

public class LookupTagTEI extends TagExtraInfo { public VariableInfo[] getVariableInfo(TagData data) { VariableInfo info1 = new VariableInfo( data.getAttributeString("id"), data.getAttributeString("type"), true, VariableInfo.AT_END); VariableInfo[] info = { info1 } ; return info; } }

TLD element

Chung ta can phai bao cho JSP container biet no nen dung lp TagExtraInfo e tao ra bien kch ban vi pham vi thch hp. oan TLD co ten lookup.tld nh sau:

Code 32: TLD file

... <tag> <tagclass>com.legiang.LookupTag </tagclass> <teiclass>com.legiang.LookupTagTEI</teiclass> <attribute> <name>name</name> Svth: inh Le Giang Trang 48

o an tot nghiep Gvhd: Ts. Nguyen Thuc Hai <required>true<required> <rtexprvalue>false</rtexprvalue> </attribute> <attribute> <name>type</name> <required>true<required> <rtexprvalue>false</rtexprvalue> </attribute> </tag> ...

JSP page
Code 33: JSP file

... <%@ taglib uri = "lookup.tld" prefix = "legiang" %> <!-- dung trong body cua JSP --> <legiang:lookup id = "sv" type = "UserTransaction" name = "java:comp/ UserTransaction "> <!-- Gia s trong lp UserTransaction co phng thc begin --> <% sv.begin() %>

4. Tag handler c trieu goi nh the nao? Giao tiep Tag nh ngha mot qui c truyen thong c ban gia tag handler va lp cai at trang JSP. Giao tiep nay nh ngha chu ky song va cac phng thc c trieu goi khi gap phai start tag va end tag cua mot action. Lp cai at trang JSP trieu goi cac phng thc setPageContext, setParent, va cac phng thc cai at thuoc tnh trc khi goi phng thc doStartTag. Lp nay cung bao am rang phng thc release se c goi trong tag handler trc khi ket thuc trang. V du sau thc hien tng bc cach goi tag handler.

Code 34: Cach thc hien tag handler khong co BodyContent

ATag t = new ATag(); t.setPageContext(...); t.setParent(...); t.setAttribute1(value1); Svth: inh Le Giang Trang 49

o an tot nghiep Hai t.setAttribute2(value2); t.doStartTag(); t.doEndTag(); t.release();

Gvhd: Ts. Nguyen Thuc

Giao tiep BodyTag m rong t giao tiep Tag bang cach nh ngha them cac phng thc cho phep tag handler truy cap vao than cua no. Giao tiep nay cung cap ba phng thc mi: setBodyContent: tao noi dung cua than tag va them vao tag handler. doInitBody: c goi trc khi nh tr than cua tag. DoAfterBody: c goi trc khi nh tr than cua tag. V du:
Code 35: Cach thc hien tag handler co BodyContent

t.doStartTag(); out = pageContext.pushBody(); t.setBodyContent(out); /* thc hien cac khi tao can thiet sau khi noi dung cua than c gan */ t.doInitBody(); t.doAfterBody(); /**/ /* Khi doAfterBody tra ve EVAL_BODY_TAG chung ta lap lai phan nh tr cho than tag */ ... t.doAfterBody(); /**/ t.doEndTag(); t.pageContext.popBody(); t.release();

Svth: inh Le Giang Trang 50

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Phan II: Phan Tch Thiet Ke Cai at


Chng IV:
I. Phan Tch.
1. at van e. Xuat phat t nhu cau thc te trong quan ly cong viec cac cong ty hien nay con nhieu nhc iem, viec ngi quan ly cap cang cao cang khong nam bat thong tin, khong trc tiep quan ly c toc o lam viec cua nhan vien mnh la kha pho bien. Hn na trong thi buoi kinh te th trng thi buoi cua cong nghe thong tin moi con ngi co qua nhieu cong viec phai lam, ho khong co thi gian ranh roi e ngoi lam bao cao roi cuoi tuan em nop cho quan ly, ho muon c nha nhng van biet cong viec cua mnh phai lam trong ngay la g?, ho muon co mot khoang thi gian du t oi e tranh thu ngh ngi. e mot phan nao thao g cac kho khan, nhc iem va xuat phat t cac yeu cau thc te cua mot so cong ty chng trnh QUAN LY CONG VIEC TREN WEB a c ra i. Trong khuon kho cua mot o tot nghiep ai hoc do thi gian, cac kien thc nhat la ve chuyen mon nghiep vu con han che chac chan khong tranh khoi nhng sai sot. Song vi s no lc het sc nham hoan thanh tot cong viec nhng do thi gian nghien cu thc hien khong nhieu, lng cong viec tng oi ln, trang thiet b, tai lieu cha ay u va trnh o han che cho nen chac chan e tai nay se co t nhieu khiem khuyet t thc te en y tng lap trnh, mong c s lng th va ch bao them cua cac Thay, Co. Rat mong nhan c s ong gop y kien chan thanh t thay co va cac ban e chng trnh hoan thien hn sm c a vao s dung thc te. 2. nh ngha cac yeu cau. Quan Ly Phan cong cong viec cho tng nhan vien.

PHAN TCH THIET KE

Svth: inh Le Giang Trang 51

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Kiem tra tien o thc hien. anh gia cong viec. Thong ke. Giai ap thac mac. Lap bang anh gia nhan vien bang bieu o. Tong hp bao cao. Thiet lap che o lam viec trc tiep gia hai nhan vien. Nhan Vien Xem cac cong viec c giao. Bao cao tien o thc hien theo ngay. e xuat cach giai quyet cong viec theo ngay. ong gop y kien xay dng cong ty. Bao cao cong viec thc hien xong. Xem cac anh gia cua ben quan ly. Quan Tr He Thong (Admin) Tao/Xoa/Sa user. Them/Xoa/Sa cac quyen. Gan quyen cho user. Refresh database. Restore database. Cap nhat database.

3. Pham vi e tai. Ve D Lieu Cho phep lu tr trong mot nam phat sinh d lieu D lieu cap nhat trc tuyen
Svth: inh Le Giang Trang 52

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

4.

Ve Giao Dien He thong hoat ong tren moi trng Web Ve Mang Hoat ong trong moi trng mang Intranet Ve x ly Thong ke Thiet lap che o lam viec gia hai nhan vien Lap bao cao Lam ti/ Phuc hoi CSDL Mo hnh x ly quan niem. a)S o ng canh.

Svth: inh Le Giang Trang 53

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

b)

S o phan ra chc nang.

Svth: inh Le Giang Trang 54

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

c) -

S o he thong. Mc 1:

Svth: inh Le Giang Trang 55

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Mc 2:

Svth: inh Le Giang Trang 56

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Phan ra t x ly 4_Phan Cong.

Phan ra t x ly 5_Cap Nhat CV.

Svth: inh Le Giang Trang 57

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Phan ra t x ly 6_Thong Ke.

Phan ra t x ly 9_Tong Hop Bao Cao.

Svth: inh Le Giang Trang 58

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Phan ra t x ly 13_Bao Cao Tien Do CV.

Phan ra t x ly 16_ Cap Nhat Permission.

Svth: inh Le Giang Trang 59

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Phan ra t x ly 19_ Cap Nhat User.

Phan ra t x ly 20_Gan Quyen.

Svth: inh Le Giang Trang 60

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

5. Mo hnh d lieu quan niem. S o thc the ket hp (ERD):

Svth: inh Le Giang Trang 61

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

II. Thiet Ke.


6. Thiet ke x ly. a) S o he thong mc vat ly.

b)

Cac n v thiet ke. Cap nhat cong viec. Them cong viec: Nhap: TenCV & NoiDung & MaDA. Xuat: Thong bao a them / thong bao loi. Sa cong viec: Nhap: TenCV | NoiDung | MaDA | DaXong. Xuat: Thong bao a sa / thong bao loi. Xoa cong viec: Nhap: MaCV. Xuat: Thong bao a xoa / thong bao loi. Phan cong cong viec. Nhap: MaNV & mot hoac nhieu MaCV & NgayBD, NgayKT. Xuat: Thong bao a phan cong / thong bao loi. anh gia cong viec. Nhap: MaDG vao bang Works.

Svth: inh Le Giang Trang 62

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Xuat: Thong bao a anh gia / thong bao loi. Kiem tra tien o cong viec. Ch hien th cac bao cao tien o cua nhan vien. Thiet lap che o lam viec. Nhap: MaNV & MaCV & ThietLap vao bang PhanCong. Xuat: Thong bao a chuyen giao cong viec / thong bao loi. Bao cao tien o cong viec. Nhap: MaCV & TienDo & Ngay | GhiChu | Kem tap tin. Xuat: Thong bao bao cao a chuyen i / thong bao loi. Thay oi mat khau. Nhap: Usename & Password cu | Password & Confirm Password mi. Xuat: Thong bao a oi mat khau / thong bao loi. Thong ke cong viec. Thong ke cong viec theo ba tieu chuan: + T ngay en ngay. + Theo anh gia (xep loai). + theo e an. Lam ti c s d lieu. Nhap: Ten CSDL mi can phuc hoi khi b hong & lam ti t ngay nao en ngay nao | t thang nao en thang nao | nam nao. Xuat: Thong bao a lam ti CSDL / thong bao loi. Phuc hoi c s d lieu. Nhap: Ten CSDL can phuc hoi (a co t lam ti CSDL). Xuat: Thong bao a phuc hoi hoan toan / thong bao loi.

Svth: inh Le Giang Trang 63

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

c)

Mot so giai thuat c the hien bang lu o. Them cong viec.

Svth: inh Le Giang Trang 64

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

The m Co ng Vie c
Kh i a u

Nha p ta t ca d lie u ve co ng vie c

M ba ng co ng vie c

Co ng vie c to n ta i? kho ng - Ta ng ma so co ng vie c - Ghi va o CSDL ca c d lie u a nha p Tho ng ba o co ng vie c a the m co

co

Tho ng ba o co ng vie c a to n ta i

Tie p tu c? kho ng Cha m d t

Svth: inh Le Giang Trang 65

Phan cong cong viec.

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Pha n Co ng Co ng Vie c
Kh i a u

- Cho n nha n vie n - Cho n mo t hoa c nhie u co ng vie c - Nga y ba t a u, ke t thu c

M ba ng pha n co ng

NV na y a d c pha n co ng ca c co ng vie c? kho ng Ghi ca c tho ng tin a nha p va o CSDL Tho ng ba o co ng vie c a pha n co ng co

co

Tho ng ba o lo i

Tie p tu c? kho ng Cha m d t

Svth: inh Le Giang Trang 66

Thong ke cong viec.

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Tho ng Ke Co ng Vie c
Kh i a u

Cho n mo t trong ba tie u chua n a cho

kho ng Ca c chua n a cho n? co Truy va n theo ca c chua n Tho ng ba o kho ng co

Hie n thba ng tho ng ke

Tie p tu c? co kho ng Cha m d t

Svth: inh Le Giang Trang 67

Lam ti c s d lieu.

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Lam Ti CSDL
Khi au

- Nhap ten csdl mi - Nhap ngay thang nam

CSDL mi ton tai ? khong

co

- Tao CSDL mi (Backup) Thong bao - Xoa cac mau tin trong cac bang CSDL a co phat sinh

Thong bao a lam ti CSDL

Tiep tuc ? co khong Cham dt

Svth: inh Le Giang Trang 68

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

7. Thiet ke c s d lieu d) Mo hnh d lieu vat ly. S o thc the ket hp (ERD):

Svth: inh Le Giang Trang 69

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

e) Table

Bang mo ta chi tiet. Vung Dien Giai Kie u Kch Th c


Gii han Gia Tr

GOPY

MAGY MANV NOI DUNG NGAY

Ma Gop Y Ma Nhan Vien Noi Dung Ngay

Text 10 Text 9 Text 50 Dat e time Int Text 10 Text 30 Lon g Text 30 >= ngay hien tai >=1

Gia Ran Tr g Nga buoc m n h Khoa Chnh (PK) Khoa Ngoai (FK) nga y hie n tai Khoa Chnh (PK) Khoa Ngoai (FK)

FILES

MAFILE MABC TENFILE FILESIZE CONTEN TTYPE

Ma File Ma Bao Cao Ten File Kch thc File Loai Noi Dung File c Tai Len Server Ma Bao Cao

BAO CAO TONG

MABCTH

Text 10

Khoa Chnh (PK)

Svth: inh Le Giang Trang 70

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

HOP MACV TON DONG

NGAY

Tong Hp Ma Cong Viec So Lng CV Con Ton ong Ngay

Int Int

>=1 >=1

Khoa Ngoai (FK)

Dat e time

>= ngay hien tai

nga y hie n tai

CV TUONG LAI

DEXUAT DANH GIA MADG PHAN HOI MAXL NHAN VIEN MANV MA GROUP TENNV NGAY

Cong Viec Se Lam Trong Tuan Ti e Xuat Ma anh Gia Phan Hoi Ma Xep Loai Ma Nhan Vien Ma Nho m Ten Nhan Vien Ngay

Text 80 Int Text 50 Text 80 Int Int Text 25 Dat < >=1 >=1 Khoa Ngoai (FK) Khoa Chnh (PK) Khoa Ngoai (FK) >=1 Khoa Chnh (PK)

Svth: inh Le Giang Trang 71

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

SINH DIACHI PHAI

Sinh a ch Phai

e time Text 50 Bit

ngay hien tai La so nguy en 0 hoac 1

PHONE USER NAME PASS WORD MABCN

BAO CAO NGAY

MACV TIENDO NGAY

ien Thoai Ten Ngi S Dung Mat Khau Ma Bao Cao Ngay Ma Cong Viec Tien o Ngay

Text 9 Text 16

Text 16 Text 10 Khoa Chnh (PK) >=1 Khoa Ngoai (FK) nga y hie n tai

int Floa 4 t Dat e time Text 80 Text 50

>= ngay hien tai

GHICHU DEXUAT

WORKS

MACV MADA

Ghi Chu e Xuat Y Kien Ma Cong Viec Ma

Int Int

>=1 >=1

Khoa Chnh (PK) Khoa

Svth: inh Le Giang Trang 72

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

e An MADG Ma anh Gia TENCV Ten Cong Viec NOI Noi DUNG Dung DAXONG a Xong DEAN MADA TENDA DIA DIEM MAXL XEPLOAI GHICHU PHAN CONG MANV MACV NGAYBD Ma e An Ten e An a iem Ma Xep Loai Xep Loai Ghi Chu Ma Nhan Vien Ma Cong Viec Ngay Bat au Thiet lap Ngay

Int Text 20 Text 100 Bit Int Text 30 Text 40 Int Text 30 Text 80 Text 9 Int Dat Nga e yBD time > Nga y KT Bit 1 Dat Nga

>=1

Ngoai (FK) Khoa Ngoai (FK)

0 hoac 1 >=1

Khoa Chnh (PK)

XEP LOAI

>=1

Khoa Chnh (PK)

>=1 Nga y hien tai 0 >

Khoa Ngoai (FK) Khoa Ngoai (FK)

THIET LAP NGAYKT

Svth: inh Le Giang Trang 73

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Ket Thuc GROUP S MA GROUP TEN GROUP TRG GROUP NGAYNC GROUP S_PER MAPER MA GROUP PERMI SIONS MAPER TENPER GHICHU f)

Ma Nho m Ten Nho m Tr ng Nho m Ngay Nhan Chc Ma Quye n Ma Nho m Ma Quye n Ten Quye n Ghi Chu

e yKT time > Nga y BD Int Text 30 Text 9

Nga y hien tai >=1 Khoa Chnh (PK)

Dat e time Int Int Int Text 20 Text 80

>=1 >=1 >=1

Khoa Chnh (PK) Khoa Chnh (PK) Khoa Chnh (PK)

Cac rang buoc toan ven .

(RB1) MAGY la khoa chnh cua GOPY. (RB2) MAFILE la khoa chnh cua FILES. (RB3) MABCTH la khoa chnh cua BAOCAOTONGHOP. (RB4) MADG la khoa chnh cua DANHGIA. (RB5) MANV la khoa chnh cua NHANVIEN. (RB6) MABCN la khoa chnh cua BAOCAONGAY. (RB7) MACV la khoa chnh cua WORKS. (RB8) MADA la khoa chnh cua DEAN. Svth: inh Le Giang Trang 74

o an tot nghiep Gvhd: Ts. Nguyen Thuc Hai (RB9) MAXL la khoa chnh cua XEPLOAI. (RB10) MANV&MACV la khoa chnh cua PHANCONG. (RB11) MAPER la khoa chnh cua PERMISSION. (RB12) MAGROUP la khoa chnh cua GROUPS. (RB13) MAPER & MAGROUP la khoa chnh cua GROUP_PER. (RB14) MANV la khoa ngoai cua GOPY tham chieu t NHANVIEN.

(RB15) MABC la khoa ngoai cua FILES tham chieu t BAOCAONGAY. (RB16) MACV la khoa ngoai cua BAOCAOTONGHOP tham chieu t WORKS
(RB17) MAXL la khoa ngoai cua DANHGIA tham chieu t XEPLOAI. (RB18) MAGROUP la khoa ngoai cua NHANVIEN tham chieu t GROUPS. (RB19) MACV la khoa ngoai cua BAOCAONGAY tham chieu t WORKS. (RB20) MADG la khoa ngoai cua WORKS tham chieu t DANHGIA. (RB21) MADA la khoa ngoai cua WORKS tham chieu t DEAN. (RB22) MANV la khoa ngoai cua PHANCONG tham chieu t NHANVIEN. (RB23) MACV la khoa ngoai cua PHANCONG tham chieu t WORKS. (RB24) MAPER & MAGROUP la khoa ngoai cua GROUP_PER tham chieu t GROUPS & PERMISSION. (RB25) Ngay nhan chc > Ngay sinh + 24. (RB26) Ngay ket thuc >= Ngay bat au. (RB27) Mot cong viec co the co nhieu nhan vien nhng tai mot thi iem ch co mot nhan vien thc hien mot cong viec.

Svth: inh Le Giang Trang 75

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

g)
Works Nhan Vien Phan Cong Gop Y Files

Bang tam anh hng.


Bao Cao Ngay Bao Cao Tong Hop Danh Gia Xep Loai Groups Groups _Per Permissi ons DeAn

T
RB1 RB2 RB3 RB4 RB5 RB6 RB7 RB8 RB9 RB10 RB11 RB12 RB13 RB14 RB15 RB16 RB17 RB18 RB19 RB20 RB21

X T S X T S X T S X T S X T S X T S X T

X T S X T

T S X

X X X X X X X X X X X X X X X X X X X X X X X X X X X X X

Svth: inh Le Giang Trang 76

o an tot nghiep Hai


RB22 RB23 RB24 RB25 RB26 RB27

Gvhd: Ts. Nguyen Thuc

X X

X X X

Svth: inh Le Giang Trang 77

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

8.

Thiet ke chng trnh. S o cau truc chng trnh.

S o Cau Truc Chng Trnh


Quan Ly Cong Viec

He Thong

Quan Ly

Nhan Vien

Tr Giup

CN nhom

anh gia CV

Xem bao cao

CN nhan vien

Thong ke CV

e xuay y kien

Lam ti CSDL

CN cong viec

Bao cao tien o

Phuc hoi CSDL

Kiem tra tien o

Xem anh gia cua quan ly

Xem thong tin NV

Thiet lap che o lam viec

Svth: inh Le Giang Trang 78

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

9.

Thiet ke giao dien. Cac Site map (thay the cho s o dch chuyen trang thai).

WEBSITE Qua n Ly Co ng Vie c

Website QLCV

Login.htm ang nha p he tho ng


Error

MainPage.htm Trang chu

About.htm Trang gi i thie u

Login

Kiem Tra Username & PassWord . jsp

Member Components No i dung ca c trang tha nh pha n

Svth: inh Le Giang Trang 79

o an tot nghiep Gvhd: Ts. Nguyen Thuc Hai Com p on ent cu a Ad m in(Qua n TrHe Th o n g )
Member components Admin component

Trang Thong tin ve Permission

Trang thong tin ve User

Trang thong tin ve Quyen Han cua User

Trang thong tin ve Group

Lam Tuoi CSDL & Sao Luu CSDL

Th e m

Th e m Co

Th e m Kiem tra User thuoc Group nao chua ? K Them User vao Group . jsp Co

Th e m Co Kiem tra Group da co chua ? K Them Group . jsp

Them permision . jsp

Kiem tra User da co ? K

Lam tuoi CSDL va Sao luu CSDL

OK

Them User . jsp

OK
Xoa Xoa Xoa permision. jsp

OK
Xoa

OK OK
Xoa

Xoa User . jsp OK OK


Sua

Xoa User khoi Group . jsp

Xoa group . jsp

OK

OK

Yeu cau kiem tra User trong Group da xoa het chua. * Bat buoc phai xoa tat ca User moi xoa duoc Group.

Sua

Sua permision. jsp

Sua User . jsp

Yeu cau chon User can sua

Sua

Sua

Chon Group de sua.


Sua User trong Group . jsp Sua Group . jsp

OK

OK

Yeu cau kiem tra thong tin lien quan khi sua. - Quyen han o Group cu . - User dang thuoc Group nao, .. . .

OK

OK

Svth: inh Le Giang Trang 80

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Com ponen ts cu a Man ager (Qua n Ly )


Member Components Manager component

Trang Thong Ke . html

Trang TONG HOP User & Cac Bao Cao . html

Trang Danh Sach Cac Thac Mac . html Giai Dap Thac Mac . jsp Thiet Lap Che Do LV . jsp Attach file

Trang Thong Tin Ve Cac Cong Viec .jsp The m

OK

Doi Password . jsp

Cong Viec

Danh gia CV.jsp

Cong Viec . jsp

Phan Cong CV . jsp

Them cong viec .jsp

Thong Ke CV.jsp

Attach file Bang Thong Ke Cac CV

Xoa

Tong Hop Bao Cao.Jsp Kiem Tra Tien Do.jsp Xem Bao Cao . jsp

Nhan vien

Phan Cong truc tiep tu cac File bao cao cua nhan vien khac.

Thiet lap che do lam viec tu de xuat cua nhan vien

Xoa cong viec .jsp

S a

Nhan Vien .jsp

Sua cong viec .jsp

Thong Ke NV.jsp

Bang Thong Ke Cac Nhan Vien

Svth: inh Le Giang Trang 81

o an tot nghiep Hai

Gvhd: Ts. Nguyen Thuc

Com p on ent cu a Em ployee(Nha n Vie n )


Member component Employee component

Trang Gop y & Thac Mac html

De xuat cach giai quyet . html

Xem giap dap cua quan ly jsp Xem cac Danh Gia CV . jsp Bao Cao Tien Do & Dinh kem File . html Ten file & noi dung bao cao

Xem CV duoc giao

Doi Password .jsp

Thac mac & gop y

Subm it OK Bao da goi thanh cong Error

Goi thong tin tren cho Quan ly

De xuat cach giai quyet bang van ban

De xuat cach giai quyet

Tim CV .jsp

Subm it Goi thong tin tren cho Quan ly

OK
Danh sach cac CV duoc giao

Error
Thong bao loi khong thay.

Bao goi khong thanh cong

Add them file cong viec da lam. (trong chuyen nghanh tin hoc)

Add file

Svth: inh Le Giang Trang 82

o an tot nghiep Gvhd: Ts. Nguyen Thuc Hai

Chng V :

TRIEN KHAI NG DUNG

I. Cac phan mem can thiet. ay la ng dung oc lap nen nen no co the chay tren cac he ieu hanh khac nhau nh cac he thong Windows, Unix, . Do o chung ta co the cai at he ieu hanh thuoc mot trong cac he thong tren va cac phan mem sau: Jakarta-Tomcat la mot Servlet/JSP container. That ra theo ly thuyet, ng dung nay co the chay tren bat ky container hay web server nao co ho tr Servlet/JSP nhng chung toi cha th nghiem tren container khac Jakarta-Tomcat 3.1. JDK 1.2 tr len. MySQL Server 3.23.27 ban beta. Hien nay a ra ban 4.0. JDBC Driver loai bon ho tr cho MySQL Server: mm.msql-2.0.4. Xalan va Xerces e chuyen XML thanh HTML bang cach s dung XSLT. Mot trong cac web browser sau: Netscape, Inernet Explorer, Opera tat ca t 4.0 tr len. ng dung nay a th nghiem tren cac web browser nay. VietKey hoac bat ky bo go tieng viet nao khac.

II. Trien khai ng dung web. ng dung web (ng dung server-side java) la mot tap cac trang JSPs, Servlets, HTML va cac tai nguyen khac c goi gon trong mot goi duy nhat va chay tren nhieu platform t nhieu nha san xuat khac nhau. ng dung Web c xac nh trong mot ng dan at biet web server sau: Mot ng dung web co the gom cac thanh phan JavaServer Pages

Svth: inh Le Giang Trang 78

o an tot nghiep Gvhd: Ts. Nguyen Thuc Hai Servlets Cac lp tien ch Cac tai nh xml, html, Enterprise JavaBean (EJB) oi vi cac ng dung x ly phan tan. Cac media nh images, sounds,

Cac oi tng client nh javabeans, applets, cac lp, Mo hnh hoat ong cua The Web Work. The Web Work hoat ong da tren mo hnh ba lp (3 layer). Sau ay la mo hnh ba lp tong quat cua ng dung Web.

Mo Hnh ng Dung Web 3 Lp Tong Quat


User Service Interface (Web Page) Business Service Data Service

Data Web Client Web Server

Database Server

Internet/Intranet

Tren nen Java ho tr ca hai ky thuat x ly Web ong la Servlet va JSP. Do o vi tng ky thuat toi co chi tiet hai mo hnh (co the nhieu hn) da tren mo hnh ba lp tong quat nay va goi la Kien Truc Mo Hnh Mot va Mo Hnh Hai. ng dung s dung ca hai mo hnh web tren nen Java la Kien Truc Mo Hnh Mot va Mo Hnh Hai ma a va ang con tranh luan tren cac sites noi tieng nh javaworld.com, developer.com, weblogic.com. Trong mo hnh mot, mot trang JSP am nhan ca viec x ly yeu cau va tra ve cac ket qua cho client. Svth: inh Le Giang Trang 79

o an tot nghiep Gvhd: Ts. Nguyen Thuc Hai ay van co s tach biet gia x ly va hien th bi v tat ca s truy cap d lieu eu c Beans thc hien. Mo hnh nay thch hp cho cac ng dung n gian, thch hp vi cac trang JSP co nhieu ma HTML. Xem hnh ve sau:

JSP Model 1 Architecture


Kie n tru cm o hnh m o t
1 Request
4

JSP

Web Server

Trong mo hnh kien truc hai, o la s ket hp ca hai ky thuat x ly web ong la Servlet va JSP. Mo hnh nay rat thch vi vi cac ng dung co x ly phc tap. ay, Servlets hoat ong nh mot tram ieu khien (controller) chu trach nhiem x ly cac yeu cau va khi tao cac components (Beans, Custom Tag) cho JSP s dung. Mo hnh nay tach phan x ly va trnh bay ro rang nhat.

Svth: inh Le Giang Trang 80

Web Client

Response

Beans Custom Tag

Data Source

o an tot nghiep Gvhd: Ts. Nguyen Thuc Hai

JSP Model 2 Architecture


Kie n tru c mo hnh hai
Web Server 1 Request Servlet (Controller)
2 Kh i ta o

III. Hng dan s dung.


Sau khi a cai at ng dung, ban m browser, trong address bar go a ch: htttp://servername:port/work/html/showLogin.html . servername: ten server cua ban, tren may cuc bo thng la localhost. port: so cong ma web server ang m. Neu so port la 80 th thong so port khong can nhap.

Neu ban s dung thng xuyen ng dung, ban nen gan gia tr mat nh cho browser vi a ch tren. Khi ban m browser, ng dung t ong c goi ma ban khong can phai go a ch tren address bar. Thc hien cong viec nay trong ba web browser: Internet Explorer, Netscape, Opera nh sau: Internet Explorer: Vao Tools menu, chon Internet Options. Trong hop thoai Internet Options, chon General tab, trong khung Home page co Address textbox cho phep ban go a ch tren. Svth: inh Le Giang Trang 81

Web Client

Beans
4

5 Response

JSP (View)

Custom Tag
(Model)

Data Source

o an tot nghiep Gvhd: Ts. Nguyen Thuc Hai Netscape: Vao Edit menu, chon Preferences. Trong hop thoai Preferences, chon navigator trong hop thoai category, hien ra mot trang Navigator, chon Home page button, trong khung Home Page co Location textbox nhap vao a ch tren. Opera: Vao Navigation menu, chon Set home page, hien ra mot input box cho chep ban nhap a ch mi vao.

KET LUAN
ng dung a ap ng c yeu cau e ra trong giai oan phan tch thiet ke he thong. ng dung ket hp c cac cong nghe oc lap nen JSP, XML, XSLT, MySQL Server do o co the chay tren nhieu nen, nhieu Web Server va nhieu Web Broswer khac nhau. Ve c s d lieu, s o thc the ket hp at dang chuan ba. ng dung s dung ca hai mo hnh web la Kien Truc Mo Hnh Mot va Hai JSP Model 1 and 2 Architecture

Svth: inh Le Giang Trang 82

o an tot nghiep Gvhd: Ts. Nguyen Thuc Hai

HNG PHAT TRIEN


Cac hng phat trien sau se lam cho ng dung hoan thien hn. M rong ng dung vi quy mo ln hn, co kha nang trao oi d lieu va x ly vi ng dung khac. Phat trien ng dung e download cac files bao cao ve client. Quan ly cac hnh anh lien quan ve cong viec cho ng dung sinh ong hn. Dung DOM x ly XML ngay tai client. Cai at cac rang buot toan ven tren database server trong phien ban mi cua he quan tr c s d lieu MySQL (phien ban 4.0). Do phien ban hien tai (ang s dung MySQL 3.23.27) khong ho tr khoa ngoai, check, rule va trigger.

Svth: inh Le Giang Trang 83

o an tot nghiep Ts. Nguyen Thuc Hai

Gvhd:

DANH SACH WEB SERVERS HO TR JSP


e chay c JSP chung ta can mot trong cac Web Server sau. Hay xem cac hng dan cai at, ni at cac JSP files, e thc thi c trong Web Servers eu co ay u trong tng Web Server khi chung ta download ve. Thuoc To Ten Web Chc / Servers Cong Ty Jakarta Tomcat

Trang Web

Apache http://jakarta.apache.org/ Org.

JavaServer Sun http://java.sun.com/products/servlet/dow Web Micro nload.html Developmet system Kit (JSWDK) JRun ServletExec Allaire http://www.allaire.com/products/jrun/ Inc. New http://newatlanta.com/ Atlanta

Lite Gefion http://www.gefionsoftware.com/ WebServer Software Java Web Server WebSphere Resin Sun http://www.sun.com/software/jwebserver/ Micro try/ system IBM Corp. http://www.ibm.com/

Caucho http://www.caucho.com/ Technolo gy

Svth: inh Le Giang Trang 84

o an tot nghiep Ts.Nguyen Thuc Hai

Gvhd:

BANG THUAT NG
action Mot element trong trang JSP co the tac ong len nhng oi tng ngam nh va oi tng serverside khac hoac co the nh ngha bien kch ban mi (xem element). custom action Mot action c nh ngha linh hoat bi bo mo ta th vien tag va tap cac lp java va c a vao trang JSP bang taglib directive. declaration Mot scripting element ma khai bao cac method, bien, hoac ca hai trong trang JSP. Co cu phap <%! javacode %> (xem scripting element). directive Mot element trong trang JSP co nhiem vu hng dan JSP container va c thong dch vao thi gian runtime. Co cu phap <%@ %> (xem JSP container va scripting element). element Trnh thong dch JSP cong nhan element nh la mot thanh phan trong trang JSP. Mot element co the la directive, action, scripting element. Element bat au vi start tag, noi dung va end tag, neu khong co noi dung th no dung cu phap empty tag. expression Mot scripting element cha bieu thc cua ngon ng java va c at vao oi tng ngam nh out. Co cu phap <%= java expression %> implicit object Mot oi tng server-side c nh ngha bi JSP container va luon co san trong trang JSP ma khong can khai bao. Co cac oi tng nh request, response, pageContext, session, application, out, config, page, exception. JSP container Mot thc the cap he thong cung cap s hoat ong, quan ly chu trnh song cho cac thanh phan (conponent) JSP va Servlet. Svth: inh Le Giang Trang 85

o an tot nghiep Gvhd: Ts.Nguyen Thuc Hai JSP page implementation class Mot lp cua ngon ng lap trnh java, cu the la mot Servlet, c trnh bay vao thi gian runtime cua mot trang va nhan oi tng request roi tra ve mot oi tng response. Lp cai at trang co the dung cac dch vu cua JSP container, gom ca Servlet API va JSP API. persistent Mot oi tng c serialization vao a va cac thiet b lu tr khac. Persistent co cac dang nh files, RDBMS (Relational database management systems), ODBMS (Object database management systems). reflection La c che cho phep ma Java tm ra cac thong tin ve cac trng, phng thc va ham tao cua cac lp c tai. scripting element La declaration, scriptlet hay expression, at ta JSP nh ngha cac th nay theo cu phap tag. Toan bo noi dung ng canh c viet theo ngon ng java. scriptlet Mot scripting element cha bat ky oan ma cua ngon ng kch ban nao trong trang JSP. JSP v1.2 la phien ban mi nhat ch ho tr ngon ng java trong cac trang JSP. Co cu phap <% javacode %>. Tuy nhien co mot so JSP container ho tr ngon ng javascript nh RESIN. serialization La mot tien trnh lu tr toan bo mot oi tng vao a hoac cac thiet b lu tr khac. Ngc lai, mot tien trnh phuc hoi oi tng th goi la deserialization. servlet Mot ky thuat server-side c viet bang Java, JSP phat trien da tren nen tang cua servlet. tag Gom oan van ban nam gia cap dau <>, co ten, thuoc tnh. Co ba loai tag: start tag, end tag, va empty tag. Svth: inh Le Giang Trang 86

o an tot nghiep Ts.Nguyen Thuc Hai V du: <book> <!-- start tag --> content </book> <!-- end tag -->

Gvhd:

<book name= Core Servlet & JSP/> <!-- empty tag --> tag handler Mot lp java c cai at cac giao tiep Tag hoac TagBody ma c trnh bay vao luc thi gian runtime oi vi mot custom action. tag library Mot tap cac custom action c mo ta trong bo mo ta th vien tag (tag library descriptor) va cac lp java. tag library descriptor Mot tai lieu XML mo ta th vien tag. transient Ngc lai vi persistent, hau het cac oi tng trong bo nh c goi la transient, co ngha la chung se chet i khi cac tham chieu cua chung ra khoi pham vi hoac tac nguon ien.

Svth: inh Le Giang Trang 87

o an tot nghiep Ts.Nguyen Thuc Hai

Gvhd:

TAI LIEU THAM KHAO


Ten Tai Lieu Ach1 Servlets and JSP JSP JavaBeans Framework Tac Gia Marty Hall By IBM Na Loai Tai m Lieu 2000 Sach 2000 Web site 2000 Web site 2001 Web site 1999 Sach 2000 Sach

Tag Libraries Tutorial (v 1.0) Sun Microsystem Xalan 2.0 & Xerces 1.3 (API) By apache.org XML and Java XML Professional Luan van tot nghiep Web Books Giao trnh ly thuyet va bai tap Java Lap trnh ng dung Web vi JSP/ Servlet Nguyen Tien Dung (Chu Bien) Hiroshi Maruyama Didier Martin Cac khoa trc

CD Rom 1999 Sach

Nguyen Phng 2001 Sach Lan Hoang c Hoang 2001 Sach

Java Lap Trnh C S D Nguyen Tien Lieu Nguyen Van Tam Nguyen Van Hoai

Svth: inh Le Giang Trang 88