{{{ #!html
jsp + servlet + javabean
教材1
}}} [[PageOutline]] = 一. 下載資源 = == 1.A 用 virtualbox 虛擬機器 == * [http://secuse.nchc.org.tw/jspservlet/u1004.vdi 下載 virtualbox 的硬碟檔] * 此硬碟檔為ubuntu 10.04 版本,已經調整好教材,請用virtualbox 3.2.2 以上版本掛載 == 1.B 自行下載資源 == 1. [http://secuse.nchc.org.tw/jspservlet/jspservlet%e5%88%9d%e9%9a%8e.pdf 下載 pdf 手冊][[BR]] 2. [http://secuse.nchc.org.tw/jspservlet/tomcat-6.0.26.tar.gz 下載tomcat 6.5] 3. [http://secuse.nchc.org.tw/jspservlet/eclipse-SDK-3.5.2-linux-gtk.tar.gz 下載 eclipse 3.5 linux版] = 二. 實做簡單的 servlet 範例 = 已將tomcat 安裝於 /opt/tomcat/ 目錄下 == 2.1 建立目錄 == {{{ cd /opt/tomcat/webapps/ mkdir -p /opt/tomcat/webapps/FirstServlet/WEB-INF/classes mkdir /opt/tomcat/webapps/FirstServlet/WEB-INF/lib }}} == 2.2 第一個程式:HelloServlet.java == {{{ cd /opt/tomcat/webapps/FirstServlet/WEB-INF/classes gedit HelloServlet.java }}} {{{ #!java import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; public class HelloServlet extends HttpServlet { protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=UTF-8"); PrintWriter out = response.getWriter(); String name = request.getParameter("name"); out.println(""); out.println(""); out.println("Hello Servlet"); out.println(""); out.println(""); out.println("

Hello! " + name + " !

"); out.println(""); out.println(""); out.close(); } } }}} == 2.2 編譯:HelloServlet.java檔 == {{{ cd /opt/tomcat/webapps/FirstServlet/WEB-INF/classes javac -classpath /opt/tomcat/lib/servlet-api.jar ./HelloServlet.java }}} == 2.3 製作佈署描述檔:web.xml == {{{ cd /opt/tomcat/webapps/FirstServlet/WEB-INF/ gedit web.xml }}} {{{ #!xml HelloServlet HelloServlet HelloServlet /hello.do }}} == 2.4 啟動web容器:tomcat == {{{ cd /opt/tomcat bin/startup.sh }}} == 2.5 用瀏覽器測試 == [http://localhost:8080/FirstServlet/hello.do?name=caterpillar] ps: '''?name=caterpillar''' 可以改成你的名子來測試是否成功