kmutnb - internet programming 7/7

27
JSP Programming Technique By: Mr. PHUPHA PUNYAPOTASAKUL (ภูผา ปัญญาโพธาสกุล)

Upload: phuphax

Post on 09-Jul-2015

1.650 views

Category:

Education


2 download

DESCRIPTION

Lecture for King Mongkut's University of Technology North Bangkok (KMUTNB) / Computer Science / Internet Programming Course by PHUPHA

TRANSCRIPT

Page 1: KMUTNB - Internet Programming 7/7

JSP Programming Technique

By: Mr. PHUPHA PUNYAPOTASAKUL

(ภูผา ปัญญาโพธาสกุล)

Page 2: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 2

Searching

• Basic concept: Build SQL based on input criteria

prod_name like ? and cat_id=?

• เตมิ AND หรือไม่เติม

boolean is_first=true;

Page 3: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 3

Joining

• SELECT * from book b, category c WHERE b.cat_id=c.cat_id and c.cat_name like ?

• Many-2-Many อาจจะเกิด duplicate record

SELECT distinct b.* from book b, bclink bc, category c where b.book_id=bc.book_id and bc.cat_id=c.cat_id

Page 4: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 4

Boolean Search

• Boolean search e.g.( ยา | medical ) & general

• ใช ้StringTokenizer(in,"()|&!",true);– Ignore space– convert | & ! to OR AND NOT– convert other to criteria e.g.

book_name like ?– put final result in ()

Page 5: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 5

Boolean Search

• Validation– Invalid search input may cause

exception– การตรวจสอบความถกูต้องของวงเลบ็

Start from 0, open count increment, close count decrement

• No -1 during checking• End up 0

– การตรวจสอบความถกูต้องอื่นๆ State checking

Page 6: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 6

Paging

• หาจำานวน record– Select count(*) from .. หรือ– rs.last();

rs.getRow();

• การไปยังหนา้ที่ต้องการ– ให ้npp คือ จำานวน record per page– ให ้page คือ หน้าท่ีต้องการแสดง เริ่มต้น

จาก 1..

Page 7: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 7

Paging

• การไปยังหนา้ที่ต้องการ– หากอ่นวา่เริ่มต้นแสดง record ท่ีเท่าไหร่– for(int i=0;i<(page-1)*npp &&

rs.next();i++){}– หรือ rs.absolute((page-1)*npp+1);

