开发者

What happens when several Java servlet apps running on the same port?

Something strange happened to my servlets and I think I've figured out why, yet I'm more confused. I used Netbean6.7 to develop a Paypal IPN (Instant Payment Notification) message servlet, it listens on port 8080 by default for Paypal IPN messages. I used some sample Java code from it's web site, but when it ran, only about 1 in 10 messages came through, and they looked correct, but why 1 in 10 ? Not 100% or none ?

So I asked some questions here and got some advices, one in particular points me to Google's App Engine, so I downloaded it and ran the demo guestbook while my IPN servlet is still running on Netbeans, the strange thing happened, after I entered "appengine-java-sdk-1.3.2\bin\dev_appserver.cmd appengine-java-sdk-1.3.2\demos\guestbook\war" from the command prompt, I went to the following url on my browser "http://localhost:8080/", I thought I would see the Google demo guestbook page, NO, what I saw was another servlet I developed 2 years ago : "Web Academy", online course registration app.

How can that happen? I never started it, and I haven't touch that project for years. I guess because it's also listening on port 8080, so now I understand why the IPN messages only came through 1 in 10 times, because another servlet was also listening on that port and could have got the messages intended for IPN, or some how those two servlets' processes mixed up and therefore couldn't respond to Paypal properly, and failed. In order to verify some of my guesses.

I turn off Netbeans, and ran the Google guestbook again at t开发者_运维问答he prompt, this time on my browser http://localhost:8080/ points to the demo guestbook page.

My Urls look like this :

  • [A] Paypal IPN : http://localhost:8080/PayPal_App/PayPal_Servlet
  • [B] Web Academy : http://localhost:8080/

So now, my questions are :

  1. Why the "Web Academy" servlet was auto started when I ran the Paypal servlet ?
  2. If I change the IPN listening port to 8083, would that mean I can run both of them on my PC at the same time without affecting each other ?
  3. But I still don't understand, [A] and [B] look different, if a page for [A] is refreshed, it should show the Paypal content, and another page looking at [B] should show the Web Academy content, and that's exactly what happens when I started Netbeans to run the Paypal servlet, both pages show their respective content correctly side by side without interfering with each other, how come the IPN messages couldn't get through 100% of the time ?
  4. In Netbeans how to assign 8080 to servlet [A] and assign port 8083 to servlet [B] ?
  5. How to turn off auto start of Web Academy by Netbeans ?


First: Servlet != Webserver. A servlet is a Java application programming interface which is supposed to run in a servletcontainer. A servletcontainer is usually part of a Java web server, like Apache Tomcat and Glassfish. A web server usually listens on a certain port for HTTP requests. A servletcontainer creates and instantiates servlets on startup. Every request whose URL matches the url-pattern of the servlet will let the servletcontainer execute the servlet.

Second: Netbeans != Webserver. Netbeans is an integrated development environment (IDE). It's just a development tool with a bunch of wizards which should ease the development. You can develop as perfectly fine with just cmd.exe and notepad.exe and a standalone server as with an IDE. You just has to know what you're doing.

As to your actual problem: you probably have developed the "Web Academy" project in Netbeans before and deployed it as "ROOT" project of the webserver in question. The GAE probably failed to startup because the port is in use by the webserver managed by Netbeans (did you note of any error messages during GAE startup?). Shutting down Netbeans (which is a bit drastically, just shutting down the webserver managed by Netbeans has been sufficient) would have freed up the port.

That said, honestly, all this confusion seems to boil down that you never learnt the basic Java / Servlet concepts separately and immediately dived in the deep hole with an all-in-one IDE where you have to learn 1000 different things altogether in a single place and assumed that it's the "only right" place to do it. I strongly recommend to take a pause and restart learning the basic concepts one by one. Get some good books and take the time to get it all right. Learn Java SE separately. Learn Servlets separately. Learn Netbeans separately. Learn webserver separately. And so on. Then do the Math. Good luck.


There are so much things questions mixed in your question that I don't know where to start. What is sure is that you need to go back to the basics and to take some time to understand how things work. Servlets are pieces of code packaged inside a web application which is deployed in a servlet container (a server). Let's look at how you access them:

http://<host>:<port>/<context>/<url-pattern>
         A      B        C           D

Where:

  • A is the machine (hostname or IP) where the servlet container is running.
  • B is the port to which the servlet container is listening to (not a servlet).
  • C is the path to a web application (typically the name of the web archive without the .war extension but a webapp can be mapped to "/" aka the root context).
  • D is the URL pattern to which a servlet is mapped.

Now, let's try to answer your questions:

Why the "Web Academy" servlet was auto started when I ran the Paypal servlet ?

My guess is that this old application is still deployed on the server you're using under NetBeans (maybe NetBeans built-in server). So when you "start" the new application (and actually the server), the old one is also accessible.

If I change the IPN listening port to 8083, would that mean I can run both of them on my PC at the same time without affecting each other ?

As we saw, this doesn't make sense, a server is listening to a port, not a servlet.

But I still don't understand, [A] and [B] look different, if a page for [A] is refreshed, it should show the Paypal content, and another page looking at [B] should show the Web Academy content, and that's exactly what happens when I started Netbeans to run the Paypal servlet, both pages show their respective content correctly side by side without interfering with each other, how come the IPN messages couldn't get through 100% of the time ?

The URL [A] points to the PayPal_Servlet of the PayPal_App. The URL [B] points to the default page of a web application mapped to the root context. In other words, both URLs points to different applications.

In Netbeans how to assign 8080 to servlet [A] and assign port 8083 to servlet [B] ?

Sorry to repeat that but this doesn't make sense. You can very likely change the port used by the server started in NetBeans but, still, both servlets would be served by the same server, running on a new port.

How to turn off auto start of Web Academy by Netbeans ?

I'm not sure but do some clean up in the directory where applications are deployed (with tomcat, check the webapps directory) or in the server.xml. Hard to say without more details on your configuration, on what you installed, etc.


Servlets are mapped to unique URLs, which includes the port, so it's perfectly okay to have several servlets listening to the same port number. It's common when you have a single web context with more than one servlet/JSP associated with a single URL.

The URL for your context usually looks like this:

http://host:port/context-name

In your case, host is localhost, port is 8080. But the context-name is "PayPal_App" for one and the root for another. I think you'd be wise to use another context like "demo" for your other app rather than the root context. Your apps don't belong there.


You cannot assigned a port to a servlet. You assign a port to your servlet container (being Tomcat, Jetty, Glassfish, JBoss, etc.) A servlet is simply an instance of javax.servlet.Servlet, not an entire server.

If you wish to have more than one servlet running in a server, you create a servlet (I'm thinking you'll want to extend javax.servlet.http.HttpServlet) and configure it in the web.xml for your application. As long as the names of your sevlets do not conflict you should be fine.


I'm guessing either you have a servlet container running externally and you simply configured NB to use it, or you have imported all your old NB projects, and they've all gotten deployed somehow. The reason you saw your registration app was that you weren't watching your GAE log files, so you didn't notice that it died when it couldn't listen on the port. You also didn't assign a unique URL to the app, so requests that went to the root were handled by the registration app. None of this explains why your PayPal requests are getting dropped, I'd start up snoop/wireshark/whatever and check your HTTP traffic. My guess is your app has a bug. Checking the servlet container log file may reveal some clues. To undeploy your registration app, find out where your app server stores the web apps and remove either the war file or the directory holding the registration app files.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