1. 程式人生 > >JSP總結之六:建立第一個簡單的JSP頁面

JSP總結之六:建立第一個簡單的JSP頁面

  手工建立 1、在Tomcat 6.0的安裝目錄的webapps目錄下新建一個目錄,起名叫myapp。 2、在myapp目錄下新建一個目錄WEB-INF,注意,目錄名稱是區分大小寫的。 3、WEB-INF下新建一個檔案web.xml,內容如下:    <?xml version="1.0" encoding="gb2312"?> <web-app>     <display-name>My Web Application</display-name>     <description>         An application for test.
    </description> </web-app> 4、在myapp下新建一個測試的jsp頁面,檔名為index.jsp,檔案內容如下: <html> <body>     <center>Now time is: <%=new java.util.Date()%></center> </body> </html> 5、重啟Tomcat。 6、開啟瀏覽器,輸入http://localhost:8080/myapp/index.jsp 。 1、 安裝好JDK1.6.0和TOMCAT6.0後,將Eclipse軟體拷貝到本地硬碟的一個資料夾裡。 2、 
安裝Eclipse的外掛MyEclipse。 3、 執行eclipse.exe檔案,開啟後,選擇路徑Window->Preferences,彈出頁面如下:     4、選擇Java->Installed JREs,點選“Add”按鈕,彈出: 5、在“JRE home directory”欄中選擇JDK1.6.0的安裝路徑(如“C:/jdk1.6.0_02 在JRE name 欄中填寫標識名字(如“jdk1.6.0_02 填好後,點選“OK”確定。 6、然後選擇路徑Server-> Installed Runtimes,彈出框如下: 7、選擇Apache Tomcat v6.0,點選“Next”,彈出:
選擇要用的Tomcat和JDK,然後點選“Finish”關閉視窗,退出Preferences視窗。 8、在Eclipse中選擇file->new->project,新建專案。彈出: 9、然後選擇“Dynamic Web Project”,點選“Next”,在按提示填寫專案名(如“test1等內容,最後點選“Finish”,專案新建成功。 10、在新建的專案中,右鍵點選“WebContent”資料夾,選擇新建“JSP”檔案,取名為“index.jsp”。 “index.jsp”的<body></body>中填寫“<center>Now time is: <%=new java.util.Date()%></center>”。 11、在Eclipse的右下欄選擇“Servers”,然後點選中間有白色三角形的綠色執行按鈕。當“Servers”的“State”顯示為“Started”時,執行成功。 12、在瀏覽器的位址列裡輸入“http://localhost:8080/test1/index.jsp”,就可以看到一個顯示時間的JSP頁面了。        NetBeans6.0 Milestone10是目前最好的NetBeans系列軟體。具備了強大的視覺化開發功能,無論是用於Java Swing還是JSP。Sun公司計劃在今年11月正式釋出NetBeans 6。明年11月正式釋出NetBeans 7。由於Eclipse的視覺化開發需要依賴各種外掛,而功能最強大的外掛MyEclipse是商業軟體,MyEclipse和NetBeans功能相當。因此建議使用NetBeans6,因為它是開源的,免費的,使用它是合法的,而且免去了選擇,安裝各種Eclipse外掛的工作。 安裝完NetBeans6後,啟動畫面如下:        啟動完成後,進入主畫面: 選擇File->New Project選單,然後選擇Project的類別為Web,選定Web Application。如下圖: 點選Next後,出現如下畫面: 在Server一欄選擇Add按鈕,然後在下面的對話方塊中 設定好Tomcat的安裝目錄。 按下一步,出現如下畫面: 不選擇任何一個Framework,我們只要建立最簡單的jsp網頁。工程建立完成後,會顯示index.jsp檔案。原始碼如下: <%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%> <%-- The taglib directive below imports the JSTL library. If you uncomment it, you must also add the JSTL library to the project. The Add Library... action on Libraries node in Projects view can be used to add the JSTL 1.1 library. --%> <%-- <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> --%> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"    "http://www.w3.org/TR/html4/loose.dtd"> <html>     <head>         <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">         <title>JSP Page</title>     </head>     <body>     <h1>JSP Page</h1>     <%--     This example uses JSTL, uncomment the taglib directive above.     To test, display the page like this: index.jsp?sayHello=true&name=Murphy     --%>     <%--     <c:if test="${param.sayHello}">         <!-- Let's welcome the user ${param.name} -->         Hello ${param.name}!     </c:if>     --%>     </body> </html> 現在執行這個工程,工程會自動編譯,並且部署到tomcat中,然後啟動瀏覽器,看到如下畫面: