Java servlet - help with variable presentation and class required
Hi Folks
I need help with my Java servlet to perform some basic calculations based on the users choice.
I have a html page with number of forms.
figure - options are: 'Square' 'Triangle'
value_type - options are: 'Perimeter' 'Area'
length a
length b
length c
After selecting values in those forms I submit them and servlet should make calculations and display result based on the selection
It is working fine but I want to make it 'smart' and reduce the code but I don't know how to pass variable name into the result
Servlet code
Code:
package Form01Package;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Servlet implementation class for Servlet: Form01Class
*
*/
public class Form01Class extends javax.servlet.http.HttpServlet
implements javax.servlet.Servlet {
static final long serialVersionUID = 1L;
/** (non-Java-doc)
* @see javax.servlet.http.HttpServlet#HttpServlet()
*/
public Form01Class() {
super();
}
/** (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doGet(HttpServletRequest
request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
PrintWriter writer = response.getWriter();
writer.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01
Transitional//EN\" \"http://www.w3.org/TR/html4/loose.dtd\">\r");
writer.append("<HTML>\r<head>\r<meta http-equiv=\"Content-Type\"
content=\"text/html;
charset=UTF-8\">\r<title>Forms</title>\r</head>\r<BODY>\r");
writer.append("Your result");
//get values from html
String figure= request.getParameter("figure");
String value_type = request.getParameter("value_type");
String a = request.getParameter("a");
String b = request.getParameter("b");
String c = request.getParameter("c");
//change from string to double
double aa = Double.parseDouble(a);
double bb = Double.parseDouble(b);
double cc = Double.parseDouble(c);
//do calculations
double ValSquarePerimeter = 4*aa;
double ValTrianglePerimeter = aa+bb+cc;
//more calculations for Area .....
if ((figure == null) || (value_type.equals("choice"))) //figure or value_type not selected
writer.append("<BR><BR>Select figure and value_type");
else
writer.append("<BR> Your result for " + figure + " " + value_type + " : " + "Val"+concat(figure)+concat(value_type)) + "<BR>"); // this line is the issue
writer.append("</BODY></HTML>");
writer.flush();
}
/** (non-Java-doc)
* @see javax.servlet.http.HttpServlet#doPost(HttpServletRequest
request, HttpServletResponse response)
*/
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
doGet(request, response);
}
}
Assumption is that I have selected Square and Perimeter on the html and a = 2
I would like to get on the servlet page following:
Quote:
Your result for Square Perimeter : 8
but I'm getting
Quote:
Your result for Square Perimeter : ValSquarePerimeter
Question is how to amend line below that it will pass value of double 'ValSquarePerimeter' rather then string ValSquarePerimeter.
Code:
writer.append("<BR> Your result for " + figure + " " + value_type + " : " + "Val"+concat(figure)+concat(value_type)) + "<BR>");
When I amend this line into
Code:
writer.append("<BR> Your result for " + figure + " " + value_type + " : " + ValSquarePerimeter + "<BR>");
I'm getting what I want but I don't want to make it static in case select some other values.
Second question is how to create class that I can call for different calculations rather then doing all calculations at the top.
I belive now servlet will do all calculations and the value of the one not selected is never used/displayed.
I tried something like
Code:
class Square {
double val_a;
public ValSquarePerimeter (double aa){
this.val_a= aa;
return 4*aa;
}
}
but I keep on getting errors about non static methods.
Thanks for help
Re: Java servlet - help with variable presentation and class required
Java is not a scripting language.
You can't manipulate variable names and hope the language will recognise them as such.
So you need to figure out which value you want.
Since you only need the one result, why are you doing all the different calculations?
I would suggest using the values for 'figure' and 'valueType' as keys to decide what calculation you should be doing.
Even if it's simple 'if...else if...else' statements.
Write a method for each different calculation that returns what you need, even if that method is just in the Servlet.
Re: Java servlet - help with variable presentation and class required
Thanks for your reply
Unfortunatelly I have 6 figures and for each 2 calculations which means 12 if else if statements.
I just hoped that I could replace 12 lines of code with one.
Re: Java servlet - help with variable presentation and class required
Break them into methods.
Indeed this is a good place to learn about classes.
Create a Shape interface that has getArea and getPerimeter methods.
Implement that interface in however many shapes you need (a Square class, a Triangle class etc).
Provide a method in the servlet that takes the 'figure' and the 3 length parameters and gives back the correct Shape based on that (this is called a Factory method).
Then simply:
Code:
Shape myShape = getShape(figure, aa, bb, cc); // this can be improved on, but let's just keep it simple
double result;
if (<perimeter>) {
result = myShape.getPerimiter();
} else {
result = myShape.getArea();
}
And simply stick that result in the text.
Indeed you could turn those two methods into a single calculate method, and have the Square/Triangle/etc classes know what value_type has been requested, but I'd suggest starting with the two methods.