เจเอสพี จาวาเซิร์ฟเวอร์เพจ JSP,Servlet,ODBC,Tomcat

ปรับปรุง : 2550-09-05 (ปรับการแสดงผล)
JSP (Java Server Pages) [ reference ]
เป็น Script language ที่ทำงานบนเครื่อง server เช่นเดียวกับ perl, php asp หรือ cold fusion เป็นต้น โดยมีโครงสร้างภาษาแบบ JAVA โดยมีตัวแปลภาษาคือ J2SE หรือ JRE
ภาษา JSP เป็นเทคโนโลยีของจาวา สำหรับสร้าง HTML, XML หรือตามที่ผู้ใช้ร้องขอ มีตัวแปลภาษาคือ Tomcat Apache และ Java Compiler พัฒนาโดย James Duncan Davidson ค.ศ.2000 ตัวแปลภาษาจะสร้าง Servlet และเปลี่ยนเป็น Byte Code สำหรับถูกเรียกใช้ครั้งต่อไป จาก JSP Source Code
JavaServer Pages (JSP) is a Java technology that allows software developers to dynamically generate HTML, XML or other types of documents in response to a Web client request. The technology allows Java code and certain pre-defined actions to be embedded into static content.
The JSP syntax adds additional XML-like tags, called JSP actions, to be used to invoke built-in functionality. Additionally, the technology allows for the creation of JSP tag libraries that act as extensions to the standard HTML or XML tags. Tag libraries provide a platform independent way of extending the capabilities of a Web server.
แนะนำเว็บ (Web Guides)
+ Session Tutorial

วิธีติดตั้ง TOMCAT Apache Web Server ใน thaiall.com/server (ไม่พบปัญหาใน Tomcat 5.0.27)
PowerPoint เตรียมสอน JSP jsp2005.zip ที่ ม.เทคโนโลยีราชมงคล ลำปาง 3 ชั่วโมง
เว็บไซต์ที่ใช้ JSP
- th.lge.com
- truelife.com
- airasia.com
- amway.co.th
- bellshopping.com
- กรมศุลกากร
- dsi.go.th
- krisdika.go.th
- labour.go.th
- สำนักงานสถิติแห่งชาติ
- ตลาดหลักทรัพย์
- sipaphuket.org
- sonyericsson.com
- thaiair.com
- thaiechamber.com
- thailocaladmin.go.th
- truecorp.co.th

