网络:网络服务器:Apache 和 Tomcat

Keywords

  • web server
  • application server
  • web container (also known as a servlet container)

总览

  • Apache is a more powerful general purpose web server

  • Tomcat = “servlet container + JSP environment + web server” (but primarily a servlet container)

    • 什么是web server?
      • A “Web server” is anything that serves files via the HTTP protocol.
      • Example: Web server [apache] process web client (web browsers) requests and forwards it to servlet container [tomcat] and container process the requests and sends response which gets forwarded by web server [apache] to the web client [browser].
      • You can write a very simple one in Java in a dozen lines of code (a server that serves a single fixed file, for example.) It’s very easy to write one, so there are many thousands in existence.


  • “Generate content when required” is exactly what “dynamic” means, and is the very opposite of “static”. The main purpose of the Apache HTTP Server is to serve static content, while the main purpose of Servlet technology is to generate content on-the-fly (dynamic content).


  • Apache + Tomcat VS Tomcat alone

    • Apache as web server, Tomcat as servlet container
    • When used together, Apache serves, Tomcat generates dynamic content when required.
      • If Apache is not used, Coyote in Tomcat will do the serving, while Catalina and Jesper are generating the dynamic content.
    • When used together, web browsers only “see” AWS(Apache Web Server) with no clue that Tomcat is working behind the curtains. AWS is a middle-man between the web browser clients and Tomcat.
      • If using Tomcat alone, then the Coyote module in Tomcat takes the place of AWS to field requests from web browser clients.


  • Apache VS Tomcat

    • You can integrate Tomcat with HTTPD if you really need to.
    • “A lot of us are just using Tomcat as a standalone server.”
    • If your primary business was running Perl CGIs, or PHP, or Ruby on Rails, or basically anything other than Servlets/JSPs, then using Apache is definitely preferable. Tomcat can do these things (or some of them, with a little work) but with Apache it’ll be easier to set up and much more efficient.
      • But my primary web site is fairly modest, with moderate traffic, and all JSP/Servlet/static HTML, and Tomcat works great for it.
    • Tomcat, on it’s own can’t bind to ports below 1024 on Unix systems unless it’s run as root. Apache binds to ports 80 and 443 as root and then downgrades itself to a user with lesser privileges.
      • A lot of people have integrated the two just so they could run on port 80 without Tomcat running as root.
    • Several years ago the performance difference between Apache and Tomcat significantly greater than it was now. Both Tomcat and the Java JVM have come a long way and the gap has narrowed quite a bit.
    • Maintenance cost
      • Apache is written in C. Tomcat is written in Java. The connectors are written with a combination of the two.
      • The people writing the connectors generally have to play catch up whenever either Tomcat or Apache roll out new releases. Keeping all three up to date can involve a lot of work and headaches.
      • Figure that cost in up front and decide if the performance increase (if there is one at all for your app) that you get by connecting the two is worth the extra effort.

Apache Tomcat

  • Tomcat is pure Java

  • Tomcat = ( Web Server [Coyote] + Servlet container [Catalina] + JSP engine[Jasper] )

    • [Catalina] Tomcat’s servlet container
      • Tomcat is primarily a servlet/ JSP container.
      • Tomcat is often used as a Servlet container behind Apache HTTP Server.
        • It serves requests to custom-built Java servlets or JSP files on your server. It is usually used in conjunction with the Apache HTTP server (at least in my experience). Use it to manually process incoming requests.
      • Catalina implements Sun Microsystems’ specifications for servlet and JavaServer Pages (JSP).
      • 什么是servlet?
        • A servlet, at the end, is a Java class.
        • Main purpose of Servlet technology is to generate content on-the-fly (dynamic content).
    • [Coyote] Tomcat’s web server.
      • Coyote is a Connector component for Tomcat that supports the HTTP 1.1 protocol as a web server.

      • Coyote listens for incoming connections to the server on a specific TCP port and forwards the request to the Tomcat Engine to process the request and send back a response to the requesting client.

        • Another Coyote Connector, Coyote JK, listens similarly but instead forwards its requests to another web server, such as Apache, using the JK protocol.This usually offers better performance.
      • Able to handle most small and medium web site needs. With support for Java NIO and 64-bit memory, even some larger scale web sites may be served well by Tomcat’s Coyote module. For various reasons, some folks choose to ignore Coyote and instead use Tomcat’s Servlet capabilities behind the Apache httpd web server product.

      • The web server is ON by default when you run Tomcat’s startup.sh script, listening on port 8080 for incoming connections (HTTP calls).

      • Is fully functional at serving static content too

      • The Apache HTTP Server (AHS), in contrast, has a completely separate implementation of a web server, using native C/C++ code. AHS is very flexible and powerful, but is often overkill. Tomcat’s own web server works very well. Use AHS only if truly needed.

    • [Jasper] Tomcat’s JSP Engine.
      • Jasper parses JSP files to compile them into Java code as servlets (that can be handled by Catalina). At runtime, Jasper detects changes to JSP files and recompiles them.


  • Designed for running server side programs written in Java and don’t use CGI

    • conflict opinion: Tomcat has a CGI servlet
    • Although it’s possible to get Tomcat to run Perl scripts and the like, you wouldn’t use Tomcat unless most of your content was Java.
    • 什么是CGI
      • CGI is a protocol that allows an HTTP server to use an external piece of software to determine how to respond to a request instead of simply returning the contents of a static file. Many HTTP servers support the CGI protocol.
      • You can use CGI without an HTTP server, but this typically has few uses beyond allowing a developer to perform command line testing of the CGI program. (You certainly can’t interact with it directly from a web browser).


  • Other information:

    • The predecessor to Tomcat was called “JServ” and was a module for Apache HTTPD.
      • The project is, for the most part, dead.
    • There are many other Servlet/JSP containers besides Tomcat, some quite popular such as Jetty, JBoss/Wildfly, Glassfish, WebSphere, and many more. While popular, Tomcat does not dominate, with only a minority of market share.

Apache HTTP Server (HTTPD - HTTP server Daemon )

  • A web server written in portable C

  • Apache is a general-purpose http server, which supports a number of advanced options that Tomcat doesn’t.

    • Mostly serves static content by itself, but there are many add-on modules (some of which come with Apache itself) that let it modify the content and also serve dynamic content written in Perl, PHP, Python, Ruby, or other languages.
    • Can’t run servlets/JSPs with Apache HTTPD
      • Probably someone, somewhere, has written a module that lets you do this, but I’m not aware of one, and even if it exists, it’s not widely used. That’s what Tomcat is for – or another servlet container.

  • Open source

    • Many entities use it to host their websites

References

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值