Forms and JSP

We’ll be working with forms in this JSP Tutorial. Forms are used to allow a user to input data. In this tutorial we’ll have a JSP with a form that gives the possibility to enter a first name, last name and username. Next, we’ll save the data in a Bean, en at the end we’ll show the data.

Step 1: create the jsp with the form. Forms are very handy tools and there’s a lot of options within JSP to show forms. In this tutorial we’ll cover the basic html-forms:

<%@ page contentType="text/html;charset=UTF-8" %>
<html>
  <head><title>Simple form</title></head>
  <body>
    <form action="processform.do" method="POST">
        First name: <input type="text" name="firstname" />
        Last name: <input type="text" name="lastname" />
        Username: <input type="text" name="username" />
        <input value="Process" />
    </form>

  </body>
</html>

This looks like this:

First name:

Last name:

Username:

This is a very basic HTML form, anyone that has ever worked with forms in HTML understands this. The real question is how to let the servlet process this data. Let’s first create a web.xml file, and after that we’ll take a closer look at the servlet.

<servlet>

<servlet-name>FormServlet</servlet-name>

<servlet-class>servlets.FormServlet</servlet-class>

</servlet>

<servlet-mapping>

<servlet-name>FormServlet</servlet-name>

<url-pattern>/processform.do</url-pattern>

</servlet-mapping>

And the servlet:

public class FormServlet extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
        HttpSession session = request.getSession();
        FormDataBean formDataBean = ((FormDataBean)session.getAttribute("formDataBean"));
        if(formDataBean == null)
            formDataBean = new FormDataBean();
        formDataBean.setFirstname(request.getParameter("firstname"));
        formDataBean.setLastName(request.getParameter("lastname"));
        formDataBean.setUsername(request.getParameter("username"));

        session.setAttribute("formDataBean", formDataBean);

        response.sendRedirect("showdata.jsp");
    }
}

Instead of using doGet we use doPost. It is because in the form we created earlier we told the form to use POST instead of GET.

In the doPost method we request the Bean from the session. The we enter the values inside the bean using the “request.getParameter” method. And at the end we’ll redirect to the showdata JSP.

The Bean looks like this:

public class FormDataBean implements Serializable {
    private String firstName;
    private String lastName;
    private String username;

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }
}

In the next tutorial we’ll show how it is done without using a servlet.