How do I monitor HTTP communication in Eclipse?

来源:互联网 发布:whois数据库 编辑:程序博客网 时间:2024/05/22 17:43

转自: http://www.avajava.com/tutorials/lessons/how-do-i-monitor-http-communication-in-eclipse.html?page=2

EclipseSW has a TCP/IP Monitor view that comes in very handy if you're interested in watching the HTTP communication between a browser and your web application. Basically, it listens on a port on your machine where Eclipse is running, and it forwards that request on to a destination host and port. TomcatSW typically runs on port 8080 when I fire up a project on my local machine, so I have the TCP/IP Monitor listen on port 8081 and forward the request to port 8080 of the localhost. By listening to 8081 and forwarding to 8080, the TCP/IP Monitor grabs and displays the requests going to the server and the responses coming back from the server. It can display headers, which can be very useful when trying to track down difficult issues with web application. It is also very useful if you ever need to do analyze something like SOAP communication.

If you have the Eclipse Web Tools, you should have the TCP/IP Monitor view, which can be found at Window → Show View → Other.

selecting view to show

I selected the TCP/IP Monitor view and clicked OK.

selecting TCP/IP Monitor view

This brought up the monitor view. I checked the 'Show Header' option and then clicked 'Properties'.

TCP/IP Monitor view

I clicked 'Add' to add a new monitor.

adding a new monitor

I set the monitor up to listen on port 8081 and to send requests to 8081 to localhost:8080.

set monitor to listen on port 8081 and forward requests to localhost:8080

I clicked 'Start' to start the port 8081 monitor.

start monitor on port 8081

I clicked OK to return.

click OK

If we fire up our web application on the default 8080 port and we try to hit 8080 directly, we should see that the monitor is not hit, since the request went directly to 8080.

browser request to port 8080

As expected, the TCP/IP Monitor shows that it wasn't hit.

monitor doesn't show activity

Next, let's try hitting port 8081. This should hit the TCP/IP Monitor listening on port 8081, and this should get forwarded to port 8080. TomcatSW should receive the request for the web page and send back the results.

browser request to port 8081

As expected, the monitor now shows the communication between the browser and the local Tomcat application. We can see in the request header that the browser asks localhost:8081 for /java-tutorials/, which returns an htmlW page which is displayed in the bottom right-hand section of the results.

monitor displays requests and responses

We can see that a cssW file and five graphic files were requested in order to render the html page. If we click on the header.jpg file, we can see that the monitor displays the actual image! In my version of EclipseSW, sometimes the monitor has some trouble display certain images, but in this example, it worked great.

display of image by monitor view

I've seen that in my version of Eclipse, the monitor can sometimes get stuck, and I need to refresh the browser in order for the request to get completed. I wouldn't recommend having the monitor running all the time, but it can be very useful for things such as watching the cookieW communication between your browser and servletW container.