Explain web server and application server

Explain web server and application server

A web server and an application server are both crucial components in the context of web applications, but they serve different purposes and have distinct functionalities.

Web Server

A web server is responsible for handling HTTP requests from clients (usually web browsers) and serving web content (such as HTML pages, images, CSS files, and JavaScript files). It primarily focuses on serving static content, though many web servers can also handle dynamic content generation through server-side scripts.

  1. Main Function: Serve static content and handle HTTP requests and responses.
  2. Examples: Apache HTTP Server, Nginx, Microsoft Internet Information Services (IIS).

Application Server

An application server provides a platform for running server-side applications. It not only serves web content but also provides a runtime environment for executing business logic. Application servers typically offer additional services such as transaction management, security, load balancing, and connection pooling, which are essential for building and running dynamic, enterprise-level applications.

  • Main Function: Host and execute business logic and provide services like transaction management, security, and scalability.
  • Examples: Apache Tomcat, JBoss, WebLogic, IBM WebSphere.

web server and application server

Key Differences web server and application server

1. Primary Focus

  •  Web Server: Focuses on serving static content and handling HTTP requests.
  •  Application Server: Focuses on executing business logic and providing additional services for dynamic content generation.

2. Content Served

  • Web Server: Serves static content (HTML, CSS, JS, images).
  • Application Server: Serves dynamic content by executing server-side applications.

3. Complexity

  • Web Server: Simpler, with less overhead, primarily handling HTTP requests and responses.
  • Application Server: More complex, providing a runtime environment and additional services for server-side applications.

4. Scalability and Management

  1.   Web Server: Typically lighter and faster for serving static content.
  2.   Application Server: Provides built-in features for scalability, transaction management, and security.

Example in web server and application server

Let’s illustrate the usage of a web server (Apache Tomcat) as a servlet container and an application server (JBoss EAP) for deploying a Java EE application.

Web Server Example (Apache Tomcat)

  • Servlet Deployment in Apache Tomcat:   
    • Create a simple servlet.

servlet
java
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/hello")
public class HelloServlet extends HttpServlet {
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        response.getWriter().write("<h1>Hello, World!</h1>");
    }
}

Example
Web.xml Configuration (if not using annotations):
   - Place the servlet configuration in WEB-INF/web.xml.

xml
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                             http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">

    <servlet>
        <servlet-name>HelloServlet</servlet-name>
        <servlet-class>HelloServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>HelloServlet</servlet-name>
        <url-pattern>/hello</url-pattern>
    </servlet-mapping>
</web-app>

Servlet to Call EJB:
   - Create a servlet to call the EJB.

java
import java.io.IOException;
import javax.ejb.EJB;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet("/helloEjb")
public class HelloServletEjb extends HttpServlet {
    @EJB
    private HelloBean helloBean;

    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html");
        response.getWriter().write("<h1>" + helloBean.sayHello() + "</h1>");
    }
}

Example
3. Deploying to Tomcat:

Package the application as a WAR file and deploy it to the Tomcat webapps directory.

Application Server Example (JBoss EAP)
1. EJB Deployment in JBoss EAP:
   - Create a simple stateless EJB.

java
import javax.ejb.Stateless;

@Stateless
public class HelloBean {
    public String sayHello() {
        return "Hello, World!";
    }
}

  1. Deploying to JBoss EAP:
    • Package the application as an EAR or WAR file and deploy it to the JBoss EAP deployments directory.

Homepage

Readmore