Learn how to create a Java Servlet and connect it with an HTML form. Step-by-step guide for beginners with code examples.
🚀Introduction
If you're diving into Java web development, servlets are one of the first things you’ll encounter. In this blog, we’ll walk through a simple example of how to create a servlet and connect it with an HTML form.
By the end, you’ll understand how to send data from an HTML page to a servlet and process it on the server.
🧠 What is a Java Servlet?
A Java Servlet is a Java class used to handle HTTP requests and responses. It runs on a Java EE-compliant server such as Apache Tomcat, GlassFish, WildFly, or Jetty. Servlets act as a bridge between your front-end (HTML) and back-end (Java logic), forming the foundation of Java-based web applications.
If you're just building simple web apps or learning servlets, Tomcat is perfect.
But if you're building enterprise applications, you might prefer GlassFish or WildFly.
🔄 How a Servlet Works
When a user fills out a form on your website and clicks Submit, this is what happens behind the scenes in a Java Servlet:
User submits a form (like typing their name).
The browser sends this form data as an HTTP request to the server (e.g., Tomcat).
The server (like Tomcat) receives the request and looks for the correct servlet to handle it. This is done by matching the URL in the request to the servlet’s configuration (in web.xml or @WebServlet).
Servlet container (Tomcat) creates two things:
HttpServletRequest: This object holds the data the user sent (like the name they typed in the form).
HttpServletResponse: This is used by the servlet to send the response (what the user will see after submitting the form).
The servlet is then called to process the request:
If the request is a GET (a simple page load), it calls doGet().
If it’s a POST (like submitting a form), it calls doPost().
Inside the doPost() (or doGet()), the servlet:
Reads the form data using request.getParameter().
Processes it (e.g., checks if the name is valid or stores it in a database).
Prepares a response, like a message, using response.getWriter().
The servlet sends back the response to the browser. The user sees the result (like "Hello, John!").
🛠️ Tools Required
- - NetBeans or Eclipse
- - Apache Tomcat (or use NetBeans with built-in Tomcat)
- - Java SDK
✍️ Step-by-Step Example
- Create the HTML Form Create a file named index.html:
TODO supply a title
TODO write content
This is shortform converter
Name:
- Create the Java Servlet Create a new Servlet named MyServlet.java:
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
*
* @author SERAJ
*/
public class MyServlet extends HttpServlet {
/**
* Processes requests for both HTTP GET and POST
* methods.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
protected void processRequest(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
response.setContentType("text/html;charset=UTF-8");
try (PrintWriter out = response.getWriter()) {
/* TODO output your page here. You may use following sample code. */
out.println("");
out.println("");
out.println("");
out.println("Servlet Servlet1");
out.println("");
out.println("");
String name = request.getParameter("username");
out.println("Hello, " + name + "!");
out.println("");
out.println("");
}
}
//
/**
* Handles the HTTP GET method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Handles the HTTP POST method.
*
* @param request servlet request
* @param response servlet response
* @throws ServletException if a servlet-specific error occurs
* @throws IOException if an I/O error occurs
*/
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
processRequest(request, response);
}
/**
* Returns a short description of the servlet.
*
* @return a String containing servlet description
*/
@Override
public String getServletInfo() {
return "Short description";
}//
}
Enter fullscreen mode
Exit fullscreen mode
Configure web.xml
Inside your project’s WEB-INF/web.xml file, add this:
MyServlet
MyServlet
MyServlet
/MyServlet
Enter fullscreen mode
Exit fullscreen mode
Run Your Project
Deploy the project on Tomcat.
Open index.html in your browser.Enter a name and click Submit.You should see:
👉 "Hello, [Your Name]!"🔧 Common Errors & Fixes✅ Conclusion
You’ve now built your first Java Servlet that connects to an HTML form! This is the foundation for building more advanced web apps using Java.