แฟ้มสำหรับ Java 5.0
+ servlet-api.jar
    คำแนะนำ
  1. ความเห็นส่วนตัวว่า ท่านควรศึกษา พื้นฐาน JAVA และการเขียนเว็บด้วย PHP มาก่อน จะทำให้เข้าใจ JSP ง่ายขึ้น
  2. เริ่มต้นด้วยการ ติดตั้ง java และ tomcat โดยดูรายละเอียดจาก thaiall.com/webserver
    สมมุติให้ติดตั้ง J2SE ใน c:\j2sdk1.4.0_03 และติดตั้ง Tomcat5 ใน c:\Tomcat5
  3. เมื่อติดตั้งแล้วให้ startup tomcat แล้วทดสอบเปิดเว็บ http://localhost:8080
    ถ้าเห็นคำว่า Apache Jakarta Project ก็แสดงว่าที่เปิดบริการ webserver ที่บริการ JSP ได้แล้ว
    หลัง Startup Tomcat ต้องรอนาทีกว่า จึงจะเปิด http://localhost:8080 ออกนะครับ .. ดังนั้นอย่าใจร้อน
  4. เขียนโปรแกรมแรก เช่น x.jsp แล้วพิมพ์อักษร abc 3 ตัว ส่งเข้าไปเก็บใน c:\Tomcat5\webapps\root
  5. ทดสอบโปรแกรมแรกที่เขียนขึ้นด้วยการเปิดเว็บ http://localhost:8080/x.jsp จะพบตัวอักษร abc ใน browser
  6. เมื่อแก้ code .jsp แล้วผลลัพธ์ในเว็บไม่เปลี่ยนเพราะกำหนด configuration ไว้ ถ้าให้เปลี่ยนต้องแก้ configuration
    โดยเปิดแฟ้ม c:\Tomcat5\conf\server.xml ด้วย notepad แล้วจะพบ 4 บรรทัดต่อไปนี้ในบรรทัดที่ 345 -348
            <!-- Define properties for each web application.  This is only needed
                 if you want to set non-default properties, or have web application
                 document roots in places other than the virtual host's appBase
                 directory.  -->
    
    แล้วเพิ่มบรรที่ใหม่ในบรรทัดที่ 349 ว่า <DefaultContext reloadable="true" />
    มีผลให้ทุกครั้งที่แก้ไข .jsp จะทำให้ผลการแก้ไขมีผลต่อการแสดงผลใน browser ด้วย
    แต่การแก้ไขในครั้งแรกนี้ ต้อง shutdown แล้ว startup Tomcat ใหม่นะครับ
  7. การเชื่อมต่อ MySQL เคยมีโปรแกรมสนับสนุนที่ http://mmmysql.sourceforge.net แต่ปัจจุบันแนะนำให้ Download รุ่นใหม่จาก http://www.mysql.com
    ตัวอย่างชื่อโปรแกรม
    - mm.mysql-2.0.4-bin.jar (รุ่นเก่า http://mmmysql.sourceforge.net/oldDist.html)
    - mysql-connector-java-3.1.10.zip (รุ่นใหม่ http://www.mysql.com/downloads/api-jdbc.html)
  8. Java servlet ต่างกับ JSP คือ jsp เป็น script language เหมือนอย่าง ASP และ PHP ที่แทรก html code เข้าไปได้เลย แต่ Java servlet เป็นโปรแกรมที่ไม่สามารถแทรก html code ได้ตามใจเหมือน jsp
    Java serverlet เป็นโปรแกรมภาษา JAVA ต้องเริ่มต้นตามหลักการเขียนภาษา JAVA ดังตัวอย่าง hi.java ด้านล่างนี้
      import java.io.*;
      import java.text.*;
      import java.util.*;
      import javax.servlet.*;
      import javax.servlet.http.*;
      public class hi extends HttpServlet {
        public void doGet(HttpServletRequest request, HttpServletResponse response)
        throws IOException, ServletException
        {
         response.setContentType("text/html");
         PrintWriter out = response.getWriter();
         out.println("<html><head>");
         String title = "hello";
         out.println("<title>" + title + "</title></head>");
         out.println("<body bgcolor=\"yellow\">");
         out.println(title);
         out.println("</body></html>");
        }
      }
      
Servlet Testing
1. To compile hi.java
c:\Tomcat5\webapps\servlets-examples\WEB-INF\classes>path=%path%;c:\j2sdk1.4.0_03\bin
c:\Tomcat5\webapps\servlets-examples\WEB-INF\classes>javac -classpath c:\Tomcat5\common\lib\servlet-api.jar hi.java
2. Change c:\Tomcat5\webapps\servlets-examples\WEB-INF\web.xml
    <servlet>
        <servlet-name>HelloWorldExample</servlet-name>
        <servlet-class>HelloWorldExample</servlet-class>
    </servlet>
    <servlet>
        <servlet-name>hi</servlet-name>
        <servlet-class>hi</servlet-class>
    </servlet>
    :::::::
    :::::::
    <servlet-mapping>
        <servlet-name>HelloWorldExample</servlet-name>
        <url-pattern>/servlet/HelloWorldExample</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>hi</servlet-name>
        <url-pattern>/servlet/hi</url-pattern>
    </servlet-mapping>
3. Start tomcat and open the servlet file
http://localhost:8080/servlets-examples/servlet/hi
    แนะนำเว็บ (Web Guides)
  1. http://java.sun.com/products/jsp
  2. http://java.sun.com/docs/
  3. http://java.sun.com/products/jsp/download.html
  4. http://java.sun.com/products/servlet/download.html
  5. http://pantip.inet.co.th/tech/developer/
  6. http://pdf.coreservlets.com
  7. http://www.anse.de/mysqlfront/
  8. http://www.webappcabaret.com (Java EE)
  9. http://www.hpcc.nectec.or.th/C1/document/tomcat.html

  1. http://www.forte.com
  2. http://www.jarticles.com
  3. http://www.javacentrix.com
  4. http://www.jspin.com
  5. http://www.jspinsider.com
  6. http://www.mycgiserver.com
  7. http://www.siamdev.com
  8. http://www.narisa.com
รวมโปรแกรมพื้นฐาน ที่เขียนด้วย JSP
1. พิมพ์เลขสุ่มมา 1 เลข ซึ่งเป็นจำนวนเต็มไม่เกิน 10
    <body><pre>
    <%
      Random r = new Random();
      out.println(r.nextInt(10));
    %>
    </pre></body>
    
2. โปรแกรมพิพพ์ 1 ถึง 10 ด้วย for
    <body><pre>
    <%
    for (int i=1;i<=10;i++)
    {
      out.println(i);
    }
    %>
    </pre></body>
    
3. โปรแกรมรับค่าจาก method get อย่างง่าย
    <body>
    <form action=x.jsp method=get>
    <input name=x><input type=submit></form>
    <%
    String abc = request.getParameter("x");
    out.println(abc);
    %>
    </body>
    
4. โปรแกรมรับค่าจาก method post และการใช้คำสั่ง if
    <body>
    <form action=testofif.jsp method=post>
    <input name=x><input type=submit></form>
    <%
    String abc = request.getParameter("x");
    if (abc == null)
      out.println("welcome");
    else
      out.println(abc);
    out.println(new java.util.Date());
    %>
    </body>
    
5. การอ่านข้อมูลจาก .mdb ผมทดสอบใน win98 ก็ไม่ได้ลงโปรแกรมอะไรเพิ่ม นอกจาก java + tomcat โดยใช้ข้อมูลจาก .mdb ใน ODBC
    <body>
    <%@ page import="java.sql.*" %>
    <%
      Connection connection;
      Statement statement;
      String sourceURL = "jdbc:odbc:empl";
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      connection = DriverManager.getConnection(sourceURL);
      statement = connection.createStatement();
      String sql = "select * from empl";
      ResultSet myresult = statement.executeQuery(sql);
      while (myresult.next()) {	
        out.println(myresult.getString("emplid") + "<br>");
      }					
      myresult.close();
    %>
    </body>
    
6. การอ่านข้อมูลจาก .mdb มาหาผลรวมเงินเดือน โดยใช้ข้อมูลจาก .mdb ใน ODBC
    <body>
    <%@ page
     import="java.sql.*"
     import = "java.lang.*"
    %>
    <%
      Connection connection;
      Statement statement;
      String sourceURL = "jdbc:odbc:empl";
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      connection = DriverManager.getConnection(sourceURL);
      statement = connection.createStatement();
      String sql = "select * from empl";
      ResultSet myresult = statement.executeQuery(sql);
      int sala,tmpsala;
      sala = 0;
      while (myresult.next()) {	
        out.print(myresult.getString("emplid") + "|");
        out.print(myresult.getString("emplname") + "|");
        tmpsala = myresult.getInt("emplsalary");
        out.println(tmpsala +"<br>");
        sala = sala + tmpsala;
      }					
     out.println(sala);
      myresult.close();
    %>
    </body>
    
7. อ่านข้อมูลมาพิมพ์ และแสดงภาษาไทยได้ บรรทัดสำคัญคือบรรทัดแรก จะเปลี่ยนจาก Tis-620 เป็น Windows-874 ก็ได้
ถ้าไม่มีบรรทัดแรก ภาษาไทยจะกลายเป็น ?????? ??????????????
    <%@ page contentType="text/html; charset=tis-620" %>
    <%@ page import="java.sql.*" %>
    <body>
    <%
      Connection connection;
      Statement statement;
      String sourceURL = "jdbc:odbc:empl";
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      connection = DriverManager.getConnection(sourceURL);
      statement = connection.createStatement();
      String sql = "select * from empl";
      ResultSet myresult = statement.executeQuery(sql);
      while (myresult.next()) {	
        out.println(myresult.getString("emplname") + "<br>");
      }					
      myresult.close();
    %>
    </body>
    
8. การอ่านข้อมูลจาก .mdb ผ่าน odbc โดยตรวจสอบความผิดพลาดขณะประมวลผล
    <body>
    <%@ page import="java.sql.*" %>
    <%
      Connection connection;
      Statement statement;
      String sourceURL = "jdbc:odbc:empl";
      try
      {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        connection = DriverManager.getConnection(sourceURL);
        statement = connection.createStatement();
        String sql = "select * from empl";
        ResultSet myresult = statement.executeQuery(sql);
        while (myresult.next()) {	
          out.println(myresult.getString("emplid") + "<br>");
        }					
        myresult.close();
      } catch (SQLException e) {
          out.println("Error creating connection or have problem on odbc");
      } catch (ClassNotFoundException e) {
          out.println("Driver of Database not found");
      }
    %>
    </body>
    
9. การเพิ่มข้อมูลที่เป็นตัวเลขเข้าฐานข้อมูลไม่มีปัญหา แต่ถ้าเพิ่มภาษาไทย ต้องมีการเปลี่ยนมาตรฐานของภาษานิดหน่อย
ตัวอย่างนี้เปลี่ยนเฉพาะ ename ที่รับมา แล้วส่งให้ sql นำไปเขียนลงแฟ้ม
    <body>
    <%@ page import="java.sql.*" %>
    <%
      String methodtype = request.getParameter("action");
      if (methodtype == null) {
        out.println(request.getParameter("comment"));
    %>
    <form action=addthainame.jsp method=post>
    employee id : <input name=eid><br>
    employee name : <input name=ename><br>
    employee salary : <input name=esalary><br>
    Add confirmed : <input type=checkbox name=confirm checked>
    <input type=submit name=action value=add>
    </form>
    <%
      } else {
      Connection connection;
      Statement statement;
      String sourceURL = "jdbc:odbc:empl";
      Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
      connection = DriverManager.getConnection(sourceURL);
      statement = connection.createStatement();
      String getname = request.getParameter("ename");
      String thainame = new String(getname.getBytes("ISO8859_1"),"TIS-620");
      String sql = "insert into empl values(";
      sql = sql + request.getParameter("eid") + ",'";
      sql = sql + thainame + "',";
      sql = sql + request.getParameter("esalary") + ")";
      if (request.getParameter("confirm") == null) {
        out.println(sql);
      } else {
        statement.executeUpdate(sql);
        response.sendRedirect("addthainame.jsp?comment=add_completly");
      }
      }
    %>
    </body>
    
10. การอ่านข้อมูลจาก mysql มาแสดงผล
    <%@ page import="java.sql.*" %>
    <%
      Connection connection;
      Statement statement;
      String sourceURL = “jdbc:mysql://localhost/dbempl?user=root&password=thai”;
      Class.forName (“org.gjt.mm.mysql.Driver”);
      connection = DriverManager.getConnection(sourceURL);
      statement = connection.createStatement();
      String sql = "select * from empl";
      ResultSet myresult = statement.executeQuery(sql);
      while (myresult.next()) {	
        out.println(myresult.getString("emplid") + "<br>");
      }					
      myresult.close();
    %>
    
11. การลบข้อมูลใน mysql
    <%@ page import="java.sql.*" %>
    <%
      Connection connection;
      String sourceURL = “jdbc:mysql://localhost/dbempl?user=root&password=thai”;
      Class.forName (“org.gjt.mm.mysql.Driver”);
      connection = DriverManager.getConnection(sourceURL);
      Statement statement = connection.createStatement();
      String sql = "delete * from empl";
      ResultSet myresult = statement.executeQuery(sql);	
      myresult.close();
    %>
    

อ.นราวิชญ์ ความหมั่น และนักศึกษามหาวิทยาลัยเทคโนโลยีราชมงคล ลำปาง
ครั้งที่ผมไปเป็นวิทยากรพิเศษ 23 กันยายน 2548 วิชาอินเทอร์เน็ตฯ

แนะนำเว็บใหม่ : ผลการจัดอันดับ
รักลำปาง : thcity.com : korattown.com : topsiam.com : มหาวิทยาลัยโยนก
ศูนย์สอบ : รวมบทความ : ไอทีในชีวิตประจำวัน : ดาวน์โหลด : yourname@thaiall.com
ติดต่อ ทีมงาน ชาวลำปาง มีฝันเพื่อการศึกษา Tel.08-1992-7223