• การวน Loopfor(int i=0;i<npp && rs.next();i++){

Page 8: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 8

Paging

• หาว่ามีทั้งหมดก่ีหนา้– ให ้numrow คือจำานวน record ท้ังหมด– (int)Math.ceil((float)numrow/npp);

• การแสดง icon back, next– ให ้numpage คือจำานวนหน้าท้ังหมด– แสดง icon back หาก page>1– แสดง icon next หาก page<numpage

Page 9: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 9

Data Formatting

• Number เช่น priceDecimalFormat df=new DecimalFormat("#,###.0000");

• Date เชน่ วันที่คืน วันที่ยืมSimpleDateFormat sf=new SimpleDateFormat("d/M/yyyy");sf.format(date);

• ปกติ date format จะอิงกับ reginal ของ OS เช่น แสดงชือ่เดือนเป็นภาษาไทย แสดงปีเป็น พุทธศักราช

Page 10: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 10

Data Formatting

• การเปลี่ยน Locale ของการแสดงผล Date– Locale loc=new Locale("en","US");– SimpleDateFormat sf=new

SimpleDateFormat("d/M/yyyy",loc);

Page 11: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 11

Data Formatting

• String ปญัหาที่อาจเกิดขึ้น เช่น– จำานวนนักเรียน < ปัจจุบัน แต่จำานวน

หนังสือ > ปัจจบุัน– <input value="She is "Hero"..">– <a href="book.jsp?name=Tom&Jerry"

• "&","<",">","\"","\'" เปลี่ยนเปน็ "&amp;","&lt;","&gt;","&quot;","&#39;"

Page 12: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 12

Data inputting

• Number อาจมีการ input 1,000.25 ซึง่จะผดิ format และเกดิ Exception ควรตัด comma ออก

• Date ใช ้function parse(input) ใน SimpleDateformat ร่วมกับ Calendar picker

Page 13: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 13

Parameter Roundtrip

• Next, Back มี parameter อะไรบ้าง ต้องสง่ต่อให้ครบ e.g. book.jsp?page=2&book_name=<%=request.getParameter("book_name")&cat_id=<%=request.getParameter("cat_id")%>

• ระวังค่า Null ในกรณีที่ไม่ม ีParameter

• Submit แบบ GET ใช้ reqest.queryString(); และใช ้regular expression replace เฉพาะ page=xx

Page 14: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 14

Parameter Roundtrip

• public static String chPage(int page,String url){ if(url.indexOf("?")==-1) return url+"?page=" + page; else if(url.indexOf("?page=")!=-1) return url.replaceFirst("[?]page=\\d+","?page=" + page); else return url.replaceFirst("[?]","?page=" + page + "&");}

• การ Edit แล้วกลับหนา้เดิม– request.getHeader("Referer") เก็บไว้ใน Hidden

Field แลว้ submit ไปพร้อมกับแบบฟอร์ม– หลังจาก Update แล้วกลบัไปยังหนา้เดมิ

Page 15: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 15

Connection Pooling

• Tomcathttp://tomcat.apache.org/tomcat-5.5-doc/jndi-datasource-examples-howto.html

• Example• Context initContext = new InitialContext();

DataSource ds = (DataSource)initContext.lookup(jndiname);Connection conn=ds.getConnection();

Page 16: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 16

Resource Controller

• สร้าง Class ชื่อว่า ResourceCtrl

• ทุกคร้ังที่ get Connection, สร้าง Statement, execute Query ให้ทำาผ่าน Class นี้ Class นี้จะบนัทึก connection, statement, resultset เก็บไว้

• การ close ให้ทำาผ่าน Class นีด้้วย Class นี้จะ mark เอาไว้ว่า object ไหนถูก close แล้ว

• สดุท้ายให้ class นีท้ำาการ close object ทั้งหมดที่ยังไม่เคยถูก close ใน finally block

Page 17: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 17

Object caching / pooling

• ควร caching หรือ pooling หาก การสร้าง object มี overhead คอ่นข้างสงู

• Pooling ถ้า object เหมือนกันทุกประการ ใช้แทนกันได้ แต่ใช้พร้อมกันไม่ได้ (เชน่ connection)

• Caching ถ้า object ไม่เหมอืนกันทุกประการ มีโอกาสที่จะถูกใช้ซำ้าๆ ใช้พร้อมกันได้– Oldest preemption ลบตัวท่ีเกา่ท่ีสุดก่อน– Least Frequency preemption ลบตัวท่ีมี

ความถีใ่นการถูกใชน้้อยท่ีสุด

Page 18: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 18

Personalization

• แต่ละ Session จะมีการแสดงผลที่แตกต่างกัน– Time zone– Language / Regional– Date format / Number format

• การใช้ cookie หรือ Database เพื่อจำาว่า user แต่ละคน ควรจะใช้ setting แบบใด

Page 19: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 19

Input validation

• หากไม่มีการ validate อาจเกิด error หรือ exception ได้

• Client side validation สะดวกกับผู้ใช้ ไม่จำาเป็นต้อง submit กอ่นจึงจะทราบข้อผดิ แต่มีโอกาสหลดุหาก browser disable JavaScript

• Server side validation โอกาส หลุดน้อย แต่ขอ้เสียคือ จะต้อง recover ค่าท่ี input ด้วย

• การใช ้Java Script Library เพ่ือลดงาน• อย่าลืม cancel การ submit หาก validate

แล้วไม่ผา่น

Page 20: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 20

Application configuration

• web.xml<init-param><param-name>xxx</param-name><param-value>xxxx</param-value></init-param></servlet>

config.getInitParameter(name);

• ResourceBundle และ ไฟล์ *.propertiesสนับสนนุ internationalization

• Database / Text file, XML file

Page 21: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 21

File Upload

• http://www.servlets.com/cos/• http://commons.apache.org/fileupload/

• Multipart Content ใน HTTP POST

• หลักการคือ save file ที่ upload ทั้งหมดใน temporary directory หลังจากนั้นจึงย้ายไปยัง directory ที่ต้องการ โดยอาจมีการเปลี่ยนชือ่ไฟล์ เชน่ book_<book_id>.<extension>

• การอ้างถึงสามารถทำาได้โดยใช ้ID

Page 22: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 22

File Upload

• การ check extension ที่อนุญาต• การกำาหนด size ของการ upload ที่

อนญุาต• Unlimited file uploading

– Table file_upload ประกอบด้วย file_id, table_name, table_id, group_name, extension

– ใช้หน้า upload ร่วมกัน เขียน code ครั้งเดียว

• การไม่ต้องเก็บ Extension สามารถทำาได้โดย Implement java.io.FileFilter

Page 23: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 23

Sending Email

• http://java.sun.com/products/javamail/

• Using SMTP Server

• Unless you're using Java SE 6, you will also need the JavaBeans Activation Framework

• http://java.sun.com/products/javabeans/jaf/index.jsp

• Authentication

Page 24: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 24

Set Input Value

• วิธีปกติ แทรก Code ลงใน input tag<input type="radio" name="xx" value="xx" <%=("xx".equals(val))?"checked":""%>>

หรือ <input type="radio" name="xx" value="xx" <% if("xx".equals(val)) out.print("checked"); %>>

• สามารถใช ้Java Script แทนได้• Checkbox, Radio button ถือเป็น array

ยกเว้นถ้ามีอันเดียว จะไมถ่ือเป็น array

Page 25: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 25

Set Input Value

function selectRadio(sObj,dVal){if(sObj==null) return;if(isNaN(sObj.length)){

sObj.checked=true;return;

}if(sObj.length>0) sObj[0].checked=true;for(var i=0;i<sObj.length;i++){

if(sObj[i].value==dVal){sObj[i].checked=true;break;

}

}

}

Page 26: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 26

Field Mapping

• กำาหนดให้ parameter ขึ้นต้นด้วย ds_, dn_, dd_, dt_ สำาหรับ string, number, date, timestamp ตามลำาดับ

• request.getParameterNames(); เพือ่ดึงชือ่ parameter ทั้งหมด

• Build insert, update SQL โดยดูจากชือ่ parameter

Page 27: KMUTNB - Internet Programming 7/7

12/10/09 Free template from www.brainybetty.com 27

Question & Answer