D:\XDocletSample\src\javamxj\HelloServlet.java
/* * @author javamxj (CSDN Blog) 创建日期 2005-1-7 */ package javamxj; import javax.servlet.*; import javax.servlet.http.*; /** * @web.servlet name="HelloServlet" * display-name="Hello Servlet" * load-on-startup="1" * @web.servlet-init-param name="hello" * value="${hello.servlet}" * @web.servlet-mapping url-pattern="/Hello/*" */ public class HelloServlet extends HttpServlet { public void init(ServletConfig config) throws ServletException { //从web.xml中获得初始化参数 super.init(config); } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException { ServletConfig config = this.getServletConfig(); String h = config.getInitParameter("hello"); try { //首先设置文档类型 response.setContentType("text/html; charset=GBK"); //获取输出流 java.io.PrintWriter out = response.getWriter(); out.println("<html><head><title>Welcome javamxj Blog!</title></head>"); out.println("<body><h1>"); out.println(" Hello: " + h); out.println("</h1></body></html>"); out.close(); } catch (Exception e) { throw new ServletException(e); } } }
D:\XDocletSample\build.xml
<?xml version="1.0" encoding="GBK"?> <project name="filtering" default="deploy" basedir="."> <description>一个简单的XDoclet实例</description> <!-- 载入属性文件 --> <property file="build.properties"/> <!-- 定义类路径 --> <path id="web.classpath"> <pathelement location="${tomcat.home}/common/lib/servlet-api.jar"/> <pathelement location="${tomcat.home}/common/lib/jsp-api.jar"/> </path> <path id="xdoclet.classpath"> <fileset dir="${xdoclet.home}/lib"> <include name="*.jar"/> </fileset> <path refid="web.classpath"/> </path> <!-- 初始化,建立目录 --> <target name="init"> <mkdir dir="${dist.dir}"/> <mkdir dir="${dist.dir}/WEB-INF"/> <mkdir dir="${dist.dir}/WEB-INF/classes"/> </target> <!-- XDoclet 的 WebDoclet 任务 --> <target name="webdoclet" depends="init"> <taskdef name="webdoclet" classpathref="xdoclet.classpath" classname="xdoclet.modules.web.WebDocletTask"/> <webdoclet destDir="${dist.dir}/WEB-INF" force="${xdoclet.force}"> <deploymentdescriptor Servletspec="2.4" xmlencoding="GBK"/> <fileset dir="${src.dir}" includes="**/*Servlet.java"/> </webdoclet> </target> <!-- 编译与部署 --> <target name="deploy" depends="webdoclet"> <javac srcdir="${src.dir}" destdir="${dist.dir}/WEB-INF/classes"> <classpath refid="web.classpath"/> </javac> <jar destfile="${tomcat.home}/webapps/${app.name}.war" basedir="${dist.dir}"/> </target> </project>
D:\XDocletSample\build.properties |
################ 环境设置 ################### # # 文件名:build.properties # 作者: CSDN javamxj # # web服务器目录 tomcat.home=F:/java/jakarta-tomcat # xdoclet目录 xdoclet.home=F:/java/xdoclet # web的临时目录 dist.dir=./dist # 源文件目录 src.dir=./src # 发布的程序名 app.name=HelloWeb # Servlet参数,可以改变 hello.servlet=javamxj blog |