Forms With JSP and Servlets: Handle the POST and Validate on the Server
Post a sign-up form straight to a JSP and anything gets accepted. Post it to a servlet instead: doPost checks name and email, forwards back with errors and the typed values, or on to the welcome page.
What you will learn
- Choose GET or POST for a form and see where the values travel
- Read posted fields with getParameter in a JSP and in doPost
- Validate fields in a servlet and forward back with a list of errors
- Keep what the user typed in the form after a failed submit
Before you start
- Servlet Request Parameters, and Forward vs Redirect
- MVC and the Front Controller With Servlets and JSP
A form sends each field by its name. You can post it straight to a JSP or to a servlet, and the difference shows up the moment someone submits it empty. This article builds a small sign-up form both ways.
GET or POST?
With method="get" the values travel in the URL. Submit Olivia Brown's details and the address bar shows name=Olivia+Brown and the email too. With method="post" they go in the request body, and the address bar only shows /welcome.jsp. The page says "Welcome, Olivia Brown" either way.
GET is for looking something up. A form that sends data uses POST, because GET puts the values where anyone can see them.
Way 1: post straight to a JSP
<!DOCTYPE html>
<html>
<body>
<h1>Sign up</h1>
<!-- the browser POSTs each field, by its name, to welcome.jsp -->
<form action="welcome.jsp" method="post">
<p>Name: <input name="name"></p>
<p>Email: <input name="email"></p>
<button>Sign up</button>
</form>
</body>
</html>
Plain HTML in the public webapp folder. The action is welcome.jsp, the method is post, and each input's name is the key the server reads. A button inside a form submits it.
<%@ page contentType="text/html;charset=UTF-8" %>
<%-- a JSP can read the posted fields straight from the request --%>
<% String name = request.getParameter("name"); %>
<html>
<body>
<h1>Welcome, <%= name %>!</h1>
<p>We will write to <%= request.getParameter("email") %>.</p>
</body>
</html>
Welcome, Olivia Brown!
We will write to olivia@example.com.
It works, but there's no validation. Submit it empty and you get "Welcome, !" with nothing after the comma. A blocked email address would be accepted too, because the JSP shows whatever arrives.
Way 2: post to a servlet that checks
package org.studyeasy.web;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
// The form posts here: the SERVER checks every field
@WebServlet("/signup")
public class SignupServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp)
throws ServletException, IOException {
String name = req.getParameter("name").trim();
String email = req.getParameter("email").trim();
List<String> errors = new ArrayList<>();
if (name.isEmpty()) {
errors.add("Name is required");
}
if (email.isEmpty()) {
errors.add("Email is required");
} else if (!email.contains("@")) {
errors.add("Email must contain @");
}
if (errors.isEmpty()) { // valid: welcome.jsp reads the same request
req.getRequestDispatcher("/welcome.jsp").forward(req, resp);
} else { // invalid: back to the form, with the errors
req.setAttribute("errors", errors);
req.getRequestDispatcher("/signup.jsp").forward(req, resp);
}
}
}
- It's
doPost, notdoGet, because the form's method is post. - The fields are read with
getParameter, same as before. - A list collects the errors: name required, email required, email must contain
@. - No errors: forward to
welcome.jsp. Errors: put them on the request and forward back tosignup.jsp.
Errors shown, values kept
<%@ page contentType="text/html;charset=UTF-8" %>
<%@ page import="java.util.List" %>
<%-- a failed POST is forwarded back here with "errors" --%>
<% List<String> errors = (List<String>) request.getAttribute("errors");
String name = request.getParameter("name");
String email = request.getParameter("email"); %>
<html>
<body>
<h1>Sign up</h1>
<% if (errors != null) { %>
<ul style="color: red">
<% for (String error : errors) { %>
<li><%= error %></li>
<% } %>
</ul>
<% } %>
<form action="signup" method="post">
<%-- keep what the user typed; nothing on the first visit --%>
<p>Name: <input name="name" value="<%= name == null ? "" : name %>"></p>
<p>Email: <input name="email" value="<%= email == null ? "" : email %>"></p>
<button>Sign up</button>
</form>
</body>
</html>
The page reads the errors attribute and prints one red list item per error. The value attributes put back what was typed, so the user doesn't start again. The form's action is signup, so it posts to the same servlet.
Three submits, three results:
name="", email="" -> Name is required, Email is required
name="Liam Carter", email="liam.example.com" -> Email must contain @
name="Liam Carter", email="[email protected]" -> Welcome, Liam Carter!
The address bar says /signup every time, because the servlet forwards to the page that answers.
Which method answers?
Submitting the form is a POST, so doPost runs and you get 200. Type /signup into the address bar instead and the browser sends a GET. This servlet has no doGet, so Tomcat answers 405 Method Not Allowed. If you want /signup to show the form on a GET, add a doGet that forwards to signup.jsp; an AI assistant writes that in seconds.
Post to a JSP or to a servlet?
| Post to a JSP | Post to a servlet | |
|---|---|---|
| Form action | welcome.jsp | signup |
| Fields read in | the page | doPost |
| Empty submit | "Welcome, !" | two errors on the form |
| Checks live in | the same file as the HTML | Java |
A JSP is fine for a quick demo. For every real form, post to a servlet, check, and forward to the JSP that shows the result.
Common mistakes
- Renaming
doPosttodoGet. The form still posts, so Tomcat answers 405. - Leaving
name="email"off an input. The browser doesn't send that field, and the page says "We will write to null." getParameter("Name")with a capital N. Field names are case-sensitive, so you get "Welcome, null!"value="<%= name %>"without the null check. On the first visit there's no parameter yet, and the box shows the word null.
Quick recap
- Forms that send data use POST; each field travels under its
name. - Read fields with
getParameterindoPost, collect errors in a list. - Forward back to the form with errors and the typed values, or on to the result page.
StudyEasy AI Community
Learn AI engineering, share what you build, and connect with other learners.
Free resources: lesson files, notebooks and code
Ask questions and join engineering discussions
Meet real people learning and building with AI
Related course on Udemy
FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE
Java 25, JSP, Hibernate, Spring Boot 4, REST APIs, React and Spring AI.View on Udemy ↗: FULL STACK JAVA : JAVA + JSP + SPRING BOOT AI + REACT + VIBE (paid course, opens in a new tab)Related posts
MVC and the Front Controller With Servlets and JSP
A Book model, a BookController servlet and a books.jsp view behind WEB-INF show MVC in plain Java. Then one servlet on /app/* becomes a front controller, the idea Spring's DispatcherServlet grew from.
Mini App With Servlets and JSP: Login, Logout and a Shared Web Template
Four pages and one template: a login form that posts to a servlet, a session that remembers the user, a filter that guards home.jsp and account.jsp, and a logout that ends it, plus when each step forwards or redirects.
SQLite and a JDBC Connection Pool in a Java Web App
A whole database in one file, studyeasy.db, and a Java web app that talks to it: one HikariCP pool, a @WebListener that opens it and runs schema.sql at startup, and a /db-check servlet that borrows a connection.
Users CRUD With Servlets and JSP, Part 1: Add and List Through a DAO
Fill an empty users table from a form and list every row: UserController picks the page, UserDao holds all the SQL, SaveUserServlet inserts and redirects, and a JSTL table shows the rows. Plus why every value goes in as a ?.
