0

I've worked with Apache web server before, using PHP, Ruby, and serving static JavaScript/HTML pages.

One thing that I'm confused on with Tomcat is basically WHY does Tomcat exists in the first place, rather than using Apache with installed JVM? Why of all these popular languages does Java need it's own specialize container, namely Tomcat?

If one wants to use PHP to serve content, simply install PHP on the Apache server and voila when a user goes to mysite.com/mypage.php this happens:

  1. Apache server gets the corresponding file mypage.php
  2. sees PHP, uses the PHP intepreter to process the page
  3. returns the result

Why isn't it the same as this when working with Java?

In my mind it should go like this: simply have Java and JVM installed on Apache server and then when user goes to mysite.com/mypage this happens:

  1. Apache server gets the corresponding file
  2. sees Java, uses JVM to compile/process the page
  3. returns the result

Is it because Java files need to be compiled, and it wouldn't make sense to re-compile it at every request? Then why not just map the request to proper .class file?

This might sound absurd to those confident with Tomcat, but as you can see I unfortunately don't get it.

Don Cheadle
  • 5,224
  • 5
  • 39
  • 54
  • 4
    why do so many things exist that all most or entirely duplicate another thing? –  Nov 16 '14 at 04:06
  • You should take a look at the differences between Apache and Tomcat: http://stackoverflow.com/questions/30632/difference-between-the-apache-http-server-and-apache-tomcat – mkobit Nov 16 '14 at 04:18
  • 3
    The real answer is that the Java Servlet Specification exists, and *something* needs to implement that spec to allow Java servlet-based web applications to run. Apache httpd is definitely not the right place to do that. PHP is self-hosting while Java doesn't directly provide these services. The comparison between PHP and Java (servlets) shouldn't be that "all PHP needs is a script on the disk while Java needs all this junk", but instead "PHP contains all this junk and Tomcat provides that same junk for Java." – Christopher Schultz May 10 '16 at 14:49

2 Answers2

1

Tomcat is an apache project. While it is theoretically possible to implement a JSP and Servlet container (like Tomcat) using native code that is not the implementation strategy that was ultimately chosen (I believe Websphere actually does follow that implementation model). However, tomcat does have Connectors and to quote

AJP

When using a single server, the performance when using a native webserver in front of the Tomcat instance is most of the time significantly worse than a standalone Tomcat with its default HTTP connector, even if a large part of the web application is made of static files. If integration with the native webserver is needed for any reason, an AJP connector will provide faster performance than proxied HTTP. AJP clustering is the most efficient from the Tomcat perspective. It is otherwise functionally equivalent to HTTP clustering.

The native connectors supported with this Tomcat release are:

JK 1.2.x with any of the supported servers
mod_proxy on Apache HTTP Server 2.x (included by default in Apache HTTP 
   Server 2.2), with AJP enabled
Elliott Frisch
  • 198,278
  • 20
  • 158
  • 249
  • why is it necessary to provide this `JSP and Servlet container`? Why is this `container` such a necessary thing? As I mentioned in OP, other languages do not seem to have any `container` besides the web server itself and the language's interpreter (as far as I can tell) – Don Cheadle Nov 16 '14 at 05:17
  • I am not sure if 'necessary' is the right word. If I build a Java Web Archive file (.war), I can drop it onto Tomcat, JBoss, Glassfish, Jetty, etc... and it will be served up. As well, there are a number of great features available when deploying inside a container. – Jamie Nov 16 '14 at 05:24
  • @mmcrae Because otherwise it isn't JSP and Servlet(s). The technologies require what is known as a web container. – Elliott Frisch Nov 16 '14 at 05:30
1

Php and Java are development platforms that can run independently of Apache. In order to run PHP scripts/apps you first need to install the PHP interpreter, the same things goes for Java where you would have to install a JVM that works as an interpreter for Java files/apps.

In order to run PHP files using Apache you have to enable the mod_php extension, this is basically a program that allows to call the PHP interpreter from Apache and execute php scripts. The J2EE platform requires a Web Container (https://docs.oracle.com/javaee/5/tutorial/doc/bnabo.html) to execute or run web applications. Apache can connect with a J2EE web container using a module, usually you would use AJP to allow Apache to communicate with Tomcat and execute Java Servlets or JSP pages.

You don't require Apache to run Java web applications, however you must use a J2EE web Container to run them, so technically Tomcat can be used as a web server but it is not recommended for production since Apache would have a better performance specially when serving static content.

Jorge
  • 227
  • 1
  • 3
  • 1
    so to run PHP on Apache you need `mod_php`, but to run Java on Apache you need some `Web Container`, Tomcat being an implementation of one? I guess I'm surprised / confused for PHP it's just a call to an interpreter whereas for Java it's a separate "container" used... if I followed that correctly – Don Cheadle Nov 16 '14 at 05:29
  • Yes you are correct, the reason for this is because the web container is a component of the J2EE platform which has different architecture/approach from PHP. (http://www.oracle.com/technetwork/java/javaee/overview/index.html) (http://en.wikipedia.org/wiki/Application_server) – Jorge Nov 16 '14 at 05:38
  • so obviously I can write a Hello World program, and it doesn't need a `Container` per Java EE specifications. But if I want to use a Servlet, which is a Java EE idea, then it needs a proper Java EE container -- is that correct? And then I suppose that Servlet's are the de-facto standard for handling web requests for Java -- or is it the Container that'd really handling the web requests? – Don Cheadle Nov 16 '14 at 06:08
  • Actually, before Tomcat there was Apache/JServ and mod_jserv, which allowed for servlets to be run in a very similar-to-PHP kind of way. – jimjag Oct 02 '17 at 20:45