Thursday, October 4, 2012

Servlet และ JSP ใครสนใจเอาไปอ่านได้เลย

,

Servlet และ JSP ใครสนใจเอาไปอ่านได้เลย

06 พ.ค.
หลาย ปีมาแล้ว นั่งเขียนเอกสารการสอนอยู่ชุดหนึ่ง หลังจากที่พอมีทักษะการด้านการใช้งานภาษาจาวาเรียบร้อยแล้ว ก็หันมาสนใจในการสร้างโปรแกรม ที่ทำงานในส่วนเซิร์ฟเวอร์บ้าง ซึ่งตามสภาพแวดล้อมการทำงาน ของภาษาจาวา ก็คือการสร้างรูปแบบการประมวลผล ที่เรียกว่า จาวาเซิร์ฟเล็ต (Java Servlet) เรียกย่อๆไม่เปลืองคำพูดว่า เซิร์ฟเล็ต และเจเอสพี (JSP: Java Server Page) ทั้งสองแบบเป็นการประมวลผลโปรแกรม บนเว็บเซิร์ฟเวอร์ แล้วส่งผลลัพธ์กลับสู่ผู้ใช้ผ่านอินเตอร์เน็ตบนพื้นฐานของเว็บ โดยใช้โปรโตคอลการสื่อสารมาตรฐาน HTTP แม้ว่าสภาพการทำงานและการใช้งานจะเหมือนกัน แต่ทั้งสองก็ต่างกันที่เซิร์ฟเล็ตเป็นเหมือนโมดูลที่ต้องเขียนด้วยคำสั่ง ภาษาจาวา และคอมไฟล์เพื่อให้ได้เป็นโมดูล (โปรแกรมย่อย)มาก่อน ที่จะนำไปติดตั้งบนเว็บเซิร์ฟเวอร์ ส่วนเจเอสพี เป็นสคริปต์ ที่สามารถคอมไฟล์ และทำงานด้วยกลไลของเว็บ ความสะดวกและการนำไปใช้แตกต่างกันไปตามความเหมาะสม ผมได้รับเชิญให้ไปสอนในองค์กรแห่งหนึ่ง จึงเกิดเอกสารชุดนี้ขึ้นมา จริงๆแล้วตั้งใจจะเอาไปตีพิมพ์เป็นหนังสือ แต่ก็มีปัญหาเรื่องช่วงเวลาการออกจำหน่าย ซึ่งถือเป็นเหตุผลทางการตลาดกับสำนักพิมพ์ และเวลาก็ล่วงเลยมานาน เก็บไว้ในเครื่องคอมพิวเตอร์จนลืม (คิดดูซิว่านานขนาดไหน และลืมไปได้อย่างไร) ดังนั้นเพื่อไม่ให้เป็นแค่สิ่งที่อยู่ในคอมพิวเตอร์ เลยขออนุญาตินำมาเผยแพร่ ให้ไปดาวน์โหลด และให้ไปอ่านกันได้ฟรีๆเลยครับ ผิดถูกประการใดขอนอมรับไว้แต่ผู้เดียว หากนำไปใช้งานอ้างเครดิตมาให้ผมซักนิดก็ดีนะครับ ถึงผมจะไม่ได้ยินก็ตาม 555 อย่างน้อยสิ่งที่นำมาเผยแพร่ น่าจะเป็นประโยชน์ ต่อผู้ที่สนใจในเทคโนโลยีตัวนี้ และผมก็พิสูจน์มาแล้วในการนำมาพัฒนาระบบ gROOM ของหน่วยงานที่ทำอยู่ (ดู gROOM ) เชิญดาวน์โหลดได้ตามอัธยาศัยเลยนะครับ
บทที่ 1 การประมวลผลรูปแบบต่างๆบนระบบคอมพิวเตอร์
บทที่ 2 เวิร์ด ไวด์ เว็บ
บทที่ 3 HTML, CSS, XML
บทที่ 4 เตรียมสภาพแวดล้อมสำหรับการพัฒนาระบบ
บทที่ 5 Java Servlet เบื้องต้น
บทที่ 6 Handle Request and Response
บทที่ 7 Cookie and Session
บทที่ 8 JSP (Java Server Page)
บทที่ 9 Java Bean and Tag Libraries
บทที่ 10 การใช้งาน JSP ติดต่อกับระบบฐานข้อมูล
บทที่ 11-1 ร้านค้าออนไลน์ด้วยเทคโนยีจาวา ตอน 1
บทที่ 11-2 ร้านค้าออนไลน์ด้วยเทคโนโลยีจาวา ตอน 2
ปล. ในเอกสารเว็บไซต์ JavaCentrix.com และอีเมล์ถูกปิดไปนานแล้วนะครับ

Java Servlet

,

Java Servlet

From Wikipedia, the free encyclopedia
Jump to: navigation, search
Life of a JSP file.
A Servlet is a Java programming language class used to extend the capabilities of servers that can be accessed by a host application via a request-response programming model. Although servlets can respond to any type of request, they are commonly used to extend the applications hosted by web servers. Thus, it can be thought of as a Java Applet that runs on a server instead of a web browser.[1]

Contents

Introduction

