GTU WTAD Practical 23 : Java Servlet querystring Example

GTU WTAD Practical 23 :   Develop a interest calculation application in which user will provide all information in HTML form and that will be processed by servlet and response will be generated back to the user.

GTU WTAD Practical 23

This Servlet will explain how to create simple interest calculator using Servlet and send back processed data back to the calling page and display.

File Name : index.jsp

File Location : Tomcat_Apache\webapps\gtu23\

<html>
	<head>
		<title>GTU WTAD Practical 23</title>
	</head>
	<body>
	
	<h2 align="center"> GTU WTAD Practical 23 :   Develop a interest calculation application in which user will provide all information in HTML form and that will be processed by servlet and response will be generated back to the user.</h2>
	
	
	
		<table>
		<tr>
			<th>Interest Calculation </th>
		</tr>
		<tr>
		<td>
		<form action="./InterestCalculation" method="post">
			Principal Amount : <input type="text" name="p"> <br>
			Rate of Interest : <input type="text" name="r"> <br>
			Year : <input type="text" name="n"> <br>
			<input type="submit" value="Calculate">
			
		</form>
		<br><br>
		<div> 
		<%
			String s = request.getParameter("interest");
			String p = request.getParameter("p");
			String r = request.getParameter("r");
			String n = request.getParameter("n");
			
			if(s!=null)
			{
				out.println("<b>Simple Interest is as follow:</b><br>");
				out.println("Pincipal Amount : " + p + "<br>");
				out.println("Rate of Interest : " + r + "<br>");
				out.println("Years : " + n + "<br>");
				out.println("\n\n SI is : " + s);
			}
		%>
		
		</div>
		</td>
		</tr>
		</table>
	</body>
</html>

 

File Name : InterestCalculation.java

File Location : Tomcat_Apache\webapps\gtu23\WEB-INF\classes

/*
		GTU WTAD Practical 22 :    Create a servlet filter that logs all access to and from servlets in an application and prints 
			the following to System.out:  
			1.  the time the request was received  
			2.  the time the response was sent  
			3.  how much time it took to process the request  
			4.  the URL of the resource requested  
			5.  the IP address of the visitor  
*/

import java.io.*;
import java.util.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class InterestCalculation extends HttpServlet
{

	public void doPost(HttpServletRequest request,HttpServletResponse response) throws IOException,ServletException
	{
		String p = request.getParameter("p");
		String r = request.getParameter("r");
		String n = request.getParameter("n");
		
		float si = Float.parseFloat(p) * Float.parseFloat(r) * Float.parseFloat(n) / 100;
		
		response.sendRedirect("index.jsp?interest=" + si + "&p=" + p + "&r=" + r + "&n=" + n);
	}

}

 

File Name : web.xml

File Location : Tomcat_Apache\webapps\gtu23\WEB-INF

<?xml version="1.0" encoding="ISO-8859-1"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
                      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
  version="3.0"
  metadata-complete="true">  
  
  <display-name>GTU WTAD Practical 23  </display-name>
  <description>    Develop a interest calculation application in which user will provide all information in HTML form and that will be processed by servlet and response will be generated back to the user.   </description>
  
  <servlet>
	<servlet-name>InterestCalculation</servlet-name>
	<servlet-class>InterestCalculation</servlet-class>
  </servlet>

  <servlet-mapping>
	<servlet-name>InterestCalculation</servlet-name>
	<url-pattern>/InterestCalculation</url-pattern>
  </servlet-mapping>
    
</web-app>

 

Output (Initial)

GTU WTAD Practical 23

 

Output 2 (After Submission)

GTU WTAD Practical 23

 

Leave a Reply