Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Implementing Session Tracking

  1. #1
    Junior Member
    Join Date
    Sep 2013
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Implementing Session Tracking

    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)


  2. #2
    Forum VIP
    Join Date
    Jul 2010
    Posts
    1,676
    Thanks
    25
    Thanked 329 Times in 305 Posts

    Default Re: Implementing Session Tracking

    Where ever you are attempting to turn a String into a double, your String is null (not initialized). I believe it is occurring where you are calling req.getParameter("ENTRY")); A null String will not throw a NumberFormatException, it will throw a null pointer. Catching the NumberFormatException is a good idea (the API says that exception can be thrown), but you must also take into account the NullPointerException (also in the API). One way to handle it would be to add an additional catch statement to catch the NullPointer, but the "better" way of doing it would probably be to just do a null check on the String before calling the parseDouble method with it.
    NOTE TO NEW PEOPLE LOOKING FOR HELP ON FORUM:

    When asking for help, please follow these guidelines to receive better and more prompt help:
    1. Put your code in Java Tags. To do this, put [highlight=java] before your code and [/highlight] after your code.
    2. Give full details of errors and provide us with as much information about the situation as possible.
    3. Give us an example of what the output should look like when done correctly.

    Join the Airline Management Simulation Game to manage your own airline against other users in a virtual recreation of the United States Airline Industry. For more details, visit: http://airlinegame.orgfree.com/

Similar Threads

  1. Servlets and Session tracking cookie's name
    By angstrem in forum Java Theory & Questions
    Replies: 3
    Last Post: June 24th, 2013, 12:46 PM
  2. tracking hidden code
    By salesman in forum What's Wrong With My Code?
    Replies: 1
    Last Post: May 22nd, 2013, 02:38 AM
  3. atlas tracking and webtrend tracking
    By mrsreddy65@gmail.com in forum Java Theory & Questions
    Replies: 0
    Last Post: August 17th, 2011, 06:01 AM
  4. atlas tracking
    By mrsreddy65@gmail.com in forum Threads
    Replies: 3
    Last Post: August 16th, 2011, 04:54 AM