943,645 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Unsolved
  • Views: 2760
  • JSP RSS
Jun 30th, 2009
0

Error: Requested resource not available for Servlet

Expand Post »
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?

JSP Syntax (Toggle Plain Text)
  1. /*
  2. Chapter 12: Question 4: Creating HTML Forms in JAVA (ASS: 78983D-G)
  3. Programmer: Suret Delport (Student No. 10007349767)
  4. Date: 20 June 2009
  5. Filename: HTMLBank.java
  6. Purpose: Servlet that uses HTML to get input from user, determines balance and then adds or
  7. subtracts from the balance and lastly calculates the total balance again.
  8. */
  9.  
  10. import javax.servlet.*;
  11. import javax.servlet.http.*;
  12.  
  13. import java.io.*;
  14. import java.util.*;
  15. import java.net.*;
  16. import java.text.DecimalFormat;
  17.  
  18. public class HTMLBank extends HttpServlet
  19. {
  20. private Account act;
  21. public DecimalFormat pattern = new DecimalFormat("$#,000.00");// To set a pattern for display
  22. public double actBalance = 10000.00; // initial display value
  23. public String displayBalance = pattern.format(actBalance);
  24. double entered = 0.0;
  25.  
  26. public void init(ServletConfig conf)throws ServletException
  27. {
  28. super.init(conf);
  29. act = new Account();
  30.  
  31. }
  32. public void doGet(HttpServletRequest req, HttpServletResponse res)
  33. throws ServletException, IOException
  34. {
  35. res.setContentType("text/html");
  36. PrintWriter out = res.getWriter();
  37.  
  38. res.setHeader("Expires","Tues, 01 Jan 1980 00:00:00 GMT");
  39.  
  40. HttpSession session = req.getSession(true); // get current session
  41. if(entered >= 0.0)
  42. {
  43. out.println("<html>");
  44. out.println("<head>");
  45. out.println("<title>Online Bank Simulator</title>");
  46. out.println("</head>");
  47. out.println("<body>");
  48. out.println("<form method=POST action=/servlet/HTMLBank>");
  49. out.println("<center>");
  50. out.println("<hr>");
  51. out.println("<h1>Banking Simulation</h1><br>");
  52. out.println("Amount: <input type=text name=ENTRY size=20 maxlength=15><br><br>");
  53. out.println("Balance:"+displayBalance+"<br><br>");
  54. out.println("<input type=submit name=DEPOSIT value=\"Deposit\"> ");
  55. out.println("<input type=submit name=WITHDRAW value=\"Withdraw\"> ");
  56. out.println("<input type=submit name=BALANCE value=\"Balance\"> <br><br>");
  57. out.println("<hr>");
  58. out.println("</form>");
  59. out.println("</table>");
  60. out.println("</body>");
  61. out.println("</html>");
  62. }
  63. else
  64. {
  65. out.println("<HTML>");
  66. out.println("<HEAD>");
  67. out.println("<TITLE>Incorrect Input Warning..</TITLE>");
  68. out.println("</HEAD>");
  69. out.println("<BODY>");
  70. out.println("<H2>YOU HAVE ENTERED AN INCORRECT AMMOUNT!!!</H2>");
  71. out.println("<H3>You must enter a number only and it must be greater than 0.0</H3>");
  72. out.println("</from>");
  73. out.println("</BODY>");
  74. out.println("</HTML>");
  75. }
  76. }
  77. public void doPost(HttpServletRequest req, HttpServletResponse res)throws ServletException, IOException
  78. {
  79. try
  80. {
  81. entered = Double.parseDouble(req.getParameter("ENTRY"));
  82. }
  83. catch(NumberFormatException e)
  84. {
  85. entered = 0.0;
  86. }
  87.  
  88. synchronized(act)
  89. {
  90. if((req.getParameter("WITHDRAW")!=null) && (entered < actBalance))
  91. {
  92. actBalance = actBalance - entered;
  93. entered = 0.0; // reset value so that accidental presses on button dont affect total
  94. }
  95. if((req.getParameter("DEPOSIT")!=null) && (entered > 0))
  96. {
  97. actBalance = actBalance + entered;
  98. entered = 0.0; // reset value so that accidental presses on button dont affect total
  99. }
  100. if(req.getParameter("BALANCE")!=null)
  101. {
  102. displayBalance = pattern.format(actBalance);
  103. }
  104. }
  105. doGet(req, res);
  106. }
  107. class Account
  108. {
  109. public int balance;
  110. }
  111. }
Reputation Points: 10
Solved Threads: 0
Light Poster
suretd is offline Offline
31 posts
since Apr 2009
Jun 30th, 2009
0

Re: Error: Requested resource not available for Servlet

> 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?
Super Moderator
Featured Poster
Reputation Points: 3233
Solved Threads: 719
Failure as a human
~s.o.s~ is offline Offline
8,871 posts
since Jun 2006

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: how can i add css in jsp website in dreamwiever 8
Next Thread in JSP Forum Timeline: I can't insert data into database





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC