在我们开发Web程序的过程,可能您也遇到过这样的问题,如果你正在开发一个web程序,然后发布到tomcat下面,那么,你在修改类的代码的时候,要想生效,就要重新启动tomcat,
这样就很麻烦,也影响效率。
其实,这个问题还是有办法解决的。下面是我在从网上看了一些资料后,自己总结和使用的方法。
开发环境:
Eclipse3.3 + Tomcat6.0.14 + JDK1.6
开发环境的安装和配置略。
类自动编译的实现步骤是:
1) 修改tomcat\conf\server.xml
添加下面的紫色部分的代码,内容和位置如下:
<Service name="Catalina">
<!-- zhangyi add to reload servlet automaticly -->
<DefaultContext reloadable="true"/>
<!--The connectors can use a shared executor, you can define one or more named thread pools-->
<!--
<Executor name="tomcatThreadPool" namePrefix="catalina-exec-"
maxThreads="150" minSpareThreads="4"/>
-->
<!-- A "Connector" represents an endpoint by which requests are received
and responses are returned. Documentation at :
Java HTTP Connector: /docs/config/http.html (blocking & non-blocking)
Java AJP Connector: /docs/config/ajp.html
APR (HTTP/AJP) Connector: /docs/apr.html
Define a non-SSL HTTP/1.1 Connector on port 8080
-->
<Connector port="8080" protocol="HTTP/1.1"
connectionTimeout="20000"
redirectPort="8443" />
2) 在tomcat\conf\Catalina\localhost下面添加工程的发布配置文件
如:zysd.xml文件,发布我的工程zysd
配置代码如下:
<Context docBase="H:\\WorkSpace3.3\\ZY-STRUTS-DEMO\\WebRoot" path="/zysd" reloadable="true">
</Context>
docBase执行你的开发工程的web目录,这样你在开发的时候,你的代码就会自动编译,但是,要让编译生效,还是要加上
reloadable="true"
3) 启动tomcat,测试
如果上面的配置完毕,就可以生效了。
我的servlet测试代码如下:
package org.zy.demo.sd.servlet;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.zy.demo.sd.ImageUtil;
public class CollectParam extends HttpServlet {
public CollectParam() {
super();
}
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out
.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
out.println("<HTML>");
out.println(" <HEAD><TITLE>A Servlet</TITLE></HEAD>");
out.println(" <BODY>");
out.print(" This is ");
out.print("<br>");
out.print(this.getClass());
out.println("<br>");
//out.println(ImageUtil.getImageUrl("map.png"));
out.println("<br>");
out.println("using the GET method");
out.println(" </BODY>");
out.println("</HTML>");
out.flush();
out.close();
}
public void init() throws ServletException {
// Put your code here
}
}
测试url为:http://localhost:8080/zysd/servlet/CollectParam
结果如下:
This is
class org.zy.demo.sd.servlet.CollectParam
using the GET method
入后我就写了一个测试类:
package org.zy.demo.sd;
public class ImageUtil {
public static String getImageUrl(String imgName){
return "/images/view/"+imgName;
}
}
然后,在上面的servlet里面调用如下:
out.println(ImageUtil.getImageUrl("map.png"));
也就是,直接写完类,就可以测试上面的链接,结果如下:
This is
class org.zy.demo.sd.servlet.CollectParam
/images/view/map.png
using the GET method
由此可见,修改已经生效。
我们可以看一下控制台的输出:
信息: Initialization processed in 2712 ms
2007-9-21 15:55:53 org.apache.catalina.core.StandardService start
信息: Starting service Catalina
2007-9-21 15:55:53 org.apache.catalina.core.StandardEngine start
信息: Starting Servlet Engine: Apache Tomcat/6.0.14
log4j:WARN No appenders could be found for logger (org.apache.struts.uti
tyMessageResources).
log4j:WARN Please initialize the log4j system properly.
2007-9-21 15:56:04 org.apache.coyote.http11.Http11Protocol start
信息: Starting Coyote HTTP/1.1 on http-8080
2007-9-21 15:56:04 org.apache.jk.common.ChannelSocket init
信息: JK: ajp13 listening on /0.0.0.0:8009
2007-9-21 15:56:04 org.apache.jk.server.JkMain start
信息: Jk running ID=0 time=0/61 config=null
2007-9-21 15:56:04 org.apache.catalina.startup.Catalina start
信息: Server startup in 11541 ms
2007-9-21 15:56:34 org.apache.catalina.core.StandardContext reload
信息: Reloading this Context has started
2007-9-21 15:57:15 org.apache.catalina.core.StandardContext reload
信息: Reloading this Context has started
2007-9-21 15:57:27 org.apache.catalina.core.StandardContext reload
有黑体部分,我们可以知道,其实当前的Context已经在自动重载了。
所以,通过这种开发模式,可以给我们带来很大的方便。您不妨也试一下,如果有其他的好地新的体会,大家可以共同讨论。
|----------------------------------------------------------------------------------------|
|----------------------------------------------------------------------------------------|