Results 1 to 4 of 4
  1. #1
    viallos is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default 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

    Java 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:


    Your result for Square Perimeter : 8
    but I'm getting


    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.

    Java Code:
     writer.append("<BR> Your result for " + figure + " " + value_type + " : " + "Val"+concat(figure)+concat(value_type)) + "<BR>");
    When I amend this line into

    Java 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

    Java 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

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default 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.
    Please do not ask for code as refusal often offends.

  3. #3
    viallos is offline Member
    Join Date
    May 2012
    Posts
    2
    Rep Power
    0

    Default 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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,439
    Rep Power
    18

    Default 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:
    Java 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.
    Please do not ask for code as refusal often offends.

Similar Threads

  1. How to get variable in jsp from servlet?
    By rbabu1988 in forum New To Java
    Replies: 3
    Last Post: 01-05-2012, 04:13 PM
  2. Accessing session variable from Java Class
    By Rohit Pokhrel in forum JavaServer Pages (JSP) and JSTL
    Replies: 4
    Last Post: 12-08-2010, 04:07 PM
  3. Java servlet class ??
    By danielmessick in forum New To Java
    Replies: 2
    Last Post: 03-20-2010, 07:19 AM
  4. Calling a variable from a different java class
    By jdiesel in forum New To Java
    Replies: 4
    Last Post: 01-09-2009, 01:12 AM
  5. Replies: 1
    Last Post: 08-07-2007, 03:47 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •