We help IT Professionals succeed at work.
Get Started

Help with Java, JSP and Servlet

BrainDeadStudent
on
2,981 Views
Last Modified: 2013-11-24
Yes, this is classwork, these are the requirements
This project is intended to enforce your knowledge in:

applying EL, JSTL, and other techniques to minimize the maintenance and maximize the readability of JSP pages
developing and implementing world-ready JSP pages using scripting elements, EL, and JSTL
developing and implementing JavaBeans and use them with JSP pages
developing and implementing Model 1 architecture Web applications
developing and implementing secure Web applications using form-based declarative security
Project Requirements

Develop and implement a Web Application that calculates the roots of a quadratic equation of the form:

ax2 + bx + c

The Web application must use:

The Model 1 architecture -- a View-Controller JSP page and a Model JavaBean -- removing the model code from the presentation

Note: According to the Model 1 architecture, all the presentation logic (user input and text/results output) must be in the JSPs and all the business logic algorithms must be in the JavaBean. So, the quadratic equation algorithm has to be one method in the JavaBean and you have to use one additional property to pass the case to the JSP. The JSP would check the case property and display the proper text and/or solutions (also passed by properties from the JavaBean). No part of the business algorithm should be implemented in the JSP; no text/results presentation should be implemented in the JavaBean.

EL and JSTL -- avoiding scripting elements in the JSP page

The Web application's form should contain three boxes for input of the values for a, b, and c; and a Calculate button. An appropriate message and/or the calculated roots should be displayed at the bottom once the Calculate button is pressed.

Note: The roots of a quadratic equation of the provided above form, are determined by the following formula:  Ax^2 + Bx + C = 0

This is what I have so far using eclipse and Tomcat, would someone please take a look and tell me if I am doing the right thing and if I have set it up correctly and am using the right methods.

index.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Quad Solver</title>
</head>
<body>
<FORM action="GreetingServlet" method="POST">
Quadratic Equation Solver for Equations in the form of
Ax^2 + Bx + C<BR>
<BR>
Enter the value of A: <INPUT type="text" name="A" size="15"><BR>
Enter the value of B: <INPUT type="text" name="B" size="15"><BR>
Enter the value of C: <INPUT type="text" name="C" size="15"><BR>
<br>
<input type="submit" value="Calculate!">
<br>
<BR>
<BR>
</FORM>
</body>
</html>

GreetingServlet.java

package com.mycompany.servlet;

import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class GreetingServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public GreetingServlet() {
		super();
	}
	 protected void doPost(HttpServletRequest request,
             HttpServletResponse response) throws ServletException, IOException {
       response.setContentType("text/html;charset=UTF-8");
       PrintWriter out = response.getWriter();
       String A = request.getParameter("A").toString();
       String B = request.getParameter("B").toString();
       String C = request.getParameter("C").toString();
      
       double a = Double.parseDouble(A);
       double b = Double.parseDouble(B);
       double c = Double.parseDouble(C);
       double D = QuadraticSolver.getD();
       double[] roots = QuadraticSolver.quadratic();
       out.println("<html>");
		out.println("<head>");
		out.println("<title>Quadratic Equation Solver</title>");
		out.println("</head>");
		out.println("<body>");
		out.println("<h1>Quadratic Equation Solver </h1>");
		out.println("<h2>Your Equation is: <br><br>" + A + "x^2 + " + B + "x + " + C+ "<br>");
	   //QuadraticSolver solver = new QuadraticSolver ();
	   out.println("</h2>");
      
       //double D = QuadraticSolver.getD();
       if (a == 0.0) 
			out.println("<br>This is not a Quadratic quation ! ");
		if (b == 0.0) 
			out.println("<br>There is no solution ! ");
		if (a > 0.0 && b > 0) {
			out.println("<br><br>There is only 1 solution -C/B :  " );
			out.println("<br><br>Your Solution is : - "+ c + " / " + b + " equals" + -c/b );
			out.println("<br><br> X1 = "+ roots[0] );
			out.println("<br><br> X2 = "+ roots[1] );
     if(roots !=null){
            out.println("<br><br>Solutions are:<br><br>   X1 = " + roots[0] + " or X2 = " + roots[1]+"<br>");
             }else{
          out.println("<br><br>There Are No Real Solutions !!");
              }
 		out.println("</h2>");
		out.println("</body>");
		out.println("</html>");
		out.close();
}
}
}
	
QuadraticSolver.java

package com.mycompany.servlet;
import static java.lang.StrictMath.*;

public class QuadraticSolver {
	static double a;
	static double b;
	static double c;
	//double D;
	double discriminant = 0.0;
	double root1;
	double root2;

	static double[] quadratic() {
		double a = 0;
		double b = 0;
		double c = 0;
	
		// Return the roots of the quadratic equation
		double[] roots = new double[2];
		double D = Math.pow(b, 2) - (c*1)*(-c);
		if (D<0) {
			System.out.println("There are no real roots");
			System.out.println(D);
			return roots;
		}
		// Use Viete's formula to avoid loss of significance
		//double q = -0.5 * (b + copySign(sqrt(d), b));
		//roots[0] = q/a;
		//roots[1] = c/q;
		//return roots;
		//roots[0]=-b/2/a+Math.pow(Math.pow(b,2)-4*a*c,0.5)/2/a;
		roots[0] = ((-b + sqrt(D)) / (2 * a));
		return roots;
	}
	static double getD() {
		double D=0;
		D = b * b - 4.0f * a * c;  
		 return D;
	}
	public static void main(String[] args) {
		//double D=0;
		// D = Math.pow(b, 2) - (c*a)*(-c);
		 double D = QuadraticSolver.getD();
		double[] roots = quadratic();
		System.out.println(roots[0]);
		System.out.println(roots[1]);
	}
My ouput:
on the first page
Quadratic Equation Solver for Equations in the form of Ax^2 + Bx + C

Enter the value of A: 
Enter the value of B: 
Enter the value of C: 

on the second page

Quadratic Equation Solver 
Your Equation is: 

1x^2 + 3x + 4
There is only 1 solution -C/B : 
Your Solution is : - 4.0 / 3.0 equals-1.3333333333333333 

X1 = NaN 
X2 = 0.0 

Solutions are:

X1 = NaN or X2 = 0.0

My Directory structure:

MyFirstServlet
	Deployment descriptor:MyFirstServlet
	Java Resources :src
		Com.mycompany.servlet
	    	        GreetingServlet.java
	     	        QuadraticSolver.Java
		Libraries
	Java Script resources
	Build
	WebContent
		META-INF
		WEB-INF
			Classes
			Lib
			Web.xml
			Index.jsp

Open in new window

Comment
Watch Question
student
CERTIFIED EXPERT
Commented:
This problem has been solved!
Unlock 2 Answers and 29 Comments.
See Answers
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE