开始部分:
[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>
|
上面
<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
內容为:
<%@ 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>
|
产生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);
}
}
}
|
目录结构如下所示:

参考文档:
http://tomcat.apache.org/tomcat-5.5-doc/jasper-howto.html
作者的Blog:http://blog.matrix.org.cn/page/joeyta