wiki:waue/2010/0608

Version 9 (modified by waue, 14 years ago) (diff)

--

jsp + servlet + javabean
教材1

本篇為此書第二章的實做練習

零. 下載資源

1.A 用 virtualbox 虛擬機器

下載 virtualbox 的硬碟檔

  • 此硬碟檔為ubuntu 10.04 版本,已經調整好教材,請用virtualbox 3.2.2 以上版本掛載

1.B 自行下載資源

  1. 下載tomcat 6.5
  1. 下載 eclipse 3.5 linux版

範例一

  • 2.1.1~ 2.1.3 的範例 (p2-1~p2-10)

2.1 建立目錄

已將tomcat 安裝於 /opt/tomcat/ 目錄下

cd /opt/tomcat/webapps/
mkdir -p /opt/tomcat/webapps/servlet1/WEB-INF/classes
mkdir /opt/tomcat/webapps/servlet1/WEB-INF/lib

2.2 第一個程式:HelloServlet?.java

cd /opt/tomcat/webapps/servlet1/WEB-INF/classes
gedit HelloServlet.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("<html>");
    out.println("<head>");
    out.println("<title>Hello Servlet</title>");
    out.println("</head>");
    out.println("<body>");
    out.println("<h1> Hello! " + name + " !</h1>");
    out.println("</body>");                                       
    out.println("</html>");
    out.close();
  }
}

2.3 編譯:HelloServlet?.java檔

cd /opt/tomcat/webapps/servlet1/WEB-INF/classes
javac -classpath /opt/tomcat/lib/servlet-api.jar ./HelloServlet.java 

2.4 製作佈署描述檔:web.xml

cd /opt/tomcat/webapps/servlet1/WEB-INF/
gedit web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
    http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>HelloServlet</servlet-class>
    </servlet>
    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello.do</url-pattern>
    </servlet-mapping>
</web-app>

2.5 啟動web容器:tomcat

cd /opt/tomcat
bin/startup.sh

2.6 用瀏覽器測試

http://localhost:8080/servlet1/hello.do?name=caterpillar

ps: ?name=caterpillar 可以改成你的名子來測試是否成功


練習

如果程式碼的第一行多了package宣告,要如何編譯執行

package tw.org.nchc.opsrc;
#其他相同省略~~~

範例二

  • 2.1.4 的範例 (p2-10 ~ p2-11)

cd /opt/tomcat/webapps
jar cvf ~/servlet1.war servlet1/*
gedit /opt/tomcat/conf/web.xml
<?xml version='1.0' encoding='utf-8'?>
<tomcat-users>
  <role rolename="tomcat"/>
  <role rolename="manager"/>
  <user username="nutchuser" password="nutchez" roles="tomcat,manager"/>
</tomcat-users>

刪除/opt/tomcat/webapps/servlet1/ 整個專案,再重開tomcat

/opt/tomcat/bin/shutdown.sh
/opt/tomcat/bin/startup.sh

先開啟http://localhost:8080/servlet1/hello.do?name=caterpillar

網頁錯誤

http://localhost:8080/manager/html/list

佈署~/servlet1.war檔(圖形介面難以文字描述)

開啟http://localhost:8080/servlet1/hello.do?name=caterpillar

有看到範例一的網頁即完成

練習

,將專案建立、開啟、停止、移除