Single logo
  • Courses
  • Blog
  • About
Join on SkoolJoin on Skool

In this topic

JSP and Servlets
  • Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
  • Set Up Tomcat 11 and a Maven Web Project in IntelliJ IDEA
  • The Servlet Request Lifecycle: @WebServlet, web.xml, init, doGet and destroy
  • Servlet Request Parameters, and Forward vs Redirect
  • Run a Java Web App in Docker: Tomcat 11 With One Compose Command
  • Sessions, Cookies and Filters in Java Servlets: Log In Once, Stay Logged In
  • Servlets and JSP: What a Servlet Container Does and How a JSP Becomes a Servlet
  • JSP Scripting: Expressions, Scriptlets, Declarations, Comments and Includes
  • JavaBeans and JSP Scopes: Page, Request, Session and Application
  • JSP Expression Language Step by Step: ${...} for Beans, Maps, Lists, Params and Scopes
  • JSTL Core Tags: c:set, c:out, c:if, c:choose and c:forEach
  • JSTL Functions and Formatting: fn:length, fn:toUpperCase, fn:contains, fmt:formatNumber and fmt:formatDate
  • Maintaining and Migrating Legacy JSP With AI: Explain the Page, Refactor to a DAO, Servlet and JSTL, Then Move It to React
  • MVC and the Front Controller With Servlets and JSP
  • Forms With JSP and Servlets: Handle the POST and Validate on the Server
  • Mini App With Servlets and JSP: Login, Logout and a Shared Web Template
  • SQLite and a JDBC Connection Pool in a Java Web App
  • Users CRUD With Servlets and JSP, Part 1: Add and List Through a DAO
  • Users CRUD With Servlets and JSP, Part 2: Update and Delete
  1. Blog
  2. /
  3. JSP and Servlets
  4. /
  5. MVC

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.

Chaand Sheikh

·27 November 2026·Updated 28 November 2026MVC

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, not doGet, 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 to signup.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.

The browser POSTs to /signup. doPost runs the checks. If errors is not empty, it forwards back to signup.jsp with the errors and the typed values, and the user submits again. If errors is empty, it forwards to welcome.jsp. BrowserPOST /signup doPostcheck name, email signup.jsperrors + typed values welcome.jspWelcome, Liam Carter! errors no errors fix and submit again
A failed submit goes round again; only a clean submit reaches the welcome page.

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 JSPPost to a servlet
Form actionwelcome.jspsignup
Fields read inthe pagedoPost
Empty submit"Welcome, !"two errors on the form
Checks live inthe same file as the HTMLJava

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 doPost to doGet. 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 getParameter in doPost, collect errors in a list.
  • Forward back to the form with errors and the typed values, or on to the result page.

Keep going in JSP and Servlets · 15 of 19

Previous
MVC and the Front Controller With Servlets and JSP
Next post
Mini App With Servlets and JSP: Login, Logout and a Shared Web Template
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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 ?.

On this page

  • GET or POST?
  • Way 1: post straight to a JSP
  • Way 2: post to a servlet that checks
  • Errors shown, values kept
  • Which method answers?
  • Post to a JSP or to a servlet?
  • Common mistakes
  • Quick recap
Community · Free to join
Single logo

StudyEasy AI Community

Hosted onSkool

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

Join free on Skool

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)

Single logo

Empowering Education, One Course at a Time

About
About usContact us
Legal
Terms of UsePrivacy Policy

© 2026 StudyEasy Organization. All rights reserved.