KMUTNB - Internet Programming 7/7

Loading...

Flash Player 9 (or above) is needed to view presentations.
We have detected that you do not have it on your computer. To install it, go here.

0 comments

Post a comment

    Post a comment
    Embed Video
    Edit your comment Cancel

    Favorites, Groups & Events

    KMUTNB - Internet Programming 7/7 - Presentation Transcript

    1. JSP Programming Technique By: Mr. PHUPHA PUNYAPOTASAKUL ( ภูผา ปัญญาโพธาสกุล )
    2. Searching
      • Basic concept: Build SQL based on input criteria prod_name like ? and cat_id=?
      • เติม AND หรือไม่เติม boolean is_first=true;
    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
    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 ()
    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
    6. Paging
      • หาจำนวน record
        • Select count(*) from .. หรือ
        • rs.last(); rs.getRow();
      • การไปยังหน้าที่ต้องการ
        • ให้ npp คือ จำนวน record per page
        • ให้ page คือ หน้าที่ต้องการแสดง เริ่มต้นจาก 1..
    7. Paging
      • การไปยังหน้าที่ต้องการ
        • หาก่อนว่าเริ่มต้นแสดง record ที่เท่าไหร่
        • for ( int i=0;i<(page-1)*npp && rs.next();i++){}
        • หรือ rs.absolute((page-1)*npp+1);
      • การวน Loop for(int i=0;i<npp && rs.next();i++){
    8. Paging
      • หาว่ามีทั้งหมดกี่หน้า
        • ให้ numrow คือจำนวน record ทั้งหมด
        • ( int )Math. ceil (( float )numrow/npp);
      • การแสดง icon back, next
        • ให้ numpage คือจำนวนหน้าทั้งหมด
        • แสดง icon back หาก page>1
        • แสดง icon next หาก page<numpage
    9. Data Formatting
      • Number เช่น price DecimalFormat df= new DecimalFormat(&quot;#,###.0000&quot;);
      • Date เช่น วันที่คืน วันที่ยืม SimpleDateFormat sf= new SimpleDateFormat(&quot;d/M/yyyy&quot;); sf.format(date);
      • ปกติ date format จะอิงกับ reginal ของ OS เช่น แสดงชื่อเดือนเป็นภาษาไทย แสดงปีเป็น พุทธศักราช
    10. Data Formatting
      • การเปลี่ยน Locale ของการแสดงผล Date
        • Locale loc= new Locale(&quot;en&quot;,&quot;US&quot;);
        • SimpleDateFormat sf= new SimpleDateFormat(&quot;d/M/yyyy&quot;,loc);
    11. Data Formatting
      • String ปัญหาที่อาจเกิดขึ้น เช่น
        • จำนวนนักเรียน < ปัจจุบัน แต่จำนวนหนังสือ > ปัจจุบัน
        • <input value=&quot;She is &quot;Hero&quot;..&quot;>
        • <a href=&quot;book.jsp?name=Tom&Jerry&quot;
      • &quot;&&quot;,&quot;<&quot;,&quot;>&quot;,&quot;&quot;&quot;,&quot;'&quot; เปลี่ยนเป็น &quot;&amp;&quot;,&quot;&lt;&quot;,&quot;&gt;&quot;,&quot;&quot;&quot;,&quot;&#39;&quot;
    12. Data inputting
      • Number อาจมีการ input 1,000.25 ซึ่งจะผิด format และเกิด Exception ควรตัด comma ออก
      • Date ใช้ function parse(input) ใน SimpleDateformat ร่วมกับ Calendar picker
    13. Parameter Roundtrip
      • Next, Back มี parameter อะไรบ้าง ต้องส่งต่อให้ครบ e.g. book.jsp?page=2&book_name=<%=request.getParameter(&quot;book_name&quot;)&cat_id=<%=request.getParameter(&quot;cat_id&quot;)%>
      • ระวังค่า Null ในกรณีที่ไม่มี Parameter
      • Submit แบบ GET ใช้ reqest.queryString(); และใช้ regular expression replace เฉพาะ page=xx
    14. Parameter Roundtrip
      • public static String chPage( int page,String url){ if (url.indexOf(&quot;?&quot;)==-1) return url+&quot;?page=&quot; + page; else if (url.indexOf(&quot;?page=&quot;)!=-1) return url.replaceFirst(&quot;[?]page=\d+&quot;,&quot;?page=&quot; + page); else return url.replaceFirst(&quot;[?]&quot;,&quot;?page=&quot; + page + &quot;&&quot;); }
      • การ Edit แล้วกลับหน้าเดิม
        • request.getHeader(&quot;Referer&quot;) เก็บไว้ใน Hidden Field แล้ว submit ไปพร้อมกับแบบฟอร์ม
        • หลังจาก Update แล้วกลับไปยังหน้าเดิม
    15. Connection Pooling
      • Tomcat http :// 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();
    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
    17. Object caching / pooling
      • ควร caching หรือ pooling หาก การสร้าง object มี overhead ค่อนข้างสูง
      • Pooling ถ้า object เหมือนกันทุกประการ ใช้แทนกันได้ แต่ใช้พร้อมกันไม่ได้ ( เช่น connection )
      • Caching ถ้า object ไม่เหมือนกันทุกประการ มีโอกาสที่จะถูกใช้ซ้ำๆ ใช้พร้อมกันได้
        • Oldest preemption ลบตัวที่เก่าที่สุดก่อน
        • Least Frequency preemption ลบตัวที่มีความถี่ในการถูกใช้น้อยที่สุด
    18. Personalization
      • แต่ละ Session จะมีการแสดงผลที่แตกต่างกัน
        • Time zone
        • Language / Regional
        • Date format / Number format
      • การใช้ cookie หรือ Database เพื่อจำว่า user แต่ละคน ควรจะใช้ setting แบบใด
    19. Input validation
      • หากไม่มีการ validate อาจเกิด error หรือ exception ได้
      • Client side validation สะดวกกับผู้ใช้ ไม่จำเป็นต้อง submit ก่อนจึงจะทราบข้อผิด แต่มีโอกาสหลุดหาก browser disable JavaScript
      • Server side validation โอกาส หลุดน้อย แต่ข้อเสียคือ จะต้อง recover ค่าที่ input ด้วย
      • การใช้ Java Script Library เพื่อลดงาน
      • อย่าลืม cancel การ submit หาก validate แล้วไม่ผ่าน
    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
    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
    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
    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
    24. Set Input Value
      • วิธีปกติ แทรก Code ลงใน input tag <input type=&quot;radio&quot; name=&quot;xx&quot; value=&quot;xx&quot; <%=(&quot;xx&quot;.equals(val))?&quot;checked&quot;:&quot;&quot;%>> หรือ <input type=&quot;radio&quot; name=&quot;xx&quot; value=&quot;xx&quot; <% if(&quot;xx&quot;.equals(val)) out.print(&quot;checked&quot;); %>>
      • สามารถใช้ Java Script แทนได้
      • Checkbox, Radio button ถือเป็น array ยกเว้นถ้ามีอันเดียว จะไม่ถือเป็น array
    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 ;
          • }
        • }
      • }
    26. Field Mapping
      • กำหนดให้ parameter ขึ้นต้นด้วย ds_, dn_, dd_, dt_ สำหรับ string, number, date, timestamp ตามลำดับ
      • req uest .getParameterNames() ; เพื่อดึงชื่อ parameter ทั้งหมด
      • Build insert, update SQL โดยดูจากชื่อ parameter
    27. Question & Answer
    SlideShare Zeitgeist 2009

    + phuphaxphuphax Nominate

    custom

    504 views, 0 favs, 2 embeds more stats

    Lecture for King Mongkut's University of Technology more

    More info about this document

    © All Rights Reserved

    Go to text version

    • Total Views 504
      • 472 on SlideShare
      • 32 from embeds
    • Comments 0
    • Favorites 0
    • Downloads 4
    Most viewed embeds
    • 31 views on http://www.abzolutetech.com
    • 1 views on http://localhost:8080

    more

    All embeds
    • 31 views on http://www.abzolutetech.com
    • 1 views on http://localhost:8080

    less

    Flagged as inappropriate Flag as inappropriate
    Flag as inappropriate

    Select your reason for flagging this presentation as inappropriate. If needed, use the feedback form to let us know more details.

    Cancel
    File a copyright complaint
    Having problems? Go to our helpdesk?

    Categories