设为首页 收藏本站
查看: 363|回复: 0

[经验分享] Debugging a servlet with tomcat and Eclipse tutorial

[复制链接]

尚未签到

发表于 2017-2-1 10:36:38 | 显示全部楼层 |阅读模式
  转自:http://www.wikijava.org/wiki/Debugging_a_servlet_with_tomcat_and_Eclipse_tutorial
  不需要懂太多的英语,看看图就会弄了。
  This tutorial will guide you through the process setting up the debugging of a simple Servlet under Eclipse using Tomcat. There are many plugins for Eclipse to help you configuring this, they all basically try to automatize the process explained in this tutorial.
  It's always good to know what's going on under the hood.


Contents





  • 1 The article


    • 1.1 Concepts

    • 1.2 Setting tomcat in debug mode

    • 1.3 Deploying your application to Tomcat

    • 1.4 Connecting Eclipse to Tomcat for debugging

    • 1.5 Proceeding with the debug

    • 1.6 Ending Debugging Session


  • 2 See Also

  • 3 Comments from the users


The article
  The configuration process for debugging a Java servlet may appear scary and complex to the beginner. However, in this tutorial I will clearly explain to you how debugging in Tomcat works as well as show you how to trigger a debugging session.

Concepts
  The debugging of a web application involves several components:

The Application Server
Generally speaking all the application servers support the debug mode, you just have to enable it when starting up the application server. It will then open a debugger server (generally on port 8000). You can then connect to this port to start debugging your application. In this tutorial I will show you how to activate the debug mode in Apache Tomcat.
The IDE
You can debug your application using the default command line debugger, which will give you complete access to the debugger and to the debugged software. It is much easier though to use an IDE to support your debugging. The IDE will in this case use a TCP/IP connection to connect to the debug port (usually 8000) of the application server. In this example I will explain you how to configure Eclipse to debug your application running on Tomcat
The debugger
The debugger is an external software which allows you to debug your programs. It will be run by the application server. Most the application servers come already configured with a debugger, the one preinstalled in Tomcat is JDB.
Your code to debug
Your code will be running on Tomcat, I will not go into details on how to deploy your applications. You can debug with the method explained in this tutorial any application that runs on the application server, with no limitation whatsoever.   As you may notice the connection between the application server and the IDE uses the TCP/IP protocol, this implies that you don't need to have the tomcat running on your same machine, you don't even need to be in the same continent of it. It is enough that you have network access (ex. internet) to the server, and you can debug whatever application wherever in the world. Isn't this the coolest thing ever?

Setting tomcat in debug mode
  Tomcat comes ready for debugging. To enable the debug mode you just need to write the following commands (under windows OS, analogous commands under different OSs):

set JPDA_ADDRESS=8000
set JPDA_TRANSPORT=dt_socket
catalina.bat jpda start



  ensure to execute this from the {catalina_home}/bin directory. For simplicity I normally generate a shell script (a .bat file) to automatize the process.
  I call this file debugmode.bat, it contains the three commands listed above and I normally put it in the {catalina_home}/bin directory. I am then able to start the tomcat in debug mode just by double clicking on this file with the mouse.

Deploying your application to Tomcat
  In order to debug your application you must deploy it in the application server, you do this in the normal way. Nothing special about this.

Connecting Eclipse to Tomcat for debugging
  To connect Eclipse to the debug server created by tomcat follow these simple steps:


  • Start Eclipse
  • open the project containing the servlet that you want to debug
  • click on the arrow close to the debug button (the little bug) to see the drop down menu
  • click on open debug dialog... (may also be labeled "Debug Configurations...")  
    DSC0000.png  
  • in the debug window double click on the Remote Java Application on the left hand side list. This will create a new debug profile.  
    DSC0001.png
     
  • choose a name for the debug profile (it's just for you to easily distinguish it)
  • select the project you want to debug
  • verify that the other settings match your configuration

    • the host is the address for your tomcat, if Tomcat is running on your machine then it will be localhost.
    • the port is the one you configured in when starting Tomcat, 8000 by default  
      DSC0002.png
       


  • click on debug
  at this point eclipse will connect to the debug server loaded in Tomcat, so it's important to do this procedure after you load Tomcat.
  You can set some breakpoints on your servlet and you will see that nothing happens. This is because the servlet is indeed NOT executed yet.

Proceeding with the debug
  To execute the servlet, and start debugging your code you simply open your web browser (for example firefox) and navigate to the address of your servlet, this will execute it. The execution will block at the point you put your breakpoint and the web browser will block too, waiting for the page.
  At this point you can normally debug your application as you do with other programs.
  Note that your IDE is running on your machine, while the servlet is running on the application server, and so is the debugger. The two latter elements are working on the same machine, but the machine for them may be different than your local one.
  Through the web browser you can call the servlet as many times as you want and your Eclipse will be showing you the details about these executions, without actually needing to click again on the debug button, this may be very handy.

Ending Debugging Session
  To end a debugging session simply click the Disconnect button within the Debug perspective.
DSC0003.png
 
  over!

运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-336044-1-1.html 上篇帖子: javaweb乱码(tomcat服务器) (装) 下篇帖子: Tomcat学习笔记 -【 虚拟主机与虚拟目录】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表