A Servlet is a Java-based server-side web technology. As the name implies, it serves a client request and receives a response from the server. Technically speaking, a Servlet is a Java class in Java EE that conforms to the Java Servlet API, a protocol by which a Java class may respond to requests. They are not tied to a specific client–server protocol, but are most often used with the HTTP protocol. Therefore, the word "Servlet" is often used in the meaning of "HTTP Servlet".[2] Thus, a software developer may use a servlet to add dynamic content to a web server using the Java platform. The generated content is commonly HTML, but may be other data such as XML. Servlets are the Java counterpart to non-Java dynamic Web content technologies such as PHP and ASP.NET. Servlets can maintain state in session variables across many server transactions by using HTTP cookies, or URL rewriting.
To deploy and run a Servlet, a web container must be used. A web container (also known as a Servlet container) is essentially the component of a web server that interacts with the Servlets. The web container is responsible for managing the lifecycle of Servlets, mapping a URL to a particular Servlet and ensuring that the URL requester has the correct access rights.
The Servlet API, contained in the Java package hierarchy javax.servlet, defines the expected interactions of the web container and a servlet.[2]
A Servlet is an object that receives a request and generates a response based on that request. The basic Servlet package defines Java objects to represent Servlet requests and responses, as well as objects to reflect the Servlet's configuration parameters and execution environment. The package javax.servlet.http defines HTTP-specific subclasses of the generic servlet elements, including session management objects that track multiple requests and responses between the web server and a client. Servlets may be packaged in a WAR file as a web application.
Servlets can be generated automatically from JavaServer Pages (JSP) by the JavaServer Pages compiler. The difference between Servlets and JSP is that Servlets typically embed HTML inside Java code, while JSPs embed Java code in HTML. While the direct usage of Servlets to generate HTML (as shown in the example below) has become rare, the higher level MVC web framework in Java EE (JSF) still explicitly uses the Servlet technology for the low level request/response handling via the FacesServlet. A somewhat older usage is to use Servlets in conjunction with JSPs in a pattern called "Model 2", which is a flavor of the model–view–controller pattern..

History

The complete Servlet specification was created by Sun Microsystems, with version 1.0 finalized in June 1997. Starting with version 2.3, the Servlet specification was developed under the Java Community Process. JSR 53 defined both the Servlet 2.3 and JavaServer Page 1.2 specifications. JSR 154 specifies the Servlet 2.4 and 2.5 specifications. As of March 26, 2010, the current version of the Servlet specification is 3.0.
In his blog on java.net, Sun veteran and GlassFish lead Jim Driscoll details the history of Servlet technology. James Gosling first thought of Servlets in the early days of Java, but the concept did not become a product until Sun shipped the Java Web Server[clarify] product. This was before what is now the Java Platform, Enterprise Edition was made into a specification.
Servlet API history
Servlet API version Released Platform Important Changes
Servlet 3.0 December 2009 JavaEE 6, JavaSE 6 Pluggability, Ease of development, Async Servlet, Security, File Uploading
Servlet 2.5 September 2005 JavaEE 5, JavaSE 5 Requires JavaSE 5, supports annotation
Servlet 2.4 November 2003 J2EE 1.4, J2SE 1.3 web.xml uses XML Schema
Servlet 2.3 August 2001 J2EE 1.3, J2SE 1.2 Addition of Filter
Servlet 2.2 August 1999 J2EE 1.2, J2SE 1.2 Becomes part of J2EE, introduced independent web applications in .war files
Servlet 2.1 November 1998 Unspecified First official specification, added RequestDispatcher, ServletContext
Servlet 2.0
JDK 1.1 Part of Java Servlet Development Kit 2.0
Servlet 1.0 June 1997

Advantages over CGI

The advantages of using Servlets are their fast performance and ease of use combined with more power over traditional CGI (Common Gateway Interface). Traditional CGI scripts written in Java have a number of disadvantages when it comes to performance:
  • When an HTTP request is made, a new process is created for each call of the CGI script. This overhead of process creation can be very system-intensive, especially when the script does relatively fast operations. Thus, process creation will take more time than CGI script execution. Java Servlets solve this, as a Servlet is not a separate process. Each request to be handled by a Servlet is handled by a separate Java thread within the web server process, omitting separate process forking by the HTTP daemon.
  • Simultaneous CGI request causes the CGI script to be copied and loaded into memory as many times as there are requests. However, with Servlets, there are the same amount of threads as requests, but there will only be one copy of the Servlet class created in memory that stays there also between requests.
  • Only a single instance answers all requests concurrently. This reduces memory usage and makes the management of persistent data easy.
  • A Servlet can be run by a Servlet container in a restrictive environment, called a sandbox. This is similar to an applet that runs in the sandbox of the web browser. This makes a restrictive use of potentially harmful Servlets possible.[2]

Life cycle of a Servlet

  • During initialization stage of the Servlet life cycle, the web container initializes the Servlet instance by calling the init() method, passing an object implementing the ServletConfig interface. This configuration object allows the Servlet to access name-value initialization parameters from the web application.
  • After initialization, the Servlet can service client requests. Each request is serviced in its own separate thread. The web container calls the service() method of the Servlet for every request. The service() method determines the kind of request being made and dispatches it to an appropriate method to handle the request. The developer of the Servlet must provide an implementation for these methods. If a request for a method that is not implemented by the Servlet is made, the method of the parent class is called, typically resulting in an error being returned to the requester.
  • Finally, the web container calls the destroy() method that takes the Servlet out of service. The destroy() method, like init(), is called only once in the lifecycle of a Servlet.
Therefore, three methods are central to the life cycle of a Servlet. These are init(), service(), and destroy(). They are implemented by every Servlet and are invoked at specific times by the server. The following is a typical user scenario of these methods.
  1. Assume that a user enters a URL to a web browser.
    • The browser then generates an HTTP request for this URL.
    • This request is then sent to the appropriate server.
  2. The HTTP request is received by the web server and forwarded to the Servlet container.
    • The Servlet container maps this request to a particular Servlet.
    • The Servlet is dynamically retrieved and loaded into the address space of the Servlet container.
  3. The Servlet container invokes the init() method of the Servlet.
    • This method is invoked only when the Servlet is first loaded into memory.
    • It is possible to pass initialization parameters to the Servlet so it may configure itself.
  4. The Servlet container invokes the service() method of the Servlet.
    • This method is called to process the HTTP request.
    • You will see that it is possible for the Servlet to read data that has been provided in the HTTP request.
    • It may also formulate an HTTP response for the client.
  5. The Servlet remains in the Servlet container’s address space and is available to process any other HTTP requests received from clients.
    • The service() method is called for each HTTP request.
  6. The Servlet container may, at some point, decide to unload the Servlet from its memory.
    • The algorithms by which this decision is made are specific to each Servlet container.
  7. The Servlet container calls the Servlet's destroy() method to relinquish any resources such as file handles that are allocated for the Servlet; important data may be saved to a persistent store.
  8. The memory allocated for the Servlet and its objects can then be garbage collected.

