Good Day,

When I run my servlet in my web browser, everything runs fine ... until i press one of the option buttons, Deposit, Withdraw or Balance, then i get the following error:


type Status report

message /servlet/HTMLBank

description The requested resource (/servlet/HTMLBank) is not available.

Here is the code as well. What am I missing? Why are my buttons not working?

/*
	Chapter 12:	Question 4: Creating HTML Forms in JAVA (ASS: 78983D-G)
	Programmer:	Suret Delport (Student No. 10007349767)
	Date:		20 June 2009
	Filename:	HTMLBank.java
	Purpose:	Servlet that uses HTML to get input from user, determines balance and then adds or
				subtracts from the balance and lastly calculates the total balance again.
*/

import javax.servlet.*;
import javax.servlet.http.*;

import java.io.*;
import java.util.*;
import java.net.*;
import java.text.DecimalFormat;

public class HTMLBank extends HttpServlet
{
	private Account act;
	public DecimalFormat pattern = new DecimalFormat("$#,000.00");// To set a pattern for display
	public double actBalance = 10000.00;                          // initial display value
	public String displayBalance = pattern.format(actBalance);
	double entered = 0.0;

	public void init(ServletConfig conf)throws ServletException
	{
		super.init(conf);
		act = new Account();

	}
	public void  doGet(HttpServletRequest req, HttpServletResponse res)
                      throws ServletException, IOException
    {
	    res.setContentType("text/html");
	    PrintWriter out = res.getWriter();

	    res.setHeader("Expires","Tues, 01 Jan 1980 00:00:00 GMT");

	    HttpSession session = req.getSession(true); // get current session
		if(entered >= 0.0)
	    {
			out.println("<html>");
			out.println("<head>");
			out.println("<title>Online Bank 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>Banking 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>");
		}
		else
		{
			out.println("<HTML>");
			out.println("<HEAD>");
			out.println("<TITLE>Incorrect Input Warning..</TITLE>");
			out.println("</HEAD>");
			out.println("<BODY>");
			out.println("<H2>YOU HAVE ENTERED AN INCORRECT AMMOUNT!!!</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>");
		}
	}
	public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
	{
		try
		{
			entered = Double.parseDouble(req.getParameter("ENTRY"));
		}
		catch(NumberFormatException e)
		{
			entered = 0.0;
		}

		synchronized(act)
		{
			if((req.getParameter("WITHDRAW")!=null) && (entered < actBalance))
			{
				actBalance = actBalance - entered;
				entered = 0.0;			// reset value so that accidental presses on button dont affect total
			}
			if((req.getParameter("DEPOSIT")!=null) && (entered > 0))
			{
				actBalance = actBalance + entered;
				entered = 0.0;			// reset value so that accidental presses on button dont affect total
			}
			if(req.getParameter("BALANCE")!=null)
			{
				displayBalance = pattern.format(actBalance);
			}
		}
		doGet(req, res);
	}
	class Account
	{
		public int balance;
	}
}

> When I run my servlet in my web browser, everything runs fine ...
> until i press one of the option buttons, Deposit, Withdraw or
> Balance, then i get the following error:

This is because HTML doesn't know anything about your servlet project. Is the name of your project "servlet"? If no, then you need to specify the name of your Servlet project since you are specifying the absolute URL in the `action' attribute of your FORM.

BTW, the reason for the error is pretty simple; there is no resource mapped to the URL "/servlet/HTMLBank". How do you originally access the servlet which is working? How is your web.xml configured?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.