Need help in instantiating Servlet (J2EE)

vickybat

I am the night...I am...
Hello fellow tdf members. :)

I need some help from JAVA/J2EE developers and learners alike.

I'm improving my knowledge on J2EE and trying to make an application using the MVC design pattern.
The application is a simple income tax calculator and the MVC has a MODEL class, a VIEW (JSP) and a CONTROLLER (SERVLET).

At first, i'm making a simple one with no LISTENER or Context Parameters. My model class has a pure java class that does the computation of income tax.
The servlet controller handles the GET POST requests by the user, passes the request to the model which computes the income tax (or any business logic) and then the output is
shown in a JSP page.

I'm using apache tomcat server/container.

The problem is my servlet is failing to intialize. In my current logic, the browser request or the FORM has two select drop downs which
lets the user enter the "Taxable Amount" and the "AGE".

The servlet gets the parameters by calling the getParameter() method on the request object, calls the model to compute and sets the result
as an ATTRIBUTE for the JSP to display.

Remember there are no LISTENERS implemented yet. I don't understand why my Servlet is failing to initialize. I'm suspecting the getParameter() returns a string from the input,
which i'm parsing to double and int primitive types, is the issue. But haven't figured out anything.

Posting my code below in the following order:

1. HTML Form (Calls the doPost() Servlet service method )

PHP:
<!doctype html>

<html lang ="en">
<head>
	<meta charset = "utf-8"/>
	<title>Income Tax</title>
</head>

<body>
<h1 align = "center">Income Tax Calculator</h1>
<form method = "POST" action ="IncomeTax.do">
Select beer characteristics<p>
Assessment Year:

<select name ="year" size ="1">
<option value ="2013-2014">2013-2014</option>
<option value ="2012-2013">2012-2013</option>
<option value ="2011-2012">2011-2012</option>
<option value ="2010-2011">2010-2011</option>
</select>
<br></br>
Gender:

<select name ="Gender" size ="1">
<option value ="Male">Male</option>
<option value ="Female">Female</option>
<option value ="Senior Citizen">Senior Citizen</option>
</select>
<br></br>
Residential Status:
<select name ="Status" size ="1">
<option value ="Resident">Resident</option>
<option value ="Non Resident">Non Resident</option>
<option value ="Not Ordinary Resident">Not Ordinary Resident</option>
</select>
<br></br>

Enter Taxable Income: 
<input type ="number" name = "TaxAmount"><br>
<br></br>

AGE :

<select name ="Age" size ="1">
<option value ="18">18</option>
<option value ="19">19</option>
<option value ="20">20</option>
<option value ="21">21</option>
<option value ="22">22</option>
<option value ="23">23</option>
<option value ="24">24</option>
<option value ="25">25</option>
<option value ="26">26</option>
<option value ="27">27</option>
<option value ="28">28</option>
<option value ="29">29</option>
<option value ="30">30</option>
<option value ="31">31</option>
<option value ="32">32</option>
<option value ="33">33</option>
<option value ="34">34</option>
<option value ="35">35</option>
<option value ="36">36</option>
<option value ="37">37</option>
<option value ="38">38</option>
<option value ="39">39</option>
<option value ="40">40</option>
<option value ="41">41</option>
<option value ="42">42</option>
<option value ="43">43</option>
<option value ="44">44</option>
<option value ="45">45</option>
<option value ="46">46</option>
<option value ="47">47</option>
<option value ="48">48</option>
<option value ="49">49</option>
<option value ="50">50</option>
<option value ="51">51</option>
<option value ="52">52</option>
<option value ="53">53</option>
<option value ="54">54</option>
<option value ="55">55</option>
<option value ="56">56</option>
<option value ="57">57</option>
<option value ="58">58</option>
<option value ="59">59</option>
<option value ="60">60</option>
<option value ="61">61</option>
<option value ="62">62</option>
<option value ="63">63</option>
<option value ="64">64</option>
<option value ="65">65</option>
<option value ="66">66</option>
<option value ="67">67</option>
<option value ="68">68</option>
<option value ="69">69</option>
<option value ="70">70</option>
<option value ="71">71</option>
<option value ="72">72</option>
<option value ="73">73</option>
<option value ="74">74</option>
<option value ="75">75</option>
<option value ="76">76</option>
<option value ="77">77</option>
<option value ="78">78</option>
<option value ="79">79</option>
<option value ="80">80</option>
<option value ="81">81</option>
<option value ="82">82</option>
<option value ="83">83</option>
<option value ="84">84</option>
<option value ="85">85</option>
<option value ="86">86</option>
<option value ="87">87</option>
<option value ="88">88</option>
<option value ="89">89</option>
<option value ="90">90</option>
<option value ="91">91</option>
<option value ="92">92</option>
<option value ="93">93</option>
<option value ="94">94</option>
<option value ="95">95</option>
<option value ="96">96</option>
<option value ="97">97</option>
<option value ="98">98</option>
<option value ="99">99</option>
<option value ="100">100</option>
</select>
<br></br>