Example

The following example Servlet prints how many times its service() method was called.
Note that HttpServlet is a subclass of GenericServlet, an implementation of the Servlet interface.
The service() method of HttpServlet class dispatches requests to the methods doGet(), doPost(), doPut(), doDelete(), and so on; according to the HTTP request. In the example below method service() is overridden and does not distinguish which HTTP request method it serves.
import java.io.IOException;
 
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
 
public class ServletLifeCycleExample extends HttpServlet {
 
    private int count;
 
    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        getServletContext().log("init() called");
        count=0;
    }
 
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        getServletContext().log("service() called");
        count++;
        response.getWriter().write("Incrementing the count: Count = "+count);
    }
 
    @Override
    public void destroy() {
        getServletContext().log("destroy() called");
    }   
 
}

Usage

Servlets are most often used to
  • process or store data that was submitted from an HTML form
  • provide dynamic content such as the results of a database query
  • manage state information that does not exist in the stateless HTTP protocol, such as filling the articles into the shopping cart of the appropriate customer.

See also

เล่นกับ Get Request และ Post Request ใน JSP

,
บทความนี้จะพูดถึงการรับส่งข้อมูลระหว่าง Page ใน JSP ด้วย get และ post
การส่งข้อมูลแบบ get จะสังเกตง่ายๆคือค่าของตัวแปรแปะมากับ URL ส่วนการ Post จะไม่เห็นรวมอยู่ใน Url เหมือนแต่จะนิยมใช้ในการส่งค่าผ่าน form
การรับส่งค่าแบบ GET

จากรูปจะเห็นได้ว่ามีการส่งข้อมูลไปยังหน้าเพจ welcome1.jsp ซึ่ง link แรก จำทำการส่งตัวแปร mode โดยตัวแปร mode เก็บค่า 1 และ link ที่สองทำการส่งค่าตัวแปร 2 ตัว คือ mode และ text

จากรูปข้างต้นเป็นการรับค่าแบบ get เพื่อนำมาใช้ใน code ซึ่งตัวอย่างจะใช้ request.getParameter(String ชื่อตัวแปร) ในการดึงค่าที่ถูกส่งมากับตัวแปรแบบ GET
การรับส่งค่าแบบ POST
การส่งข้อมูลแบบ POST จะไม่เห็นค่าและชื่อตัวแปรใน address bar ของ Browser ซึ่งนิยมใช้ในการส่งค่าผ่าน form ต่าง

จากรูปเป็นการส่งข้อมูลแบบ Post และการรับข้อมูลแบบมาใช้งาน โดยจะสังเกตเห็นได้ว่าการดึงค่าจากตัวแปรที่ส่งมาใช้คำสั่งเดียวกันคือ request.getParameter(..);
ซึ่งทำให้สรุปง่ายว่าแม้ว่าจะส่งข้อมูลแบบ Post หรือ Get การดึงค่าในตัวแปรก็ไม่แตกต่างกัน
ปล.เนื้อหาในบทความนี้เป็นการจดบันทึกส่วนตัวของผมซึ่งปล่อยเป็นสถานะใคร ใช้วิจารณญาณในการอ่านนะครับ ถ้าส่วนไหนถูกผิดอย่างไรก็ขอคำแนะนติชมและชี้แนะด้วยนะครับ

Servlet คืออะไร ??

,
                 หลายคนอาจจะคุ้นหูคุ้นตากับ html php asp มากกว่า servlet เพราะส่วนมากแล้ว servlet นั้นจะไม่ค่อยได้แสดงตัวตนออกมาให้ผู้คนได้เห็นทางหน้าเว็บบราวเซอร์มากนัก
เนื่องจาก servlet นั้นจะทำงานในส่วนของ business logic ซะมากกว่า ถ้าเทียบกับ model MVC  servlet ก็จะเป็นส่วนของการ controller นั่นเอง
แล้วทำไมเราถึงต้องใช้ servlet กันล่ะ ? มันมีดีอะไร มาดูกัน…
เมื่อก่อนนั้น การทำงาน business logic นั้น เราจะใช้ CGI เป็นตัวที่คอยทำงาน แต่ทว่า หากเราใช้ CGI นั้น เมื่อมี request เข้ามามากกว่า 1 request cgi จะสร้าง process ขึ้นมาใหม่เสมอ
แล้วลองคิดดูว่า หากเป็นเว็บที่มีคนเข้าเป็นแสนเป็นล้านคน อย่าง google.com ถ้ายังใช้ cgi อยู่ จะเป็นอย่างไร คงต้องใช้ cpu 1024 core กันเลยทีเดียว
แต่เดี๋ยวก่อนเราทางเลือกใหม่มาเสนอด้วย…ab shaper เครื่องนี้  ไม่ใช่และ !!!
servlet !! เพราะ servlet นั้นจะทำงานด้วย thread ซึ่งจะถูกจัดการด้วย JVM และ webserver ทำให้ server สร้าง object ของ servlet เพียง object เดียว แต่ทุกๆ request เข้ามาใช้ร่วมกันได้
servlet

แล้วเราสามารถเอา servlet มาทำงานอะไรได้บ้าง ?  แน่นอน มันแสดง  html ได้แต่เราต้องพิม java code เองอะนะ ทำไปทำไม – -”  สู้เราเขียน html เอายังง่ายซะกว่า
ส่วนที่ servlet ทำส่วนใหญ่ ก็คือ controller ดังที่กล่าวมาแล้ว เช่น การติดต่อ database ,การจัดการ session  เพื่อให้ได้มาซึ่ง dynamic web นั่นเอง

Apache Tomcat คืออะไร

,

Apache Tomcat คืออะไร

