Getting Started: Handling Form Data

Learn how to create a HTML form that uses the HTTP POST method to submit user-provided data to a Java Servlet running on App Engine.

Before you begin

Configure your development environment and create your App Engine project.

Creating a form

Create a HTML form using JavaServer Pages (JSP).

The following JSP code generates a form to create a blog post. It takes three text input fields, title, author and content and has a Save button that submits the data to the servlet:

<%@ taglib uri="" prefix="c"%>
<%@ taglib uri="" prefix="fn"%>
<div class="container">
    Create a new blog post

  <form method="POST" action="/create">

      <label for="title">Title</label>
      <input type="text" name="title" id="title" size="40" value="${fn:escapeXml(blog.title)}" class="form-control" />

      <label for="author">Author</label>
      <input type="text" name="author" id="author" size="40" value="${fn:escapeXml(}" class="form-control" />

      <label for="description">Post content</label>
      <textarea name="description" id="description" rows="10" cols="50" class="form-control">${fn:escapeXml(blog.content)}</textarea>

    <button type="submit">Save</button>

This form sends the form data to the handler at the /create URL. You should use JSP's escapeXml functionality to counter cross-site scripting (XSS) attacks, as shown in the code snippet. Instructions for creating the form handler are shown next.

Handling form data

Once the user submits the form data, it is handled by a form handler. In the snippet below, the form handler is a servlet named createBlogPost:

import javax.servlet.annotation.WebServlet;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@WebServlet(name = "createBlogPost", value="/create")
public class createBlogPost extends HttpServlet {

  public void doPost(HttpServletRequest req, HttpServletResponse resp)
      throws ServletException, IOException {

    PrintWriter out = resp.getWriter();

        "Article with the title: " + req.getParameter("title") + " by "
            + req.getParameter("author") + " and the content: "
            + req.getParameter("description") + " added.");

Note the annotation @WebServlet(name = "createBlogPost", value="/create"), which maps the servlet to the URI where it handles requests: /create. For developers familiar with the App Engine Java 7 runtime, this annotation replaces the mappings that were previously made in the web.xml file.

Deploying to App Engine

You can deploy your app to App Engine using Maven.

Go to the root directory of your project and type:

mvn appengine:deploy

After Maven deploys your app, open a web browser tab automatically at your new app by typing:

gcloud app browse

What's next

As an extension of handling user data, you might want to use Cloud SQL, Cloud Datastore, or Cloud Storage to store the data:

Send feedback about...

App Engine standard environment for Java