<center>
	<input type ="SUBMIT">
</center>
</form>

</body>
</html>

2. Servlet Code (Handles the request and has the respective service method)

PHP:
package com.example.web;

import com.example.model.*;
import javax.servlet.*;
import java.io.*;
import javax.servlet.http.*;
import java.util.*;


public class MyIncomeTaxServlet extends HttpServlet {
	
	public void doPost(HttpServletRequest request, HttpServletResponse response)throws IOException, ServletException
	{
		double amount =  Double.parseDouble(request.getParameter("TaxAmount"));
		int age =  Integer.parseInt(request.getParameter("Age"));
		
		IncomeTax tax = new IncomeTax(amount,age);
		
		double n = tax.calculate();
		
		request.setAttribute("result",n);
		RequestDispatcher view = request.getRequestDispatcher("IncomeTax.jsp");
		view.forward(request,response);
		
		
	}

3. Model Class (Nothing special here.)

PHP:
package com.example.model;

public class IncomeTax {
	
	 double num ;
		int age ;
		
		public IncomeTax(double num, int age)
		{
			this.num = num;
			this.age = age;
		}
		public double calculate()
		{
			double tax;
			double net_tax = 0;
			if (num >= 200001 && num <= 250000 && age < 60 )
			{
				tax = (num*10)/100;
				net_tax = num - tax;
			
		}else if(num >= 250001 && num <= 500000 && age < 80)
			{
				tax = (num*10)/100;
				net_tax = num - tax;
			
			}
			else if(num >= 500001 && num <= 1000000)
			{
				tax = (num*20)/100;
				net_tax = num - tax;
				
			}
			else if(num > 1000000)
			{
				tax = (num*30)/100;
				net_tax = num - tax;
				
			}
			else{
				net_tax = this.num;
			}
			return net_tax;
			
			
		}
}

4. JSP (The view.)

PHP:
<%@ page import = "java.io.*,java.util.*,com.example.web.*,com.example.model.*"%>

<html>
<body>
<h1 align = "center">Income Tax Result JSP</h1>
<p>

<%
	double result = (Double)request.getAttribute("result");
	out.print("The net taxable amount is:" +" "+result);
	

%>
</body>
</html>

5. The DD (Deployment Descriptor)

PHP:
<?xml version="1.0" encoding="UTF-8"?>

<web-app xmlns="*java.sun.com/xml/ns/javaee"
  xmlns:xsi="*www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="*java.sun.com/xml/ns/javaee
                      *java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">
  
  <display-name>Welcome to Tomcat</display-name>
  <description>
     Welcome to Tomcat
  </description>
  
	<servlet>
		<servlet-name>TestIncomeTax</servlet-name>
		<servlet-class>com.example.web.MyIncomeTaxServlet</servlet-class>
	</servlet>
	
	<servlet-mapping>
		<servlet-name>TestIncomeTax</servlet-name>
		<url-pattern>/IncomeTax.do</url-pattern>
	</servlet-mapping>
  
  </web-app>

Now guys i hope i've given enough information. Please help me figure this out. Only guys knowing J2EE can understand this.


Figured it out guys and the code is fixed too.:)
 
Last edited:
Top Bottom