Jsp&Servelet 学习笔记- 在tomcat上部署一个单独的servlet的程序(Ant)

 
2.1 在tomcat上部署一个单独的servlet的程序

如果想在把一个servlet程序部署在tomcat之上测试它能否运行,做一个初步的测试。而不想花时间为这个servlet来构建一个完整的web应用。 通过执行Tomcat Shell 命令来关闭它<tomcat-installation-directory>/bin/shutdown

复制这个servlet文件到tomcat的缺省web应用中,或者用Ant工具的build.xml文件来移动这个文件临时地进入到Tomcat的缺省web应用中。

有时候当我们设计出一个servlet时,会渴望看看它是否能正常运行。你能够测试它通过复制它到Tomcat的缺省web应用下,除非这个servlet还得运行还需依靠其他的servlet或其他的类组件。这个取胜的应用位于<Tomcat-installation-directory>/webapps/ROOT.

如果有一个完全合法的Servlet类叫做mydev.CookieServlet,那么下面将有一个完整的步骤手动将这个单独的Servlet部署到Tomcat中。

  • 创建一个mydev目录把它放到 <Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes目录下
  •    复制CookieServlet class文件到<Tomcat-installation-directory>/webapps/ROOT/WEB-INF/classes/ jspservletcookbook目录下
  •   启动Tomcat
  •     输入http://localhost:8080/servlet/jspservletcookbook.CookieServlet.

这个手动方法比较麻烦并且很慢有没有比这个更好的部署方法呢?答案是肯定的;

我们将用Jakarta Ant来代替手动部署的流程。假定我们已经安装配置好了Ant开源工具

创建一个方便的目录在下面放入build文件并创建一个global.properties文件

widows命令行中转到这个目录下 敲入ant命令。

 

 

< project  name ="Cookbook"  default ="deploy-servlet"  basedir ="." >

   
< taskdef  name ="start"  classname ="org.apache.catalina.ant.StartTask"   />
   
< taskdef  name ="stop"  classname ="org.apache.catalina.ant.StopTask"   />

<!--  Load in some global properties  -->

   
< property  file ="global.properties"   />
   
< target  name ="init"  description ="Initializes some properties." >

       
< echo  message ="Initializing properties." />
       
< property  name ="build"  value =".uild"   />
       
< property  name ="src"  value =".src"   />

        
<!--  The context-path is just a slash character when it is the ROOT application;
        see the start and stop targets, which already include the slash as part of
        the URL pattern 
-->

       
< property  name ="context-path"  value =""   />
   
</ target >
  
   
< target  name ="prepare"  depends ="init" >
       
< echo  message ="Cleaning up the build directory." />
       
< delete  dir ="${build}" />
       
< mkdir  dir ="${build}" />
   
</ target >
  
<!--  Set the CLASSPATH to various Tomcat .jar files  -->

   
< path  id ="classpath" >
       
< fileset  dir ="${tomcat.dir}/common/lib" >
           
< include  name ="*.jar"   />
       
</ fileset >
       
< fileset  dir ="${tomcat.dir}/common/endorsed" >
            
< include  name ="*.jar"   />
       
</ fileset >
   
</ path >
  
<!--  start the default Tomcat web application  -->

   
< target  name ="start"  
     description
="Starts the default Web application" >
       
< echo  message ="Starting the default application...." />
       
< start
          
url ="${url}"  
          username
="${username}"  
          password
="${password}"  
          path
="/${context-path}"  
        
/>
   
</ target >

<!--  stop the default Tomcat web application  -->

   
< target  name ="stop"  
     description
="Stops the default Web application" >
       
< echo  message ="Stopping the application...." />
       
< stop
          
url ="${url}"  
          username
="${username}"  
          password
="${password}"  
          path
="/${context-path}"  
       
/>
   
</ target >

<!--  stop the default Tomcat web application, compile your servlet, add it to the default 
Web application, then start the default web application 
-->

   
< target  name ="deploy-servlet"  depends ="prepare"  
      description
=
      "Compile the specified servlet, then move it into Tomcat's default
       Web application."
>

       
< echo  message ="Stopping the default Tomcat application...." />
       
< antcall  target ="stop" />
       
< echo  message ="Compiling the servlet...." />
       
< javac  srcdir ="${src}"  destdir ="${build}" >
           
< include  name ="${compiled.servlet}.java"   />   
           
< classpath  refid ="classpath" />
       
</ javac >
       
< echo  message =
          "Copying the servlet to Tomcat ROOT web application..."
/>

       
< copy  todir ="${tomcat.webapps}/WEB-INF/classes" >
           
< fileset  dir ="${build}"   />
       
</ copy >
       
< echo  message ="Starting the default application...." />
       
< antcall  target ="start" />
   
</ target >

</ project >

 

tomcat.webapps=k:/jakarta-tomcat- 4.1.12 /webapps/ROOT

tomcat.dir=k:/jakarta-tomcat- 4.1.12

url=http://localhost:8080/manager

compiled.servlet=CookieServlet

username=tomcat

password=tomcat

 

用一个属性文件global.properties来加载这些在这个property文件中定义好的属性。如:

${tomcat.dir}

 

 

 
  global.properties 文件
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值