title: Passing parameters from JSP page to a java method to persist to a database
body: <p>I am trying to create a registration JSP page which when submit is clicked I would like it to pass the information entered to an addUser method in a java class</p>
  
  <p>This is my JSP Page:</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
       pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org /TR/html4/loose.dtd"&gt;
  &lt;html&gt;
  &lt;head&gt;
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  &lt;title&gt;Registration&lt;/title&gt;
  &lt;/head&gt;
  &lt;body&gt;
  &lt;center&gt;
              &lt;h2&gt;Signup Details&lt;/h2&gt;
              &lt;form action="RegisterUser.java" method="post"&gt;
              &lt;br/&gt;Username:&lt;input type="text" name="username"&gt;
              &lt;br/&gt;Password:&lt;input type="password" name="password"&gt;
              &lt;br/&gt;Email:&lt;input type="text" name="email"&gt;
  
              &lt;%= String name = request.getParameter("username");
                  String pass = request.getParameter("password");
                  String email = request.getParameter("email");
                  User u = new User();
                  u.setName(name);
                  u.setPassword(pass);
                  u.setEmail(email);%&gt;
  
              &lt;br/&gt;&lt;input type="submit" value="Submit" action="RegisterUser.addUser(u)"&gt;
              &lt;/form&gt;
          &lt;/center&gt;
  
  &lt;/body&gt;
  &lt;/html&gt;
  </code></pre>
  
  <p>When Submit is clicked I want to add the details to a database using the following method</p>
  
  <pre><code>public class RegisterUser {
       private static final String PERSISTENCE_UNIT_NAME = "User";
       private static EntityManagerFactory factory;
       private User user;
  
       public void addUser(User user){
           factory = Persistence.createEntityManagerFactory(PERSISTENCE_UNIT_NAME);
           EntityManager em = factory.createEntityManager();
           // Create new user
           em.getTransaction().begin();
           em.persist(user);
           em.getTransaction().commit();
           em.close();
  </code></pre>