Apache Tomcat เป็น web server ที่พัฒนาโดย Apache Group

Apache Tomcat เป็น software ประเภท open source ที่ถูกดำเนินการโดยเทคโนโลยี Java Servlet และ JavaServer Pages

Apache Tomcat เป็น web server ที่รองรับการทำงานของ jsp/servlet

Apache Tomcat ถูกใช้อย่างแพร่หลาย ในการพัฒนาเว็บแอพพลิเคชันโดย jsp/servlet

Apache Tomcat ได้พัฒนามาตั้งแต่ปี 1999

Apache Tomcat มีส่วนประกอบตามรูปด้านล่าง


- bin
โฟลเดอร์นี้จะใช้สำหรับ start/shutdown script และมีไฟล์ที่มีประโยชน์อื่น ๆ เช่น startup shutdown catalina

- conf
โฟลเดอร์นี้จะใช้สำหรับ ตั้งค่าต่าง ๆ ของโปรแกรม เช่น web.xml server.xml

- lib
โฟลเดอร์นี้จะใช้สำหรับ เก็บ Jar files ต่าง ๆ และใช้สำหรับ starting และ stopping Tomcat เช่น servlet-api.jar tomcat-api.jar

- logs
โฟลเดอร์นี้จะใช้สำหรับ เก็บ log files ต่าง ๆ

- webapps
โฟลเดอร์นี้จะใช้สำหรับ เก็บโปรเจ็คที่เราได้สร้างขึ้นมา

- work
โฟลเดอร์นี้จะใช้สำหรับ โปรแกรม เอาไว้วางไฟล์ intermediate files(เช่น compiled JSP files) ในระหว่างการทำงาน ซึ่งถ้าลบโฟลเดอร์นี้ออก ในระหว่างแปรโปรแกรมกำลังทำงาน จะไม่สามารถทำงานสคิป jsp ได้

Apache Tomcat สามารถดาวน์โหลดได้ฟรี ที่ http://tomcat.apache.org

Saturday, August 4, 2012

ติดตั้ง tomcat 6 ร่วมกับ apache อย่างไรครับ

,
คือ จะเขียนสคริปต์ jsp และ php อะครับ
ต้องใช้ทั้ง apache และ tomcat จะติดตั้ง อย่างไร ครับ
แบบง่ายๆ apt-get เลยได้ไหม
เพราะเห็นตามเว็บ เจอแต่ ยากๆ

 ผมเคยเขียนเก็บไว้บน google doc ก็เลยเอามา post ให้ครับ (ของผมใช้ java5 กับ tomcat5 ถ้าจะใช้ tomcat6 ให้ลงเป็น java6 นะครับ)


1. upload this file to application server

jakarta-tomcat-5.0.28.tar.gz

jdk-1_5_0_17-linux-i586.bin


2. make working directory

#mkdir /home/opt

#mv  jakarta-tomcat-5.0.28.tar.gz  jdk-1_5_0_17-linux-i586.bin  /home/opt


3. install j2sdk

#./jdk-1_5_0_17-linux-i586.bin

result:   jdk1.5.0_17   --->   directory


4. extract tomcat tarball

#tar -xvf  jakarta-tomcat-5.0.28.tar.gz

result:   jakarta-tomcat-5.0.28   --->  directory


5. setup program for start at boot time

#cp  /home/opt/jakarta-tomcat-5.0.28/bin/catalina.sh  /etc/init.d

then, add this line


# OS specific support.  $var _must_ be set to either true or false.

JAVA_HOME=/home/opt/jdk1.5.0_17
CLASSPATH=/home/opt/jdk1.5.0_17/jre/lib/rt.jar
CATALINA_HOME=/home/opt/jakarta-tomcat-5.0.28
PATH=$PATH:/home/opt/jdk1.5.0_17/bin
export JAVA_HOME
export CLASSPATH
export CATALINA_HOME
export PATH


6. edit global environment

#pico /etc/profile

then, add this lilne


JAVA_HOME=/home/opt/jdk1.5.0_17
CLASSPATH=/home/opt/jdk1.5.0_17/jre/lib/rt.jar
CATALINA_HOME=/home/opt/jakarta-tomcat-5.0.28
PATH=$PATH:/home/opt/jdk1.5.0_17/bin
export JAVA_HOME
export CLASSPATH
export CATALINA_HOME
export PATH

umask 022     --->   last line (old configuration)


7. startup test

#/etc/init.d/catalina.sh start

result:

Using CATALINA_BASE:   /home/opt/jakarta-tomcat-5.0.28
Using CATALINA_HOME:   /home/opt/jakarta-tomcat-5.0.28
Using CATALINA_TMPDIR: /home/opt/jakarta-tomcat-5.0.28/temp
Using JAVA_HOME:       /home/opt/jdk1.5.0_17

and

root@catserver:~# netstat -lnt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:25              0.0.0.0:*               LISTEN
tcp6       0      0 127.0.0.1:8005          :::*                    LISTEN
tcp6       0      0 :::8009                 :::*                    LISTEN
tcp6       0      0 :::8080                 :::*                    LISTEN        --->      tomcat already start
tcp6       0      0 :::22                   :::*                    LISTEN


8. open web browser and type

http://your server ip address:8080


9. configure tomcat user and password for web management

#pico  /home/opt/jakarta-tomcat-5.0.28/conf/tomcat-users.xml

result:

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="tomcat"/>
  <role rolename="role1"/>
  <user username="tomcat" password="tomcat" roles="tomcat"/>
  <user username="both" password="tomcat" roles="tomcat,role1"/>
  <user username="role1" password="tomcat" roles="role1"/>
</tomcat-users>

edit:

<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="manager"/>
  <role rolename="admin"/>
  <user username="manager" password="hahaha" roles="manager"/>
  <user username="admin" password="hahaha" roles="admin"/>
</tomcat-users>

*now! you can login with manager and admin account


10. change permission for security

 #chmod 600 /home/opt/jakarta-tomcat-5.0.28/conf/tomcat-users.xml


