ITeye博客

嵌入式Tomcat用于在Eclipse中调试

sunrie · 2007-08-02 10:19 · JSP/Servlet · Markdown

嵌入式tomcat相对于myeclipse、wtp等eclipse插件优势主要在与速度快、免部署、方便调试。

首先下载嵌入式的tomcat
http://apache.mirror.phpchina.com/tomcat/tomcat-5/v5.5.25/bin/apache-tomcat-5.5.25-embed.zip
解压,假设解压路径为%CATALINA_HOME%=D:/apache-tomcat-5.5.25-embed
在eclipse中Window->Preference->Java->Build Path->User Libraries中添加一个EmbedTomcat的库,将%CATALINA_HOME%/lib下的所有包都加入到该库中。
在工程中加入EmbedTomcat库,加入如下2个java文件,设置好contextPath,mappingPath,以及tomcat路径,将
SimpleTomcatLauncher以DEBUG方式作为普通java application启动,即可设置断点进行调试。

EmbeddedTomcatLauncher
java 代码

 

  1. package per.sunrie;
  2. import org.apache.catalina.Context;
  3. import org.apache.catalina.Engine;
  4. import org.apache.catalina.Host;
  5. import org.apache.catalina.connector.Connector;
  6. import org.apache.catalina.startup.Embedded;
  7. /**
  8. * @author sunrie
  9. * @version $Id$
  10. */
  11. public abstract class EmbeddedTomcatLauncher {
  12. private static final int DEFAULT_PORT = 8080;
  13. private Embedded tomcat;
  14. protected EmbeddedTomcatLauncher() {
  15. initEmbedded();
  16. initShutdownHook();
  17. }
  18. protected abstract String[] getContextsAbsolutePath();
  19. protected abstract String[] getContextsMappingPath();
  20. protected abstract String getTomcatPath();
  21. protected int getPort(){
  22. return DEFAULT_PORT;
  23. }
  24. protected void startTomcat() {
  25. try {
  26. long startTime = System.currentTimeMillis();
  27. tomcat.start();
  28. System.err.println(
  29. "Embedded Tomcat started in "
  30. + (System.currentTimeMillis() - startTime) + " ms."
  31. );
  32. } catch (Exception e) {
  33. e.printStackTrace();
  34. }
  35. }
  36. private void initEmbedded() {
  37. tomcat = new Embedded();
  38. tomcat.setCatalinaBase(getTomcatPath());
  39. Host host =
  40. tomcat.createHost(
  41. "localhost",
  42. tomcat.getCatalinaHome() + "/webapps"
  43. );
  44. String[] contexts = getContextsMappingPath();
  45. String[] contextsPath = getContextsAbsolutePath();
  46. Context context = null;
  47. for (int i = 0; i < contexts.length; ++i) {
  48. context = tomcat.createContext(contexts[i], contextsPath[i]);
  49. host.addChild(context);
  50. }
  51. Engine engine = tomcat.createEngine();
  52. engine.setName("MyDevAppServer");
  53. engine.addChild(host);
  54. engine.setDefaultHost(host.getName());
  55. tomcat.addEngine(engine);
  56. // 只能本机访问
  57. Connector connector = tomcat.createConnector("localhost", getPort(), false);
  58. // 可从其它机器访问
  59. // Connector connector = tomcat.createConnector((InetAddress) null, getPort(), false);
  60. connector.setURIEncoding("UTF-8");
  61. tomcat.addConnector(connector);
  62. }
  63. private void initShutdownHook() {
  64. Runtime.getRuntime().addShutdownHook(
  65. new Thread() {
  66. public void run() {
  67. stopTomcat();
  68. }
  69. }
  70. );
  71. }
  72. private void stopTomcat() {
  73. try {
  74. tomcat.stop();
  75. } catch (Exception e) {
  76. e.printStackTrace();
  77. }
  78. }
  79. }

SimpleTomcatLauncher
java 代码

 

  1. package per.sunrie;
  2. import java.net.MalformedURLException;
  3. import java.net.URL;
  4. /**
  5. * @author sunrie
  6. * @version $Id$
  7. */
  8. public class SimpleTomcatLauncher extends EmbeddedTomcatLauncher {
  9. public static void main(String[] args) {
  10. new SimpleTomcatLauncher().startTomcat();
  11. }
  12. @Override
  13. protected String[] getContextsAbsolutePath() {
  14. return new String[] { getSingleContextAbsolutePath() };
  15. }
  16. private String getSingleContextAbsolutePath() {
  17. try {
  18. URL url = getClass().getResource("/");
  19. url = new URL(url, getSingleContextPathRelativeToClassPath());
                String path = url.getPath();
                path = URLDecoder.decode(url.getPath(), "UTF-8");
                return path;
  20. } catch (MalformedURLException e) {
  21. throw new RuntimeException(e);
            } catch (UnsupportedEncodingException e) {
                throw new RuntimeException(e);
  22. }
  23. }
  24. private String getSingleContextPathRelativeToClassPath() {
            //实际contextPath(与classpath的相对路径)
  25. return "../web";
  26. }
  27. @Override
  28. protected String[] getContextsMappingPath() {
            //映射路径
  29. return new String[] { "/webContext" };
  30. }
  31. @Override
  32. protected String getTomcatPath() {
            //Tomcat的HOME
  33. return "/D:/apache-tomcat-5.5.25-embed";
  34. }
  35. }

评论(0)

暂无评论。

评论功能维护中,当前仅展示历史评论。