title: Java Serialized Class Objects with Java Servlets
body: <p>I currently have a class project where I need to deploy a simple banking web application on Tomcat. Upon opening up the application, we should be brought to an HTML file that contains a form for users to submit a username OR for them to create a new account. This must be done by using Java servlets where the mapping is done in a web.xml. </p>
  
  <p>The part where I am having trouble with is incorporating serializable objects. In my case, the serializable object class is Accounts. In the servlets, I need to know how to manipulate them (create, store, delete, and retrieve).</p>
  
  <p>Here is the Account.java that I'm referencing. 
  <a href="https://i.stack.imgur.com/m94dR.png" rel="nofollow noreferrer">Account.java</a></p>
  
  <pre><code>//Class from which objects will be created 
  //and then passed as arguments.
  
  import java.io.Serializable;
  
  public class Account implements Serializable
  {
          private int acctNum;
          private String surname;
          private String firstNames;
          private double balance;
  
          public Account(int acctNo, String sname, String fnames, double bal)
          {
                  acctNum = acctNo;
                  surname = sname;
                  firstNames = fnames;
                  balance = bal;
          }
  
          public int getAcctNum()
          {
                  return acctNum;
          }
  
          public String getName()
          {
                  return (firstNames + " " + surname);
          }
  
          public double getBalance()
          {
                  return balance;
          }
  
          public double withdraw(double amount)
          {
                  if (amount &lt;= balance)
                          return amount;
                  else
                          return 0;
          }
  
          public void deposit(double amount)
          {
                  if (amount &gt; 0)
                          balance += amount;
          }
  }
  </code></pre>
  
  <p>And here is an example of a Java servlet I would be using.
  <a href="https://i.stack.imgur.com/J6QdJ.png" rel="nofollow noreferrer">FirstServlet.java</a></p>
  
  <pre><code>import java.io.*;
  import javax.servlet.*;
  import javax.servlet.http.*;
  
  public class FirstServlet extends HttpServlet
  {
      public void doPost(HttpServletRequest request,
                          HttpServletResponse response)
              throws IOException,ServletException
      {
          response.setContentType("text/HTML");
  
          String user = request.getParameter("usrname");
          PrintWriter out = response.getWriter();
          /*
          out.println("&lt;HTML&gt;");
          out.println("&lt;HEAD&gt;");
          out.println("&lt;TITLE&gt;Simple Servlet&lt;/TITLE&gt;");
          out.println("&lt;/HEAD&gt;");
          out.println("&lt;BODY&gt;");
          out.println("&lt;BR&gt;&lt;BR&gt;&lt;BR&gt;");
          out.println("&lt;CENTER&gt;&lt;H1&gt; A Simple Servlet &lt;/H1&gt;&lt;/CENTER&gt;");
          out.println("&lt;/BODY&gt;");
          out.println("&lt;/HTML&gt;");
          */
          out.print("&lt;html&gt;&lt;head&gt;&lt;title&gt;Simple Servlet&lt;/title&gt;&lt;/head&gt;&lt;body&gt;&lt;br&gt;&lt;br&gt;&lt;br&gt;&lt;CENTER&gt;&lt;H1&gt; A Simple Servlet &lt;/H1&gt;&lt;br&gt;&lt;br&gt;Username: "+ user +"&lt;/CENTER&gt;&lt;/body&gt;&lt;/html&gt;");
          out.flush();
      }
  }
  </code></pre>