Solved

How do I deploy and run this JSP Web Application locally?

Posted on 2008-06-21
34
507 Views
Last Modified: 2013-11-24
I am using Tomcat 6.0 through port 8282.  My operating system is Windows XP.  I am able to deploy and run other JSP Web Applications without errors.  The attached (zipped) folder is located at I:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps\ on my local machine.  When I attempt to deploy and run the attached JSP Web Application locally (http://localhost:8282/DistanceCalculator2/distanceCalculator2.jsp), I get the error below.  What is causing this error?  Thank you in advance!  

Note:  I have added .txt extensions to the end of each file because the other filetypes (.jsp, .java, and .class) are not able to be uploaded here.
HTTP Status 500 - 
 

--------------------------------------------------------------------------------
 

type Exception report
 

message 
 

description The server encountered an internal error () that prevented it from fulfilling this request.
 

exception 
 

javax.servlet.ServletException: java.lang.NoClassDefFoundError: calc/DistanceCalc2 (wrong name: DistanceCalc2)

	org.apache.jasper.servlet.JspServlet.service(JspServlet.java:274)

	javax.servlet.http.HttpServlet.service(HttpServlet.java:803)

Open in new window

DistanceCalculator2.zip
0
Comment
Question by:CharleneS77
  • 19
  • 15
34 Comments
 
LVL 27

Accepted Solution

by:
rrz earned 500 total points
ID: 21839476
>javax.servlet.ServletException: java.lang.NoClassDefFoundError: calc/DistanceCalc2 (wrong name: DistanceCalc2)
It says wrong name but it should have said "wrong fully qualified name". The problem is that you neglected to put the package declaration as the first line in  your DistanceCalc2.java  file. The first line should be  
package calc;    

You can leave this question open for a while. I will help you with some follow up questions.  Don't get discouraged sometimes  error messages are hard to figure out even for the experts.
0
 

Author Comment

by:CharleneS77
ID: 21839547
Thank you for the response and the positive words.  I am now able to access my jsp, but when I click the Calculate button, I get the error below.  My latest version is attached.
HTTP Status 404 - /DistanceCalculator2/DistanceCalculator2.jsp
 

--------------------------------------------------------------------------------
 

type Status report
 

message /DistanceCalculator2/DistanceCalculator2.jsp
 

description The requested resource (/DistanceCalculator2/DistanceCalculator2.jsp) is not available.

Open in new window

DistanceCalculator2.zip
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 500 total points
ID: 21839607
I can't find the problem. Java is case sensitive. So the request for
>HTTP Status 404 - /DistanceCalculator2/DistanceCalculator2.jsp  
will not find  
 /DistanceCalculator2/distanceCalculator2.jsp  
but you have  
><form method="post" action="distanceCalculator2.jsp"/>    
So it should work.  But you do have a HTML syntex error. It should be  without slash
<form method="post" action="distanceCalculator2.jsp">      
Since your action is to the same page, we can cheat and just use
<form method="post" >
Try that. Good night. I will be back tomorrow.
0
 

Author Comment

by:CharleneS77
ID: 21842829
If I use
<form method="post" action="distanceCalculator2.jsp">  
nothing happens when I click on the Calculate button.

If I use
<form method="post">  
nothing happens when I click on the Calculate button.
0
 
LVL 27

Expert Comment

by:rrz
ID: 21843107
>nothing happens when I click on the Calculate button.  
What do you mean by "nothing" ?  Does the form submit a request ? No error mesages ?  If you change to
action="someOtherPage.jsp"  
does it go there ?
0
 

Author Comment

by:CharleneS77
ID: 21843150
>>>What do you mean by "nothing" ?  
When I click the Calculate button, no output is displayed.

>>>Does the form submit a request ?
How do I determine whether a request was submitted?

>>>No error mesages ?
No error messages.

>>>>If you change to action="someOtherPage.jsp" does it go there ?
Yes, I moved currentTime.jsp to the same directory and it did go to currentTime.jsp.
0
 
LVL 27

Expert Comment

by:rrz
ID: 21843191
>When I click the Calculate button, no output is displayed.  
Is there anything in the page source in the browser ?  
0
 

Author Comment

by:CharleneS77
ID: 21843199
When I right click on the page and select View Source, I see the following:

 
 
 
 
 

<html>

	<head><title>JSP Distance Calculator using EL Statements, JSTL, and a JavaBean</title> </head>

	<body>

		<font face="Verdana"><center><i>

		<h1>JSP Distance Calculator using EL Statements, JSTL, and a JavaBean</h1>

		</i></center></font>

	

		<form method="post" action="distanceCalculator2.jsp">

			<br>

			<font size="2" face="Verdana">

			Please enter the value of X1:

			<font color="blue">*</font>

			<input type="text" name="x1" length="7"/>		

			<br>

			</font>
 

			<font size="2" face="Verdana">

			Please enter the value of Y1:

			<font color="blue">*</font>

			<input type="text" name="y1" length="7"/>		

			<br>

			</font>
 

			<font size="2" face="Verdana">

			Please enter the value of X2:

			<font color="blue">*</font>

			<input type="text" name="x2" length="7"/>		

			<br>

			</font>
 

			<font size="2" face="Verdana">

			Please enter the value of Y2:

			<font color="blue">*</font>

			<input type="text" name="y2" length="7"/>		

			</font>
 

			<p>

			<input type="submit" value="Calculate"/>

			</p>

		</form>
 

		

			
 

			
 

			
 

			
 

			

				

	</body>

</html>

Open in new window

0
 
LVL 27

Expert Comment

by:rrz
ID: 21843226
I copy and pasted your last post and it displays in my browser(IE 7). You still see nothing in your browser window ?
0
 

Author Comment

by:CharleneS77
ID: 21843235
>>>>I copy and pasted your last post and it displays in my browser(IE 7). You still see nothing in your browser window ?

Did you copy and paste the html that I got from View Source?  Are you able to get  result for the distance between the two points?  How?
0
 
LVL 27

Expert Comment

by:rrz
ID: 21843245
>Did you copy and paste the html that I got from View Source?  
Yes.
>Are you able to get  result for the distance between the two points?  How?  
I didn't test that yet.
0
 
LVL 27

Expert Comment

by:rrz
ID: 21843257
meanwhile add this inside your <c:choose> tag  
<c:otherwise>
                         something is wrong, I'll keep looking
</c:othewise>
0
 
LVL 27

Expert Comment

by:rrz
ID: 21843264
no add this
<c:otherwise>
                     rrz${distance2.status}rrz
</c:othewise>
0
 

Author Comment

by:CharleneS77
ID: 21843293
When I added the <c:otherwise> tag after the last <c:when> tag, and inside the <c:choose> tag, I get the following error:
HTTP Status 500 - 
 

--------------------------------------------------------------------------------
 

type Exception report
 

message 
 

description The server encountered an internal error () that prevented it from fulfilling this request.
 

exception 
 

org.apache.jasper.JasperException: /distanceCalculator2.jsp(94,3) The end tag "&lt;/c:othewise" is unbalan

Open in new window

0
 
LVL 27

Expert Comment

by:rrz
ID: 21843299
try again with my last post
0
 

Author Comment

by:CharleneS77
ID: 21843317
I still get the same error:

HTTP Status 500 -

--------------------------------------------------------------------------------

type Exception report

message

description The server encountered an internal error () that prevented it from fulfilling this request.

exception

org.apache.jasper.JasperException: /distanceCalculator2.jsp(94,3) The end tag "&lt;/c:othewise" is unbalanced


Below is what I have in the <c:choose> tag:
		<c:choose>

			<c:when test="${distance2.status == -1}">

				<hr/>

				Please input a value for X1.

			</c:when>
 

			<c:when test="${distance2.status == -2}">

				<hr/>

				Please input a value for Y1.

			</c:when>
 

			<c:when test="${distance2.status == -3}">

				<hr/>

				Please input a value for X2.

			</c:when>
 

			<c:when test="${distance2.status == -4}">

				<hr/>

				Please input a value for Y2.

			</c:when>
 

			<c:when test="${distance2.status == 1}">

				<hr/>

				<font face="Verdana"> <u>

				<h3> Output</h3>

				</u> </font>
 

				<font size="2" face="Verdana">

				X1 = ${distance2.x1} <br>

				Y1 = ${distance2.y1} <br>

				X2 = ${distance2.x2} <br>

				Y2 = ${distance2.y2} <br>

				</font>
 

				<br>

				<font size="3" face=Verdana" color="blue"> <b>

				The distance between (X1, Y1) and (X2, Y2) is 

				<fmt:formatNumber value="${distance2.distanceBetweenPoints}" 

						type="number" 

						maxFractionDigits="2"/>

			</c:when>

			<c:otherwise>

                     			rrz${distance2.status}rrz

			</c:othewise>

		</c:choose>

Open in new window

0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 500 total points
ID: 21843324
I made a typo( missed an r). Try using  
                  <c:otherwise>
                                       rrz${distance2.status}rrz
                  </c:otherwise>
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 

Author Comment

by:CharleneS77
ID: 21843337
>>>>I made a typo

Sorry I didn't catch that one.  Now I am seeing the same thing:  I have the page, I am able to input the values, I click the Calculate button, and nothing happens.  The only difference I see is the following at the bottom of the page, below the Calculate button:  rrz()rrz.  This is displayed before I click the Calculate button the first time.
0
 

Author Comment

by:CharleneS77
ID: 21843349
Is it something in the JavaBean?  Are my setDistanceBetweenPoints( ) and calculateDistance( ) methods ok?
0
 
LVL 27

Expert Comment

by:rrz
ID: 21843351
I guess I will try your web app in my Tomcat now.
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 500 total points
ID: 21843380
When I request your JSP I see
WARNING: Internal Error: File /WEB-INF/web.xml not found  
in my console.  So you should add  a web.xml file in  your web app's WEB-INF folder.  Just empty root tag will do for now(see below). Meanwhile I am still working.


<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_5.xsd"
    version="2.5">

</web-app>
0
 

Author Comment

by:CharleneS77
ID: 21843389
OK, I have added the web.xml file you posted.  I wonder why I do not get that error.
0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 500 total points
ID: 21843655
>OK, I have added the web.xml file you posted.  I wonder why I do not get that error.  
I don't know.  
 I got your code to work some what.  You should really validate the user's input.  If user inputs letters instead of numbers then he will see error message
java.lang.NumberFormatException:  
Anyway here is what works for me. You will have to take out debugging lines. I want you to see how I debug. I guess your mistake was to  call calculateDistance( ); in  setDistanceBetweenPoints  instead of in  getDistanceBetweenPoints( )  



// The JavaBean
// DistanceCalc2.java

package calc;

public class DistanceCalc2
{      
      // read-write properties
      private String x1;
      private String y1;
      private String x2;
      private String y2;
            
      // read-only properties
      private double distanceBetweenPoints = 0.0;
      
      // status flag
      private int status = -1;
      
      // flags indicating errors or success
      private static final int MISSING_X1 = -1;
      private static final int MISSING_Y1 = -2;
      private static final int MISSING_X2 = -3;
      private static final int MISSING_Y2 = -4;
      private static final int SUCCESSFUL = 1;            
      
            
      // setter and getter methods
      //-----------------------------------------      
      public void setX1( String passedX1 )
      {
            x1 = passedX1;
      }
      //-----------------------------------------            
      public String getX1( )
      {
            return x1;
      }
      //-----------------------------------------      
      public void setY1( String passedY1 )
      {
            y1 = passedY1;
      }      
      //-----------------------------------------            
      public String getY1( )
      {
            return y1;
      }
      //-----------------------------------------            
      public void setX2( String passedX2 )
      {
            x2 = passedX2;
      }      
      //-----------------------------------------            
      public String getX2( )
      {
            return x2;
      }
      //-----------------------------------------            
      public void setY2( String passedY2 )
      {
            y2 = passedY2;
      }      
      //-----------------------------------------            
      public String getY2( )
      {
            return y2;
      }
      //-----------------------------------------      
      public void setDistanceBetweenPoints( /*String passedX1, String passedY1, String passedX2, String passedY2*/ )
      {
            /*
            x1 = passedX1;
            y1 = passedY1;
            x2 = passedX2;
            y2 = passedY2;
            */
            
            calculateDistance( );
      }
      //-----------------------------------------      
      public double getDistanceBetweenPoints( )
      {
                calculateDistance( );
                  System.out.print("returning distance");
            return distanceBetweenPoints;
      }
      //-----------------------------------------                  
      public int getStatus( )
      {
            return status;
      }      
      //-----------------------------------------                  
      private void calculateDistance( )
      {
            if( ( x1 == null  ) || ( y1 == null ) || ( x2 == null ) || ( y2 == null ) )
            {
                  if( x1 == null )
                  {
                        status = MISSING_X1;
                  }
                  if( y1 == null )
                  {
                        status = MISSING_Y1;
                  }
                  if( x2 == null )
                  {
                        status = MISSING_X2;
                  }
                  if( y2 == null )
                  {
                        status = MISSING_Y2;
                  }
            }
            else
            {
                       System.out.print("before calc");
                  distanceBetweenPoints = Math.sqrt( ( Double.parseDouble( x1 ) - Double.parseDouble( x2 ) ) *
                                                                                      ( Double.parseDouble( x1 ) - Double.parseDouble( x2 ) ) +
                                                                                      ( Double.parseDouble( y1 ) - Double.parseDouble( y2 ) ) *
                                                                                      ( Double.parseDouble( y1 ) - Double.parseDouble( y2 ) ) );
                  System.out.print("after calc");
                  status = SUCCESSFUL;
            }
      }
}
 


<%-- distanceCalculator2.jsp --%>

<jsp:useBean id="distance2" scope="page" class="calc.DistanceCalc2" />
<jsp:setProperty name="distance2" property="*" />

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>

<html>
      <head><title>JSP Distance Calculator using EL Statements, JSTL, and a JavaBean</title> </head>
      <body>
            <font face="Verdana"><center><i>
            <h1>JSP Distance Calculator using EL Statements, JSTL, and a JavaBean</h1>
            </i></center></font>
      zz${param.x1}zz${param.y1}zz${param.x2}zz${param.y2}zz<br/>
        zz${distance2.x1}zz${distance2.y1}zz${distance2.x2}zz${distance2.y2}zz<br/>
        zz${distance2.distanceBetweenPoints}zz
            <form method="post" action="distanceCalculator2.jsp"/>
                  <br>
                  <font size="2" face="Verdana">
                  Please enter the value of X1:
                  <font color="blue">*</font>
                  <input type="text" name="x1" length="7"/>            
                  <br>
                  </font>

                  <font size="2" face="Verdana">
                  Please enter the value of Y1:
                  <font color="blue">*</font>
                  <input type="text" name="y1" length="7"/>            
                  <br>
                  </font>

                  <font size="2" face="Verdana">
                  Please enter the value of X2:
                  <font color="blue">*</font>
                  <input type="text" name="x2" length="7"/>            
                  <br>
                  </font>

                  <font size="2" face="Verdana">
                  Please enter the value of Y2:
                  <font color="blue">*</font>
                  <input type="text" name="y2" length="7"/>            
                  </font>

                  <p>
                  <input type="submit" value="Calculate"/>
                  </p>
            </form>

            <c:choose>
                  <c:when test="${distance2.status == -1}">
                        <hr/>
                        Please input a value for X1.
                  </c:when>

                  <c:when test="${distance2.status == -2}">
                        <hr/>
                        Please input a value for Y1.
                  </c:when>

                  <c:when test="${distance2.status == -3}">
                        <hr/>
                        Please input a value for X2.
                  </c:when>

                  <c:when test="${distance2.status == -4}">
                        <hr/>
                        Please input a value for Y2.
                  </c:when>

                  <c:when test="${distance2.status == 1}">
                        <hr/>
                        <font face="Verdana"> <u>
                        <h3> Output</h3>
                        </u> </font>

                        <font size="2" face="Verdana">
                        X1 = ${distance2.x1} <br>
                        Y1 = ${distance2.y1} <br>
                        X2 = ${distance2.x2} <br>
                        Y2 = ${distance2.y2} <br>
                        </font>

                        <br>
                        <font size="3" face=Verdana" color="blue"> <b>
                        The distance between (X1, Y1) and (X2, Y2) is
                        <fmt:formatNumber value="${distance2.distanceBetweenPoints}"
                                    type="number"
                                    maxFractionDigits="2"/>
                  </c:when>
            </c:choose>
            rrz${distance2.status}rrz
      </body>
</html>
0
 

Author Comment

by:CharleneS77
ID: 21851504
When I run this I get the following error:

HTTP Status 500 - 
 

--------------------------------------------------------------------------------
 

type Exception report
 

message 
 

description The server encountered an internal error () that prevented it from fulfilling this request.
 

exception 
 

org.apache.jasper.JasperException: /distanceCalculator2.jsp(3,0) The value for the useBean class attribute calc.DistanceCalc2 is invalid.

Open in new window

0
 

Author Comment

by:CharleneS77
ID: 21851548
Nevermind, I have it working.
0
 

Author Comment

by:CharleneS77
ID: 21851591
What am I missing to display my output in the <c:choose> tag?
0
 
LVL 27

Expert Comment

by:rrz
ID: 21852074
>What am I missing to display my output in the <c:choose> tag?  
The output is displayed for me. What problem are you having with it ?  
0
 

Author Comment

by:CharleneS77
ID: 21852123
I have most of it figured out now.  I do have one more problem though.  

With a call to calculateDistance( ); inside the getDistanceBetweenPoints( ) method, I am able to get a result for distanceBetweenPoints, but I am NOT able to check for user input.

Without a call to calculateDistance( ); inside the getDistanceBetweenPoints( ) method, I am able to check for user input, but I am NOT able to get a result for distanceBetweenPoints.  

My most recent code is here:


// The JavaBean

// DistanceCalc2.java
 

package calc;
 

public class DistanceCalc2

{      

	// read-write properties

   private String x1;

   private String y1;

   private String x2;

   private String y2;

            

   // read-only properties

   private double distanceBetweenPoints;

    

  

	// default constructor

   //-----------------------------------------  

	public DistanceCalc2( )

	{

		

	}

   //-----------------------------------------  

		            

   // setter and getter methods

   //-----------------------------------------      

   public void setX1( String passedX1 )

   {

   	x1 = passedX1;

   }

   //-----------------------------------------            

   public String getX1( )

   {

      return x1;	

   }

   //-----------------------------------------      

   public void setY1( String passedY1 )

   {

   	y1 = passedY1;

   }      

   //-----------------------------------------            

   public String getY1( )

   {

   	return y1;

   }

   //-----------------------------------------            

   public void setX2( String passedX2 )

   {

         x2 = passedX2;

   }      

   //-----------------------------------------            

   public String getX2( )

   {

      return x2;

   }

   //-----------------------------------------            

   public void setY2( String passedY2 )

   {

  		y2 = passedY2;

   }      

   //-----------------------------------------            

   public String getY2( )

   {

  		return y2;

   }  

   //-----------------------------------------  

   public void setDistanceBetweenPoints( )

   {

   	calculateDistance( );

   } 

   //-----------------------------------------		    

   public double getDistanceBetweenPoints( )

   {

		//calculateDistance( );

     	return distanceBetweenPoints;

   }      

   //-----------------------------------------                  

   private void calculateDistance( )

   {

			

		distanceBetweenPoints = Math.sqrt( ( Double.parseDouble( x1 ) - Double.parseDouble( x2 ) ) * 

                                         ( Double.parseDouble( x1 ) - Double.parseDouble( x2 ) ) + 

                                         ( Double.parseDouble( y1 ) - Double.parseDouble( y2 ) ) * 

                                         ( Double.parseDouble( y1 ) - Double.parseDouble( y2 ) ) );		

	}

}
 
 
 

<%-- distanceCalculator2.jsp --%>
 

<jsp:useBean id="distance2" scope="page" class="calc.DistanceCalc2" />

<jsp:setProperty name="distance2" property="*" />
 

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
 

<html>

      <head><title>JSP Distance Calculator using EL Statements, JSTL, and a JavaBean</title> </head>

      <body>

      	<font face="Verdana"><center><i>

            	<h1>JSP Distance Calculator using EL Statements, JSTL, and a JavaBean</h1>

            	</i></center></font>
 

	<c:set var="x1" value="${distance2.x1}"/>

	<c:set var="y1" value="${distance2.y1}"/>

	<c:set var="x2" value="${distance2.x2}"/>

	<c:set var="y2" value="${distance2.y2}"/>

	<c:set var="distanceBetweenPoints" value="${distance2.distanceBetweenPoints}"/>
 
 

            	<form method="post" action="distanceCalculator2.jsp"/>

                  	<br>

                  	<font size="2" face="Verdana">

                  	Please enter the value of X1:

                  	<font color="blue">*</font>

                  	<input type="text" name="x1" length="7"/>            

              	    	<br>

                  	</font>
 

                  	<font size="2" face="Verdana">

                  	Please enter the value of Y1:

                  	<font color="blue">*</font>

                  	<input type="text" name="y1" length="7"/>            

                  	<br>

                  	</font>
 

                  	<font size="2" face="Verdana">

                  	Please enter the value of X2:

                  	<font color="blue">*</font>

                  	<input type="text" name="x2" length="7"/>            

                  	<br>

                  	</font>
 

                  	<font size="2" face="Verdana">

                  	Please enter the value of Y2:

                  	<font color="blue">*</font>

                  	<input type="text" name="y2" length="7"/>            

                  	</font>
 

                  	<p>

                  	<input type="submit" value="Calculate"/>

                  	</p>

            	</form>
 

	<c:if test="${pageContext.request.method=='POST'}">

            	<c:choose>

                  	<c:when test='${x1 == null}'>

                        		<hr/><br/>

			<font size="4" face=Verdana" color="red">

                        		Please input a value for X1.

			</font>

                  	</c:when>
 

                  	<c:when test='${y1 == null}'>

                        		<hr/><br/>

			<font size="4" face=Verdana" color="red">

                        		Please input a value for Y1.

			</font>

                  	</c:when>
 

                  	<c:when test='${x2 == null}'>

                        		<hr/><br/>

			<font size="4" face=Verdana" color="red">

                        		Please input a value for X2.

			</font>

                  	</c:when>
 

                  	<c:when test='${y2 == null}'>

                        		<hr/><br/>

			<font size="4" face=Verdana" color="red">

                        		Please input a value for Y2.

			</font>

                  	</c:when>

	

		<c:otherwise>

                                   	<hr/>

                        		<font face="Verdana"> <u>

                        		<h3>Output</h3>

                        		</u></font>
 

                        		<font size="4" face="Verdana" color="blue">

                        		X1 = ${x1} <br/>

                        		Y1 = ${y1} <br/>

                        		X2 = ${x2} <br/>

                        		Y2 = ${y2} <br/>

                        		</font>
 

                        		<br/><br/>

                        		<font size="4" face="Verdana" color="blue"> 

                        		The distance between (X1, Y1) and (X2, Y2) is 

                        		<fmt:formatNumber value="${distanceBetweenPoints}" 

                                    	type="number" maxFractionDigits="15"/> 

			</font>

                  	</c:otherwise>

            	</c:choose>

	</c:if>
 

	<font size="4" face="Verdana" color="green">

	<br/>

	<br/>

        	x1=${x1} 

	<br/>

	y1=${y1} 

	<br/>

	x2=${x2} 

	<br/>

	y2=${y2}

	<br/>

        	distanceBetweenPoints=${distanceBetweenPoints}

	<br/>

	</font>
 

      </body>

</html>

Open in new window

0
 

Author Comment

by:CharleneS77
ID: 21852180
I got that part now.  My getDistanceBetweenPoints( ) method now looks like this:

   public double getDistanceBetweenPoints( )
   {
            if( ( x1 != null ) && ( y1 != null ) && ( x2 != null ) && ( y2 != null ) )
            {
                  calculateDistance( );
            }
           return distanceBetweenPoints;
   }

I know you recommended exception handling for letters being input.  Where would I put the exception?
0
 

Author Comment

by:CharleneS77
ID: 21852244
Do I need to use the <%@ page errorPage> directive to handle the exception?



0
 
LVL 27

Assisted Solution

by:rrz
rrz earned 500 total points
ID: 21852329
Did you redesign ?  If yes then post your two files. I fixed your original code.  
I added this to the JSP.  
                  <c:when test="${distance2.status == -5}">
                        
                        Please input numbers only.
                  </c:when>  

In the bean code,  

      private void calculateDistance( )
      {
            if( ( x1 == null  ) || ( y1 == null ) || ( x2 == null ) || ( y2 == null ) )
            {
                  if( x1 == null )
                  {
                        status = MISSING_X1;
                  }
                  if( y1 == null )
                  {
                        status = MISSING_Y1;
                  }
                  if( x2 == null )
                  {
                        status = MISSING_X2;
                  }
                  if( y2 == null )
                  {
                        status = MISSING_Y2;
                  }
            }
            else
            {
                  try{
                  distanceBetweenPoints = Math.sqrt( ( Double.parseDouble( x1 ) - Double.parseDouble( x2 ) ) *
                              ( Double.parseDouble( x1 ) - Double.parseDouble( x2 ) ) +
                                ( Double.parseDouble( y1 ) - Double.parseDouble( y2 ) ) *
                              ( Double.parseDouble( y1 ) - Double.parseDouble( y2 ) ) );
                  status = SUCCESSFUL;
                  }catch(NumberFormatException nfe){status = -5;}
            }
      }
0
 

Author Comment

by:CharleneS77
ID: 21852342
I did redesign.  I got rid of the status variable altogether.  Below is my latest code including my attempt at exception handling.  


// The JavaBean

// DistanceCalc2.java
 

package calc;
 

public class DistanceCalc2

{      

	// read-write properties

   private String x1;

   private String y1;

   private String x2;

   private String y2;

            

   // read-only properties

   private double distanceBetweenPoints;

	private String errorMessage;

    

  

	// default constructor

   //-----------------------------------------  

	public DistanceCalc2( )

	{

		

	}

   //-----------------------------------------  

		            

   // setter and getter methods

   //-----------------------------------------      

   public void setX1( String passedX1 )

   {

   	x1 = passedX1;

   }

   //-----------------------------------------            

   public String getX1( )

   {

      return x1;	

   }

   //-----------------------------------------      

   public void setY1( String passedY1 )

   {

   	y1 = passedY1;

   }      

   //-----------------------------------------            

   public String getY1( )

   {

   	return y1;

   }

   //-----------------------------------------            

   public void setX2( String passedX2 )

   {

         x2 = passedX2;

   }      

   //-----------------------------------------            

   public String getX2( )

   {

      return x2;

   }

   //-----------------------------------------            

   public void setY2( String passedY2 )

   {

  		y2 = passedY2;

   }      

   //-----------------------------------------            

   public String getY2( )

   {

  		return y2;

   }  

   //-----------------------------------------  

   public void setDistanceBetweenPoints( )

   {

   	calculateDistance( );

   } 

   //-----------------------------------------		    

   public double getDistanceBetweenPoints( )

   {

		if( ( x1 != null ) && ( y1 != null ) && ( x2 != null ) && ( y2 != null ) )

		{

			try 

			{

            Double.parseDouble( x1 );

            Double.parseDouble( y1 );

				Double.parseDouble( x2 );

            Double.parseDouble( y2 );

        	} 

			catch ( NumberFormatException nfe ) 

			{

            errorMessage = "Please enter a valid number: " + nfe.getMessage( );	

				setErrorMessage( errorMessage );

        	} 

			catch ( Exception e ) 

			{

           	errorMessage = "An error occured: " + e.getMessage( );

				setErrorMessage( errorMessage );

        	}

			

			calculateDistance( );

		}

     	return distanceBetweenPoints;

   }      

   //-----------------------------------------

   public void setErrorMessage( String passedErrorMessage )

   {

   	errorMessage = passedErrorMessage;

   }	

   //-----------------------------------------  

   public String getErrorMessage( )

   {

   	return errorMessage;

   } 

   //-----------------------------------------                  

   private void calculateDistance( )

   {			

		distanceBetweenPoints = Math.sqrt( ( Double.parseDouble( x1 ) - Double.parseDouble( x2 ) ) * 

                                         ( Double.parseDouble( x1 ) - Double.parseDouble( x2 ) ) + 

                                         ( Double.parseDouble( y1 ) - Double.parseDouble( y2 ) ) * 

                                         ( Double.parseDouble( y1 ) - Double.parseDouble( y2 ) ) );		

	}

}

  

//===================================

//===================================
 

<%-- distanceCalculator2.jsp --%>
 

<jsp:useBean id="distance2" scope="page" class="calc.DistanceCalc2" />

<jsp:setProperty name="distance2" property="*" />
 

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
 

<html>

      <head><title>JSP Distance Calculator using EL Statements, JSTL, and a JavaBean</title> </head>

      <body>

	<br/>

      	<font face="Verdana"><center><i>

            	<h2>JSP Distance Calculator using EL Statements, JSTL, and a JavaBean</h2>

            	</i></center></font>
 

	<c:set var="x1" value="${distance2.x1}"/>

	<c:set var="y1" value="${distance2.y1}"/>

	<c:set var="x2" value="${distance2.x2}"/>

	<c:set var="y2" value="${distance2.y2}"/>

	<c:set var="distanceBetweenPoints" value="${distance2.distanceBetweenPoints}"/>

	<c:set var="errorMessage" value="${distance2.errorMessage}"/>
 
 

            	<form method="post" action="distanceCalculator2.jsp">

                  	<br>

                  	<font size="2" face="Verdana">

                  	Please enter the value of X1:

                  	<font color="blue">*</font>

                  	<input type="text" name="x1" length="7"/>            

              	    	<br>

                  	</font>
 

                  	<font size="2" face="Verdana">

                  	Please enter the value of Y1:

                  	<font color="blue">*</font>

                  	<input type="text" name="y1" length="7"/>            

                  	<br>

                  	</font>
 

                  	<font size="2" face="Verdana">

                  	Please enter the value of X2:

                  	<font color="blue">*</font>

                  	<input type="text" name="x2" length="7"/>            

                  	<br>

                  	</font>
 

                  	<font size="2" face="Verdana">

                  	Please enter the value of Y2.

                  	<font color="blue">*</font>

                  	<input type="text" name="y2" length="7"/>            

                  	</font>
 

                  	<p>

                  	<input type="submit" value="Calculate"/>

                  	</p>

            	</form>
 

	<c:if test="${pageContext.request.method=='POST'}">

            	<c:choose>

                  	<c:when test='${x1 == null}'>

                        		<hr/><br/>

			<font size="4" face="Verdana" color="red">

                        		You did not enter a value for X1.

			</font>

                  	</c:when>
 

                  	<c:when test='${y1 == null}'>

                        		<hr/><br/>

			<font size="4" face="Verdana" color="red">

                        		You did not enter a value for X1.

			</font>

                  	</c:when>
 

                  	<c:when test='${x2 == null}'>

                        		<hr/><br/>

			<font size="4" face="Verdana" color="red">

                        		You did not enter a value for X1.

			</font>

                  	</c:when>
 

                  	<c:when test='${y2 == null}'>

                        		<hr/><br/>

			<font size="4" face="Verdana" color="red">

                        		You did not enter a value for X1.

			</font>

                  	</c:when>
 

		<c:otherwise>

                                   	<hr/>

                        		<font face="Verdana"> <u>

                        		<h3>Output</h3>

                        		</u></font>
 

                        		<font size="4" face="Verdana" color="blue">

                        		X1 = ${x1} <br/>

                        		Y1 = ${y1} <br/>

                        		X2 = ${x2} <br/>

                        		Y2 = ${y2} <br/>

                        		</font>
 
 
 
 

			<c:choose>

				<c:when test='${errorMessage != null}'>

					<br/>

					<font size="4" face="Verdana" color="red">

                        				${errorMessage}

					</font>

				</c:when>

			

				<c:otherwise>

                        				<br/>

                        				<font size="4" face="Verdana" color="blue"> 

                        				The distance between (X1, Y1) and (X2, Y2) is 

                        				<fmt:formatNumber value="${distanceBetweenPoints}" 

                                    			type="number" maxFractionDigits="15"/> 

				</font>

				</c:otherwise>

			</c:choose>
 
 
 

                  	</c:otherwise>

            	</c:choose>

	</c:if>

	

	<%--

	<font size="4" face="Verdana" color="green">

	<br/><br/>

        	x1=${x1} 

	<br/>

	y1=${y1} 

	<br/>

	x2=${x2} 

	<br/>

	y2=${y2}

	<br/>

        	distanceBetweenPoints=${distanceBetweenPoints}

	<br/>

	</font>

	--%>
 

      </body>

</html>

Open in new window

0
 

Author Comment

by:CharleneS77
ID: 21852464
OK, it works like a charm.  Thank you very much for taking the time to help me.  Below is the working code:
// The JavaBean

// DistanceCalc2.java
 

package calc;
 

public class DistanceCalc2

{      

	// read-write properties

   private String x1;

   private String y1;

   private String x2;

   private String y2;

            

   // read-only properties

   private double distanceBetweenPoints;

   private int status = 0;

  

	// default constructor

   //-----------------------------------------  

	public DistanceCalc2( )

	{

		

	}

   //-----------------------------------------  

		            

   // setter and getter methods

   //-----------------------------------------      

   public void setX1( String passedX1 )

   {

   	x1 = passedX1;

   }

   //-----------------------------------------            

   public String getX1( )

   {

      return x1;	

   }

   //-----------------------------------------      

   public void setY1( String passedY1 )

   {

   	y1 = passedY1;

   }      

   //-----------------------------------------            

   public String getY1( )

   {

   	return y1;

   }

   //-----------------------------------------            

   public void setX2( String passedX2 )

   {

         x2 = passedX2;

   }      

   //-----------------------------------------            

   public String getX2( )

   {

      return x2;

   }

   //-----------------------------------------            

   public void setY2( String passedY2 )

   {

  		y2 = passedY2;

   }      

   //-----------------------------------------            

   public String getY2( )

   {

  		return y2;

   }  

   //-----------------------------------------  

   public void setDistanceBetweenPoints( )

   {

   	calculateDistance( );

   } 

   //-----------------------------------------		    

   public double getDistanceBetweenPoints( )

   {

		if( ( x1 != null ) && ( y1 != null ) && ( x2 != null ) && ( y2 != null ) )

		{

			try

			{

				calculateDistance( );   

       	}

			catch(NumberFormatException nfe)

			{

				status = 1;

			}

  		}

     	return distanceBetweenPoints;

   }   	   

   //-----------------------------------------    

   public int getStatus( )

   {

   	return status;

   } 

   //-----------------------------------------                

   private void calculateDistance( )

   {			

		distanceBetweenPoints = Math.sqrt( ( Double.parseDouble( x1 ) - Double.parseDouble( x2 ) ) * 

                                         ( Double.parseDouble( x1 ) - Double.parseDouble( x2 ) ) + 

                                         ( Double.parseDouble( y1 ) - Double.parseDouble( y2 ) ) * 

                                         ( Double.parseDouble( y1 ) - Double.parseDouble( y2 ) ) );		

	}

   //-----------------------------------------

}
 

//=======================

//=======================
 

<%-- distanceCalculator2.jsp --%>
 

<jsp:useBean id="distance2" scope="page" class="calc.DistanceCalc2" />

<jsp:setProperty name="distance2" property="*" />
 

<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>

<%@taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt" %>
 

<html>

      <head><title>JSP Distance Calculator using EL Statements, JSTL, and a JavaBean</title> </head>

      <body>

	<br/>

      	<font face="Verdana"><center><i>

            	<h2>JSP Distance Calculator using EL Statements, JSTL, and a JavaBean</h2>

            	</i></center></font>
 

	<c:set var="x1" value="${distance2.x1}"/>

	<c:set var="y1" value="${distance2.y1}"/>

	<c:set var="x2" value="${distance2.x2}"/>

	<c:set var="y2" value="${distance2.y2}"/>

	<c:set var="distanceBetweenPoints" value="${distance2.distanceBetweenPoints}"/>

	<c:set var="status" value="${distance2.status}"/>
 
 

            	<form method="post" action="distanceCalculator2.jsp">

                  	<br>

                  	<font size="2" face="Verdana">

                  	Please enter the value of X1:

                  	<font color="blue">*</font>

                  	<input type="text" name="x1" length="7"/>            

              	    	<br>

                  	</font>
 

                  	<font size="2" face="Verdana">

                  	Please enter the value of Y1:

                  	<font color="blue">*</font>

                  	<input type="text" name="y1" length="7"/>            

                  	<br>

                  	</font>
 

                  	<font size="2" face="Verdana">

                  	Please enter the value of X2:

                  	<font color="blue">*</font>

                  	<input type="text" name="x2" length="7"/>            

                  	<br>

                  	</font>
 

                  	<font size="2" face="Verdana">

                  	Please enter the value of Y2.

                  	<font color="blue">*</font>

                  	<input type="text" name="y2" length="7"/>            

                  	</font>
 

                  	<p>

                  	<input type="submit" value="Calculate"/>

                  	</p>

            	</form>
 

	<c:if test="${pageContext.request.method=='POST'}">

            	<c:choose>
 

		<c:when test="${status == 1}">

			<hr/><br/>

                        		<font size="4" face="Verdana" color="red">

                        		Please input numbers only.

			</font>

                  	</c:when> 
 

                  	<c:when test='${x1 == null}'>

                        		<hr/><br/>

			<font size="4" face="Verdana" color="red">

                        		You did not enter a value for X1.

			</font>

                  	</c:when>
 

                  	<c:when test='${y1 == null}'>

                        		<hr/><br/>

			<font size="4" face="Verdana" color="red">

                        		You did not enter a value for Y1.

			</font>

                  	</c:when>
 

                  	<c:when test='${x2 == null}'>

                        		<hr/><br/>

			<font size="4" face="Verdana" color="red">

                        		You did not enter a value for X2.

			</font>

                  	</c:when>
 

                  	<c:when test='${y2 == null}'>

                        		<hr/><br/>

			<font size="4" face="Verdana" color="red">

                        		You did not enter a value for Y2.

			</font>

                  	</c:when>
 

		<c:otherwise>

                                   	<hr/>

                        		<font face="Verdana"> <u>

                        		<h3>Output</h3>

                        		</u></font>
 

                        		<font size="4" face="Verdana" color="blue">

                        		X1 = ${x1} <br/>

                        		Y1 = ${y1} <br/>

                        		X2 = ${x2} <br/>

                        		Y2 = ${y2} <br/>

                        		</font>
 

                        		<br/>

                        		<font size="4" face="Verdana" color="blue"> 

                        		The distance between (X1, Y1) and (X2, Y2) is 

                        		<fmt:formatNumber value="${distanceBetweenPoints}" 

                                    	type="number" maxFractionDigits="15"/> 

			</font>
 

                  	</c:otherwise>

            	</c:choose>

	</c:if>

	

	<%--

	<font size="4" face="Verdana" color="green">

	<br/><br/>

        	x1=${x1} 

	<br/>

	y1=${y1} 

	<br/>

	x2=${x2} 

	<br/>

	y2=${y2}

	<br/>

        	distanceBetweenPoints=${distanceBetweenPoints}

	<br/>

	</font>

	--%>
 

      </body>

</html>

Open in new window

0
 

Author Closing Comment

by:CharleneS77
ID: 31469484
Thanks again for the superior assistance!
0

Featured Post

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

762 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now