What is Java J2EE (Java Enterprise Edition)? Full Explanation

TechLoons
2 min readJul 16, 2023

--

Photo by Caspar Camille Rubin on Unsplash

Java Platform, Enterprise Edition (Java EE), now known as Jakarta EE, is a collection of specifications and APIs that provide a platform for developing and deploying enterprise applications in Java. It offers a set of standardized technologies and components for building scalable, reliable, and secure distributed systems.

Here’s an example that demonstrates the use of Java EE technologies, such as Servlets and JSP, to create a simple web application:

  1. Create Servlet
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("/HelloServlet")
public class HelloServlet extends HttpServlet {
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
response.setContentType("text/html");
response.getWriter().println("<h1>Hello, Servlet!</h1>");
}
}

2. Create a JSP (JavaServer Pages) file named “index.jsp”:

<html>
<head>
<title>Simple Web App</title>
</head>
<body>
<h1>Welcome to our Simple Web App!</h1>
<p>Click the button below to be greeted by a servlet:</p>
<form action="HelloServlet">
<input type="submit" value="Greet me!">
</form>
</body>
</html>

3. Create a deployment descriptor file named “web.xml” under the “WEB-INF” directory:

<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_4_0.xsd"
version="4.0">
<display-name>SimpleWebApp</display-name>

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

<servlet-mapping>
<servlet-name>HelloServlet</servlet-name>
<url-pattern>/HelloServlet</url-pattern>
</servlet-mapping>

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app>
  1. Build and deploy the web application in a Java EE-compatible server, such as Apache Tomcat. After deployment, you can access the application by navigating to http://localhost:8080/YourAppName.

In this example, we have a simple web application consisting of a Servlet (HelloServlet) and a JSP file (index.jsp). The HelloServlet class extends HttpServlet and responds to HTTP GET requests by writing a simple HTML response to the client. The index.jsp file presents a form that triggers the servlet when the "Greet me!" button is clicked.

The web.xml file serves as the deployment descriptor, defining the servlet and its mapping, as well as the welcome file to be displayed when accessing the root URL of the application.

When you access the web application, the index.jsp page is displayed. Clicking the "Greet me!" button submits a request to the HelloServlet, which responds with the greeting message.

Note: Since Java EE 6, the use of annotations (@WebServlet) allows servlets to be declared directly in the Java class, eliminating the need for explicit configuration in the deployment descriptor (web.xml). However, in the example above, the web.xml file is provided for completeness and compatibility with older versions of Java EE.

--

--

TechLoons
TechLoons

Written by TechLoons

Welcome to TechLoons, your go-to source for the latest tips and information on a wide range of topics.

No responses yet