I have an assignment with 1 piece of code. The assignment is in 3 parts. I have answered all 3 but I am not sure if I understand the questions correctly. I want to know if I can post the assignment question with the code. Can someone then read the question - look at the code and just tell me if I have understood correctly or if I need to start over.
Question:
(See Creating HTML Forms in Java ref below as assign4)Modify the program in Assign4 to use session tracking to store the balance as a session attribute. Save the program as SessionBank.java. Use a local variable for the account balance, removing the instance variable.
Do not use synchronization. Recall that session attributes are stored as Objects and must be downcast. Simultaneously test two threads as was done in Assign4. Do not include a delay in the code for a deposit. Write a short paragraph that explains why the two threads do not interfere with each other. HINT: Because a Double object cannot be modified, use the Double method, doubleValue(), to return an intrinsic double value.
My Code:
import javax.servlet.*; import javax.servlet.http.*; import java.io.*; import java.util.*; import java.net.*; import java.text.DecimalFormat; public class SessionBank extends HttpServlet { private Account act; public DecimalFormat pattern = new DecimalFormat("$#0,000.00"); // To set a patter for display //public Double actBalance = 0.00; // Change from an Instance Variable to local variable in line 41 public void init(ServletConfig conf)throws ServletException { super.init(conf); act = new Account(); } public void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { doPost(req,res); } public void doPost(HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException { //Makes instance variable a local variable double actBalance = 0.00; String displayBalance = pattern.format(actBalance); double entered = 0.0; //set MIME type of content returned to browser res.setContentType("text/html"); PrintWriter out = res.getWriter(); res.setHeader("Expires","Tues, 01 Jan 1980 00:00:00 GMT"); //Get the current session and set Attribute HttpSession session = req.getSession(true); session.setAttribute("actBalance", actBalance); // had it like this new ("actBalance", Double(actBalance)) //Starts outputting the HTML form out.println("<html>"); out.println("<head>"); out.println("<title>Online Bank ATM Simulator</title>"); out.println("</head>"); out.println("<body>"); out.println("<form method=POST action=/servlet/HTMLBank>"); out.println("<center>"); out.println("<hr>"); out.println("<h1>Bank ATM Simulation</h1><br>"); out.println("Amount: <input type=text name=ENTRY size=20 maxlength=15><br><br>"); out.println("Balance:"+displayBalance+"<br><br>"); out.println("<input type=submit name=DEPOSIT value=\"Deposit\"> "); out.println("<input type=submit name=WITHDRAW value=\"Withdraw\"> "); out.println("<input type=submit name=BALANCE value=\"Balance\"> <br><br>"); out.println("<hr>"); out.println("</form>"); out.println("</table>"); out.println("</body>"); out.println("</html>"); try { entered = Double.parseDouble(req.getParameter("ENTRY")); } catch(NumberFormatException e) { entered = 0.0; } if((req.getParameter("WITHDRAW")!=null) && (entered < actBalance) && (entered > 0)) { actBalance = actBalance - entered; doGet(req, res); } else if((req.getParameter("DEPOSIT")!=null) && (entered > 0)) { actBalance = actBalance + entered; doGet(req, res); } else if(req.getParameter("BALANCE")!=null) { displayBalance = pattern.format(actBalance); doGet(req, res); } else { //Output rest(error) of HTML out.println("<HTML>"); out.println("<HEAD>"); out.println("<TITLE>Incorrect Input Warning..</TITLE>"); out.println("</HEAD>"); out.println("<BODY>"); out.println("<H1>Invalid Entry</H1>"); out.println("<H2>You have entered an incorrect amount!!!</H2>"); out.println("<H3>You must enter a number only and it must be greater than 0.0</H3>"); out.println("</from>"); out.println("</BODY>"); out.println("</HTML>"); } } class Account { public int balance; } }
Error that I also receive:
Exception report
Description: The server encountered an internal error () that prevented it from fulfilling this request.
java.lang.NullPointerException
sun.misc.FloatingDecimal.readJavaFormatString(Unkn own Source)
java.lang.Double.parseDouble(Unknown Source)
SessionBank.doPost(SessionBank.java:77)
SessionBank.doGet(SessionBank.java:33)
javax.servlet.http.HttpServlet.service(HttpServlet .java:689)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)
org.apache.catalina.servlets.InvokerServlet.serveR equest(InvokerServlet.java:419)
org.apache.catalina.servlets.InvokerServlet.doGet( InvokerServlet.java:133)
javax.servlet.http.HttpServlet.service(HttpServlet .java:689)
javax.servlet.http.HttpServlet.service(HttpServlet .java:802)