11. restart tomcat (re-configure)

#/etc/init.d/catalina.sh stop

#/etc/init.d/catalina.sh start


12. login with admin and manager account


finish

*จริงๆ apt-get เอาก็ได้นะครับ ทั้ง java6 , tomcat6 แต่ต้องตั้งค่า environment ให้ถูกครับ และถ้าผมจำไม่ผิดนะครับ ubuntu server 8.10 ตอนที่ format เครื่องเพื่อลง OS มันจะมีให้เลือกนะครับว่าจะติดตั้ง server แบบไหน ผมว่าผมเคยเห็นว่ามี tomcat ให้เลือกนะครับ ถ้าเลือกตรงนั้น พอ format เสร็จก็จะได้ tomcat server แบบสมบูรณ์ไปเลย

ขอบคุณครับ

JavaServer Pages (JSP) Part I

,
JavaServer Pages (JSP) เป็น web-scripting เทคโนโลยีคล้ายกับ Netscape server-side JavaScript (SSJS) หรือ Microsoft Active Server Pages (ASP) แต่ผิดกันตรงที่หัวใจของ JSP คือ Java ซึ่งเป็นภาษาที่คอนเซ็ปหลักอยู่ที่ออฟเจ็ค (object-oriented style) ซึ่งช่วยทำให้ง่ายต่อการพัฒนาในโปรเจคใหญ่ ๆ ตลอดจนสามารถนำส่วนประกอบต่าง ๆ กลับมาใช้ได้อีก (software reusable) จุดเด่นที่สำคัญของ JSP คือสามารถทำงานได้โดยไม่ขึ้นอยู่กับผู้ผลิตซอฟแวร์รายใดรายหนึ่งโดยเฉพาะ ซึ่งโดยทั่วไปเทคโนโลยีต่าง ๆ มักจะออกมาในลักษณะของผลิตภัณฑ์จากบริษัทผู้ผลิตแห่งใดแห่งหนึ่ง แต่ JSP ใช้ลักษณะของ specification ซึ่งกำหนดโดย Sun Microsystems ดังนั้นผู้ผลิตซอฟแวร์จึงสามารถอ้างอิง specification ที่กำหนดขึ้น ผลิต JSP Container (ตัวที่ใช้ในการรัน JSP) ขึ้นมาใช้กับแฟลตฟอร์มใดก็ได้ 
JSP Containers
JSP Pages (ไฟล์ที่เขียนขึ้นโดยใช้ JSP script และลงท้ายด้วย .jsp) จะถูกรันโดย JSP Container ซึ่งมักจะเป็นส่วนประกอบที่อยู่ใน Webserver หรือ เป็นตัวแอทออนใน Application Server.  โดยทั่วไป JSP Container จะเป็นตัวรับ request จาก client ส่งผ่านไปยัง JSP Page และส่งค่าที่ได้จากการประมวลผลโดย JSP Page กลับไปยัง client.  JSP Container ที่ใช้กันอยู่มีมาจากหลายค่าย ยกตัวอย่างเช่น GNU JSP, Expresso, Tomcat Jakarta, Resin, Weblogic เป็นต้น 
JSP Container ที่เราจะใช้กันคือ Jasper ซึ่งเป็น JSP Container ที่อยู่ใน Tomcat Servlet Engine จากค่าย apache (www.apache.org) ซึ่งทาง Sun ใช้เป็นตัวอ้างอิงในวงการ JSP โดยในอนาคต Tomcat จะเป็นตัวที่ใช้ในการรัน Servlet แทน Jserv ซึ่งใช้กันอยู่ในปัจจุบัน
การติดตั้ง Tomcat
1) Java Development Kit
โปรแกรม Tomcat ถูกเขียนขึ้นโดยใช้จาว่า ซึ่งจะรันกับ Java Development Kit (JDK) เวอร์ชัน 1.1 และ 1.2 ดังนั้นก่อนจะรัน เราจะต้องติดตั้ง JDK1.xxx ตามแฟลตฟอร์มที่เราใช้เสียก่อน
สำหรับวินโดว์และยูนิกส์ให้ไปดาวโหลดที่ http://java.sun.com/jdk/. (บางครั้ง JDK อาจถูกเรียกว่า SDK)
สำหรับ Linux ให้ไปที่ http://www.blackdown.org/.
สมมุติว่าเราใช้วินโดว์ และติดตั้ง JDK1.2.2 ไว้ที่ D:\jdk1.2.2 ให้เซ็ต PATH=d:\jdk1.2.2\bin\ โดยใช้คำสั่งใน Dos (set PATH=d:\jdk1.2.2\bin\;) หรือ คลิกขวาที่ My Computer แล้วเลือกที่ Environment แทป
ที่เราต้องเซ็ต PATH เพราะ Tomcat จะรันโดยใช้คำสั่ง java.exe ซึ่งอยู่ใน bin ไดเรคทรอรี่ของ JDK
2) Jakarta Tomcat
ทาง apache ยังคงทำการพัฒนา Tomcat ให้มีประสิทธิภาพมากเพื่อใช้เป็นมาตราฐานในอนาคต ดังนั้นถ้าเข้าไปที่เวปไซด์ในส่วนของดาวโหลด เราจะเห็นตัวเลือกในการดาวโหลดได้คร่าว ๆ เป็นสองแบบ คือ แบบ souce files ที่ต้องมาคอมไพล์เองโดยใช้เครื่องมือช่วยที่เรียกว่า Ant ซึ่งเป็นส่วนที่นักพัฒนานิยมดาวโหลดกัน กับแบบที่คอมไพล์แล้ว (binary) พร้อมที่จะรันซึ่งเหมาะสำหรับคนที่อยากนำ Tomcat มาใช้งานอย่างเดียว และเป็นอย่างที่เราจะใช้ ดังนั้น ให้ไปที่ http://jakarta.apache.org/downloads/binindex.html แล้วทำการติดตั้ง
สมมุติว่าเราใส่ Tomcat ไว้ที่ D:\jakarta-tomcat ให้เปิด Command Prompt (MSDos) จาก Application Menu หรือใช้ start->run->type cmd แล้วไปที่ไดเร็คทรอรี่ที่เป็น bin ซึ่งในตัวอย่างก็คือ D:\jakarta-tomcat\bin แล้วรันโดยใช้คำสั่ง D:\jakarta-tomcat\bin> startup หลังจากนั้นให้เปิด browser เพื่อเช็คว่า Tomcat ทำงานถูกต้องหรือเปล่า โดยไปที่ http://127.0.0.1:8080/examples/jsp/ 
Note: http://127.0.0.1:8080 คือ ให้ browser ส่ง request ไปที่เครื่องตัวเอง (localhost) ที่พอร์ต 8080 (Tomcat default port)
ถ้าจะปิด Tomcat ก็ให้พิมพ์ D:\jakarta-tomcat\bin\> shutdown ลงไปแทน
Web Applications 
ก่อนที่เราจะเริ่มเขียน JSP สิ่งหนึ่งที่ควรจะทำความเข้าใจคือโครงสร้างของไดเรคทรอรี่ต่าง ๆ สำหรับไฟล์ของเราใน Tomcat ในสมัยก่อนเราไม่ค่อยได้ให้ความสนใจกับการจัดเรียงไฟล์ต่าง ๆ ซักเท่าไหร่ ข้อเสียที่เห็นได้ชัดคือการยากในการจัดเก็บ และการยากในการย้ายไฟล์จากเซฟเวอร์หนึ่ง ไปยังอีกเซฟเวอร์หนึ่ง เพราะโดยทั่วไปแต่ละ webserver จะมีลักษณะการจัดเก็บไฟล์ไม่เหมือนกัน แต่เมื่อ Java Servlet Specification เวอร์ชั่น 2.2 ออกมา เซฟเวอร์ที่ใช้รัน Servlet v2.2 จะถูกบังคับให้มีต้องการสนันสนุนการจัดเก็บไฟล์แบบหนึ่ง ที่เรียกว่า Web Applicaton 
web application  คือกลุ่มของไดเรคทรอรี่และไฟล์ที่อาจจะประกอบด้วย html, jsp, servlet, javabean และอื่น ๆ ซึ่งอยู่รวมกันในลักษณะของระบบไฟล์ (file system) หรือถูกอัดอยู่ในไฟล์เดียวกันโดยจะเรียกว่า Web Archive (.war) ไฟล์ ซึ่งมีประโยชน์ในการโยกย้ายและติดตั้งจากเซฟเวอร์หนึ่งไปยังอีกเซฟเวอร์หนึ่ง
Tomcat เป็น Servlet Engine ที่สนันสนุน Java Servlet API v.2.2 ดังนั้นไฟล์ JSP ที่เราเขียน จะต้องถูกจัดอยู่ในรูปแบบ Web Application นี้ด้วย
ถ้าใครเคยคลุกคลีกับ webserver จะเห็นว่ามีการใช้ไดเรคทรอรี่ในการจัดกลุ่มของไฟล์ไว้ด้วยกันตามหน้าที่ ซึ่งทาง Tomcat ก็ใช้หลักการเดียวกันในการจัดการ web application หลักการก็คือ แต่ละ web appliction จะถูกจัดให้อยู่ในไดเรคทรอรี่ของตัวเอง ที่เรียกว่า context path ยกตัวอย่างเช่น ถ้า web application ของเราถูกแมปกับ context path ที่ชื่อ webapplication1 เวลาดูจาก URL ก็จะเห็นเป็น http://www.mycompany.com/webapplication1/ เป็นต้น
อย่างที่กล่าวมาข้างต้นว่า web application คือกลุ่มของไดเรคทรอรี่และไฟล์ที่ถูกจัดอยู่รวมกันเป็นชั้น ๆ ดังนั้นถ้าเรามีไฟล์ที่ชื่อ helloworld.html :ซึ่งอยู่บนสุด (top-level) ของไดเรคทรอรี่ใน web application เราก็จะสามารถเรียกออกมาดูได้โดยใช้ URL เท่ากับ http://www.mycompany.com/webapplication1/helloworld.html
ในกรณีที่ไฟล์ไม่ได้อยู่บนสุดของไดเรคทรอรี่ใน web application เราก็ยังสามารถเรียกออกมาได้โดยอ้างอิงไปที่ไดเรคทรอรี่ย่อยนั้น ยกตัวอย่างเช่น ถ้ากลุ่มไฟล์ JSP ถูกเก็บไว้ในไดเรคทรอรี่ถัดลงมาที่ชื่อ jsp เราก็สามารถที่จะเรียกไฟล์มาดูโดยใช้ URL เท่ากับ http://www.mycompany.com/webapplication1/jsp/xxx.jsp 
ส่วนประกอบของ Web Application
web application หนึ่ง ๆ อาจประกอบด้วย
1) Servlet
2) JavaServer Pages
3) Utility Classes
4) ไฟล์ปกติ เช่น HTML, images, sounds 
5) JavaBean
แต่ละอย่างจะถูกเก็บไว้ในไดเรคทรอรี่ต่อไปนี้
1) root directory หรือส่วนบนสุด : ไฟล์ *.html, *.jsp มักจะถูกเก็บไว้ส่วนบนสุดของ web application แต่อาจจะมีไดเรคทรอยีย่อยลงมาจาก root เพื่อเก็บ ไฟล์ images ก็ได้ 
2)  WEB-INF/web.xml : ไฟล์ web.xml เป็นส่วนที่ใช้ในการบรรยายส่วนประกอบต่าง ๆ (Web Application Deployment Descriptor) รวมไปถึงการป้อนค่าเริ่มต้นให้กับโปรแกรมต่าง ๆ ที่บรรจุอยู่ใน web application นี้ ยกตัวอย่างเช่น เราสามารถป้อนค่าเริ่มต้นให้กับ Servlet ได้โดยใส่ค่าเข้าไปใน XML Tag ตรงส่วนที่เรียกว่า <init-param> เป็นต้น
(หาอ่านเพิ่มเติมได้จาก JavaServlet Specification v2.2, http://java.sun.com/products/servlet/)
3) WEB-INF/classes/* : เป็นส่วนที่ใช้ในการเก็บไฟล์ *.class ทั้งหมดที่ใช้ใน web application ซึ่งอาจจะเป็นทั้ง Servlet หรือ ไฟล์ class ธรรมดาที่ไม่ได้อยู่ในรูปของ .jar ก็ได้ ยกตัวอย่างเช่น ถ้าไฟล์ของเราอยู่ใน package ชื่อ com.mycompany.mypackage.MyServlet ไฟล์นี้ก็จะถูกเก็บอยู่ในไดเร็คทรอรี่ WEB-INF/classes/com/mycompany/mypackage/MyServlet.class
(คอนเซ็ปเกี่ยวกับ package สามารถอ่านเพิ่มเติมได้จาก JAVA PACKAGE)
4) WEB-INF/lib/*.jar : ไดเร็คทรอรี่นี้ใช้เก็บไฟล์ .jar ที่เราต้องการใช้สำหรับ web application ซึ่งอาจจะเป็นส่วนที่ได้มาจาก third party หรือเป็นพวก Database driver ก็ได้ ประโยชน์อย่างหนึ่งที่เห็นได้ในการเก็บไลบารี่ต่าง ๆ ไว้ที่ไดเร็คทรอรี่นี้คือ การง่ายต่อการติดตั้ง เหตุผลคือ หลังจาก web application ของเราถูกใส่เข้าไปใน server แล้ว ตัว server จะโหลดไฟล์ที่อยู่ในไดเร็คทรอรี่นี้โดยอัตโนมัติ เราจึงไม่ต้องทำการเซ็ต CLASSPATH ให้ยุ่งยาก
ตัวอย่างง่าย ๆ ของโครงสร้าง web application อาจเป็น
/index.html
/howto.jsp
/feedback.jsp
/images/banner.gif
/images/jumping.gif
/WEB-INF/web.xml
/WEB-INF/lib/jspbean.jar
/WEB-INF/classes/com/mycompany/servlets/MyServlet.class
/WEB-INF/classes/com/mycompany/util/MyUtil.class 
Integration with Tomcat 
การติดตั้ง web application สามารถทำได้ 3 วิธีคือ 
1) นำ web application ที่อยู่ในรูปของกลุ่มไดเรคทรอรี่ (ยังไม่ได้ถูกบีบอัด) ไปใส่ไว้ที่ $TOMCAT_HOME/webapps/.  กรณีนี้ Tomcat จะจัด context path ให้ web application เองโดยอิงจากชื่อ subdirectory ที่บรรจุกลุ่มไดเรคทรอรี่ของ web application ไว้ ยกตัวอย่างเช่น เราใส่ web application ไว้ที่ $TOMCAT_HOME/webapps/myapp/ ดังนั้น context path ที่จะใช้ติดต่อกับ web application นี้ก็คือ myapp วิธีนี้เป็นวิธีที่ง่ายที่สุด และเหมาะสำหรับการพัฒนาก่อนนำไปใช้จริง 
2) นำ web application archive (.war ไฟล์) ไปใส่ไว้ที่ $TOMCAT_HOME/webapps/.  วิธีนี้เหมาะสำหรับการติดตั้ง web application ใน production server ที่ใช้งานจริง โดย Tomcat จะแกะและขยายไฟล์ทั้งหมดที่อยู่ใน .war ไฟล์ออกมาแล้วทำการโหลดเข้าหน่วยความจำ 
3) ในกรณีที่ไดเรคทรอรี่ที่เราพัฒนา web application ไม่ได้อยู่ที่ $TOMCAT_HOME/webapps/ เราก็สามารถบอก Tomcat ให้โหลด web application นี้ได้โดยเข้าไปแก้ที่ $TOMCAT_HOME/conf/server.xml ในส่วนของ <Context> ให้ชี้ไปที่ไดเรคทรอรี่ที่เราเก็บ web application ไว้ เช่น ถ้าเราเก็บ web application ไว้ที่ D:\MyWebApplication\ แล้วอยากให้ชื่อ MyWebApp เป็น context path ในการชี้ไปที่ web application ของเรา เราก็แค่แอท 
<Context path="/MyWebApp" docBase="D:\MyWebApplication\" debug="0" reloadable="true"/> 
ไว้ที่บรรทัดถัดมาจาก Context  path อันสุดท้ายที่มีอยู่แล้ว เป็นต้น 
(สมมุติว่าเราติดตั้ง Tomcat ไว้ที่ ไดเรคทรอรี่ D:\jakarta-tomcat ดังนั้น $TOMCAT_HOME จะเป็น D:\jakarta-tomcat)  My Hello JSP
คอนเซ็ปของ JSP คือการใส่ Servlet code ลงไปใน HTML ไฟล์โดยผ่านทางสคิปต์ที่ขึ้นต้นด้วย <% และลงท้ายด้วย %> ดังตัวอย่างต่อไปนี้
<HTML>
<HEAD>
<TITLE>My Hello JSP</TITLE>
</HEAD>
<BODY>
<H1>Everybody says 
<% String str = "Hello JSP";
     out.println(str); %>
</H1>
</BODY>
</HTML>
อย่างที่กล่าวมาข้างต้น การที่จะรัน JSP ได้ เราจะต้องจัดไฟล์ต่าง ๆ ให้อยู่ใน web application  ดังนั้นถ้าใครอยากรันทดสอบ ก็ให้สร้างไดเรคทรอรี่ขึ้นมาอันหนึ่ง สมมุติว่าชื่อ D:\MyWebApplication\ โดยไดเรคทรอรี่นี้จะเป็น root directory ของ web application ที่เราจะใช้เก็บไฟล์ JSP ทุกไฟล์ที่เราเขียน หลังจากนั้นให้สร้าง subdirectory ที่ชื่อ jsp แล้วก๊อปปี้โค๊ดข้างบนไปใส่ไว้ในไฟล์ที่ชื่อ hellojsp.jsp แล้วเก็บไว้ที่ subdirectory jsp ที่เราเพิ่งสร้างขึ้น ดังรูป

รูปที่ 1 hellojsp.jsp
ถ้าสังเกตดี ๆ จะเห็นอีก subdirectory ชื่อ Web-inf ให้สร้าง subdirectory นี้ขึ้น แล้วใส่ไฟล์ web.xml ซึ่งเป็นไฟล์ที่ใช้บรรยายส่วนประกอบต่าง ๆ (Web Application Deployment Descriptor) ของ web application ของเราเข้าไป ซึ่งถ้าเปิดดู จะไม่เห็นอะไรมากในตอนนี้
หลังจากนั้นให้ไปแก้ $TOMCAT_HOME/conf/server.xml ไฟล์เพื่อลิงค์  Context Path (สมมุติว่าชื่อ MyWebApp) เข้ากับ web application ของเรา พอเสร็จก็ทำการ startup Tomcat, เปิด browser  แล้วพิมพ์  http://127.0.0.1:8080/(your context)/jsp/hellojsp.jsp ผลก็จะออกมาดังรูป 

รูปที่ 2 hellojsp.jsp in action
เกิดอะไรขึ้นบ้าง  ?
ถ้าดูเผิน ๆ จากทาง browser การเรียกไฟล์ JSP คงดูคล้าย ๆ กับการเรียกไฟล์ HTML ธรรมดา ๆ  แต่ความจริงแล้ว  หลังจาก request จาก client ส่งมาถึงเซฟเวอร์, JSP Container จะทำการแปลงไฟล์ JSP ให้กลายเป็น Servlet  source ไฟล์ (ในกรณีที่ JSP ไฟล์ดังกล่าวถูกเรียกเป็นครั้งแรก) ซึ่งไฟล์ Servlet source ที่ได้จะถูกคอมไพล์เป็น .class เพื่อใช้ในการประมวลผล request ของ client แล้วส่งกลับไปให้เซฟเวอร์ในรูปของ outputStream ซึ่งถูกส่งไปที่ client ในท้ายสุด
หลังจากนั้น ถ้า JSP ไฟล์ดังกล่าวถูกเรียกอีก การแปลงไฟล์หรือคอมไพล์จะไม่เกิดขึ้น เพราะ JSP  Container จะใช้ไฟล์ .class ที่เก็บไว้แล้วประมวลผลแทน  ข้อสังเกตคือ การเรียกไฟล์  JSP ครั้งแรกจะรู้สึกว่าช้า แต่ครั้งถัดไป ๆ จะเห็นได้ว่าเร็วขึ้นมาก เพราะได้มีการลดขั้นตอนต่าง ๆ ที่เสียเวลาไป
ใน JSP Specification 1,1 ยังมีการแนะนำให้ JSP Engine รองรับการโหลดไฟล์ JSP ขึ้นมาคอมไพล์ใหม่แบบอันโนมัติ (Reloading) ซึ่งมักจะเกิดขึ้นในกรณีที่ JSP ไฟล์ได้มีแก้ไข แล้วจะต้องมีการคอมไพล์ไฟล์ Servlet ที่กำลังใช้งานอยู่ใหม่

รูปที่  3 การแปลงและคอมไพล์ JSP ไฟล์
จากข้อความที่อธิบายข้างต้น เราสามารถแบ่งระยะเวลาของไฟล์ JSP หนึ่ง ๆ ออกเป็นสองช่วงคือ 
1) Translation Time คือช่วงเวลาที่ JSP ไฟล์ถูกแปลงให้กลายเป็น Servlet ไฟล์และถูกคอมไพล์ให้กลายเป็น .class ไฟล์ ซึ่งจะเกิดก่อนการรับ request จาก client ครั้งแรก
2) Client Request Time คือช่วงเวลาที่ .class ของ JSP ไฟล์ ทำการรับ request จากแต่ละ client แล้วทำการประมวลผล
JSP Translation
จากตัวอย่างไฟล์ hellojsp.jsp เราจะเห็นว่าโค๊ดจะมีแค่สองส่วนคือ ส่วนที่เป็น HTML code และส่วนที่เป็น Java code ซึ่งถูกบรรจุอยู่ใน <%  ... %> 
ในขั้นตอนของการแปลงไฟล์จาก .jsp เป็น Servlet  souce ไฟล์ (.java) ส่วนที่เป็น HTML code จะถูกเปลี่ยนให้อยู่ในรูปที่เทียบเท่ากับ out.print("HTMLCODE");  ไฟล์ hellojsp.jsp อาจถูกแปลงเป็น .java ได้คร่าว ๆ ดังนี้ 
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
public class _hellojsp1_xjsp extends HttpServlet {
  public void service(HttpServletRequest request, HttpServletResponse response) 
                                     throws ServletException, IOException {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("<HTML>"); 
    out.println("<HEAD>");
    out.println("<TITLE>My Hello JSP</TITLE>");
    out.println("</HEAD>");
    out.println("<BODY>");
    out.println("<H1>Everybody says, ");
    // Java code inside <% 
    String str = "Hello JSP";
    out.println(str);
    // End of java code %>
    out.println("</H1>");
    out.println("</BODY>");
    out.println("</HTML>");
  }
}
* เพื่อที่จะเข้าใจหลักการทำงานของ JSP ผู้อ่านควรมีความเข้าใจ Java Servlet เป็นพื้นฐาน ถ้ามีเวลาว่าง ผู้เขียนอยากให้ช่วยอ่าน Java Servlet tutorial ซักเล็กน้อยเสียก่อน 
 

Java Servlet Copyright © 2012 | Design by I Love Linux