本備忘記主要是以 tomcat jasper 2 編譯 jsp 檔案為 servlet 的 java 源檔案.
就像 JBuilder 產生 Servlet 的功能, 有助於 jsp 開者 debug.
開始備忘記:
[1] 安裝 jdk 5
[2] 下載 Tomcat
[3] 安裝 Eclipse 及 相關 plugins
[4] 實例測試
[1] 安裝 jdk 5:
下載 jdk-1_5_0_07-nb-5_0-win-ml.exe
http://java.sun.com/j2se/1.5.0/download-netbeans.html
安裝至 D:\jdk1.5.0_07
新增環境變數 JAVA_HOME=D:\jdk1.5.0_07
D:\jdk1.5.0_07\bin 加入至 PATH 中
D:\jdk1.5.0_07\lib\dt.jar 及 D:\jdk1.5.0_07\lib\tools.jar 加入至 CLASSPATH 中
執行 D:\>java -version
輸出 java version "1.5.0_07" 即安裝成功.
[2] 下載 Tomcat:
下載 apache-tomcat-5.5.20.zip
http://www.eng.lsu.edu/mirrors/apache/tomcat/tomcat-5/v5.5.20/bin/apache-tomcat-5.5.20.zip
解壓縮至 C:\apache-tomcat-5.5.20
[3] 安裝 Eclipse 及 相關 plugins:
安裝 Eclipse:
下載 eclipse-SDK-3.2.1-win32.zip
http://www.eclipse.org/downloads/
http://ftp.jaist.ac.jp/pub/eclipse/eclipse/downloads/drops/R-3.2.1-200609210945/eclipse-SDK-3.2.1-win32.zip
解壓縮至 c:\eclipse_tptp
安裝 Eclipse Callisto plugins:
點擊 c:\eclipse_tptp\eclipse.exe 執行 Eclipse
選擇 Help -> Software Updates -> Find and Install -> Search for new features to install
按 Next 後 點選 Callisto Discovery Site 後 按 Finish
然後選擇最接近的下載點安裝, 然後 隨 C and C++ Developement 外, 全部安裝.
如下圖所示
eclipse_tptp_1.gif
這裡下載安裝使用超過20分鐘. 最好選擇較接近的下載點.
[4] 實例測試:
執行 Eclipse c:\eclipse_tptp\eclipse.exe
切換至 J2EE Perpective
Window -> Open Perpective -> Other ->> J2EE
按 OK
建立測試項目:
File -> New -> Project ->> Web -> Dynamic Web Project 按 Next
Project name : Jasper2Sample
然後按 Finish
右鍵點選 Jasper2Sample -> Properties -> Java Build Path -> Source
按 Add Folder 再按 Create New Folder
Folder name : output_servlet
然後按 Finish 及 OK 如下圖所示
切換至 Library [Tab]
按 Add External JARs
加入
C:\apache-tomcat-5.5.20\common\lib\servlet-api.jar
C:\apache-tomcat-5.5.20\common\lib\jsp-api.jar
C:\apache-tomcat-5.5.20\common\lib\jasper-runtime.jar
然後按 OK
如下圖所示
建立 Jasper Ant Build File:
右鍵點選 Jasper2Sample -> New -> File
File name : build_servlet.xml
然後按 Finish
內容為:
<!--------------------- build_servlet.xml -------------------->
<project name="Webapp Precompilation" default="jspc" basedir=".">
<property name="tomcat.home" value="C:\apache-tomcat-5.5.20" />
<property name="output.servlet" value="output_servlet" />
<property name="webapp.path" value="WebContent" />
<target name="jspc">
<taskdef classname="org.apache.jasper.JspC" name="jasper2">
<classpath id="jspc.classpath">
<pathelement location="${java.home}/../lib/tools.jar" />
<fileset dir="${tomcat.home}/bin">
<include name="*.jar" />
</fileset>
<fileset dir="${tomcat.home}/server/lib">
<include name="*.jar" />
</fileset>
<fileset dir="${tomcat.home}/common/lib">
<include name="*.jar" />
</fileset>
</classpath>
</taskdef>
<jasper2 validateXml="false" uriroot="${webapp.path}" outputDir="${output.servlet}" />
</target>
<target name="cleanup">
<delete>
<fileset dir="${output.servlet}" />
<fileset dir="build/classes/org/apache/jsp" />
</delete>
</target>
</project>
<!--------------------- build_servlet.xml -------------------->
上面
<property name="tomcat.home" value="C:\apache-tomcat-5.5.20" />
為 tomcat 主目錄位置
<property name="output.servlet" value="output_servlet" />
為 servlet 輸出目錄位置
<property name="webapp.path" value="WebContent" />
為 Web root 位置
建立測試 jsp 檔案:
右鍵點選 WebContent -> New -> File
File name : index.jsp
然後按 Finish
內容為:
<%------------------- index.jsp ----------------------%>
<%@ page language="java" contentType="text/html; charset=BIG5"
pageEncoding="BIG5"%>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=BIG5">
<title>Jasper2 Sample</title>
</head>
<body>
<%="Hello Joeyta" %>
</body>
</html>
<%------------------- index.jsp ----------------------%>
產生 Servlet 源檔案:
右鍵點選 build_servlet.xml -> Run As -> Ant Build
然後 Refresh Jasper2Sample Project
就會顯示 output_servlet -> org.apache.jsp.index_jsp.java
內容如下所示:
/********************** index_jsp.java **********************/
package org.apache.jsp;
import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.jsp.*;
public final class index_jsp extends org.apache.jasper.runtime.HttpJspBase
implements org.apache.jasper.runtime.JspSourceDependent {
private static java.util.List _jspx_dependants;
public Object getDependants() {
return _jspx_dependants;
}
public void _jspService(HttpServletRequest request, HttpServletResponse response)
throws java.io.IOException, ServletException {
JspFactory _jspxFactory = null;
PageContext pageContext = null;
HttpSession session = null;
ServletContext application = null;
ServletConfig config = null;
JspWriter out = null;
Object page = this;
JspWriter _jspx_out = null;
PageContext _jspx_page_context = null;
try {
_jspxFactory = JspFactory.getDefaultFactory();
response.setContentType("text/html; charset=BIG5");
pageContext = _jspxFactory.getPageContext(this, request, response,
null, true, 8192, true);
_jspx_page_context = pageContext;
application = pageContext.getServletContext();
config = pageContext.getServletConfig();
session = pageContext.getSession();
out = pageContext.getOut();
_jspx_out = out;
out.write("\n<html>\n<head>\n<meta http-equiv=\"Content-Type\" content=\"text/html; charset=BIG5\">\n<title>Jasper2 Sample</title>\n</head>\n<body>\n");
out.print("Hello Joeyta" );
out.write("\n</body>\n</html>");
} catch (Throwable t) {
if (!(t instanceof SkipPageException)){
out = _jspx_out;
if (out != null && out.getBufferSize() != 0)
out.clearBuffer();
if (_jspx_page_context != null) _jspx_page_context.handlePageException(t);
}
} finally {
if (_jspxFactory != null) _jspxFactory.releasePageContext(_jspx_page_context);
}
}
}
/********************** index_jsp.java **********************/
目錄結構如下所示:
參考文檔:
http://tomcat.apache.org/tomcat-5.5-doc/jasper-howto.html
沒有留言:
發佈留言