🚀 Master Java & Backend Engineering!
Enjoying practical, step-by-step Java tutorials? Subscribe to Ram N Java for more high-quality developer guides!
🔔 Subscribe to Ram N JavaIntroduction: Building a Hello World Servlet
Writing your first web application using Java Servlets is a foundational milestone for any backend Java developer. A classic Hello World Servlet demonstrates the entire request-response cycle: starting from an HTML page, dispatching through an HTTP request, mapping inside web.xml, and handling the logic inside a Java Servlet class.
Step 1: Setting Up the Front-End (index.html)
The application entry point is a straightforward HTML page containing a hyperlink pointing to our servlet endpoint:
<html>
<body>
<h2>Hello World Servlet Application</h2>
<a href="hello">Click Here for Hello World</a>
</body>
</html>
When a user clicks on this link, a GET request targeting /hello is sent to the web server.
Step 2: Configuring web.xml (Deployment Descriptor)
The Servlet Container (such as Apache Tomcat) uses web.xml to register the servlet and match incoming URLs to the correct Java class:
<web-app>
<servlet>
<servlet-name>HelloWorld</servlet-name>
<servlet-class>com.ram.servlet.HelloWorldServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>HelloWorld</servlet-name>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
</web-app>
- <servlet-name>: A unique alias linking the servlet declaration to its mapping.
- <servlet-class>: The fully qualified name of your Java Servlet class.
- <url-pattern>: The URI pattern that triggers this servlet.
Step 3: Writing the HelloWorldServlet Class
Our custom servlet extends HttpServlet and overrides the key lifecycle methods:
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class HelloWorldServlet extends HttpServlet {
private String message;
@Override
public void init() throws ServletException {
// Initialization code executed once
message = "Hello World";
}
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException {
// Set response content type to HTML
res.setContentType("text/html");
// Write HTML output directly to client
PrintWriter out = res.getWriter();
out.println("<h1>" + message + "</h1>");
}
@Override
public void destroy() {
// Cleanup resources before servlet is removed
}
}
Step 4: Packaging and Deploying to Apache Tomcat
- Export WAR File: Export your web project as a
.warfile (e.g.,helloworld.war). - Deploy: Copy the WAR file directly into the
webappsdirectory of your Apache Tomcat installation. - Start Server: Launch Tomcat (
startup.batorstartup.sh). Tomcat automatically unpacks the archive. - Run: Access your application in the browser at
http://localhost:8080/helloworld/and click the link!
Recommended Related Tutorials
Continue your Java web development journey with these recommended tutorials from Ram N Java:
Click here to watch in Youtube : https://www.youtube.com/watch?v=aZxsKLT4vBI
Click the below Image to Enlarge
Helloworld Project Dir Structure
| Servlets : Helloworld Example |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 | import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; // Extend HttpServlet class public class HelloWorld extends HttpServlet { private static final long serialVersionUID = 1L; private String message; public void init() throws ServletException { // Do required initialization message = "Hello World"; } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Set response content type response.setContentType("text/html"); // Actual logic goes here. PrintWriter out = response.getWriter(); out.println("<h1>" + message + "</h1>"); } public void destroy() { // do nothing. } } |
Web.xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | <?xml version="1.0" encoding="ISO-8859-1"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0" metadata-complete="true"> <display-name>HelloWorld Application</display-name> <description> This is a simple web application with a source code organization based on the recommendations of the Application Developer's Guide. </description> <servlet> <servlet-name>HelloWorld</servlet-name> <servlet-class>HelloWorld</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloWorld</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app> |
index.html
1 2 3 4 5 6 7 8 9 10 11 12 13 | <!DOCTYPE HTML><html lang="en"><head> <meta charset="UTF-8"> <title>Helloworld Example</title> </head> <body> <p> <h3>Helloworld Example</H3> <p></p> <ul> <li><a href="hello">Helloworld</a></li> </ul> </body></html> |
To Download HelloworldApp Project Click the below link
https://sites.google.com/site/javaee4321/servlets/HelloworldApp.zip?attredirects=0&d=1
No comments:
Post a Comment