What are the advantages of building a small Java web app to run in a Servlet container (like Tomcat) vs. building a standalone Java app with a built-in web server and running it behind a reverse proxy?
I've been playing with Java for about a year. I've noticed that to launch Tomcat takes time, and it's not always possible to do a hot-redeploy due to class loader issues. The Servlet API seems somewhat convoluted to me, especially from the perspective of configuration and of RESTful design (which it doesn't really fully support).
On the other hand, I've noticed that my IDE can compile and run a standalone app at lightning speed. Configuring Apache for reverse-proxying is a piece of cake, and embedded Jetty seems to handle whatever I can throw at it. I don't need Servlets when I can use Restlet, Wicket, etc. Being able to know better how my app works (because it's not integrated with a huge app server) feels empowering. The stack traces are shorter. Download size is smaller. End-user configuration is easier. I'm guessing performance is likely to be better because there are fewer software layers involved.
However, I am reminded of the saying that what sounds too good to be true usually is. So my question is, why would I not want to make my web apps standalone? What does a Servlet container give me and/or my end users that we really need but don't know about?
Web containers are a part of a web server and they generally processes the user request and send a static response. Servlet containers are the one where JSP created components reside. They are basically responsible to provide dynamic content as per the user request.
A Java servlet that is also a stand alone program. And a server that is also a web client.
The servlet container is the part of web server which can be run in a separate process. We can classify the servlet container states in three types: Standalone: It is typical Java-based servers in which the servlet container and the web servers are the integral part of a single program.
Apache Tomcat is a long-lived, open source Java servlet container that implements core Java enterprise (now Jakarta EE) specifications, including the Jakarta Servlet, Jakarta Server Pages, and Jakarta WebSocket specs.
There are 2 separate questions in here:
Should I be using an embedded server, or deploy into a container?
I don't think you should be seeing a big difference one way or the other. There's slightly more code to startup a Jetty server programmatically, and configuration is easier to do programmatically. Even though IDE support for web app configuration and deployment is getting better, it's still worse than for standalone applications (this is kinda by definitions, since there's a superset of things to support).
On the other hand, app servers give you some nice benefits like built-in management, built-in ability to run as a service, etc.
You could even use a hybrid approach: use an embedded server to develop locally, then deploy into a container in production. But that's a bit weird: if you go through the trouble of making a proper WAR file, IDEs should really be able to handle deployment into a container adequately.
BTW, it's weird that you have issues with hot-redeploy; Tomcat shouldn't be having issues with it unless you're running into some strange corner case...
Should I be using Servlet API?
This is orthogonal from #1. You could very well embed Jetty and implement Servlets. You could also use Restlet API inside Tomcat through a ServerServlet http://www.restlet.org/documentation/1.0/faq#02.
I personally find the Servlet API to be pretty straight-forward.You get nice things like concurrency and state management. I don't quite know what that means that RESTful design is not supported, but if Restlets address your requirements better, then use that...
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With