Calculation in Servlets ???

I have the JSP scripts working as below.

<form method="post" action="">
  <table border="1" cellspacing="1" cellpadding="1" align="center">
    <tr>
      <td bgcolor="#FFFFCC">
        <div align="right"><b>Loan Amount ($):</b></div>
      </td>
      <td bgcolor="#CCCCFF">
        <input type="text" name="AMOUNT">
      </td>
    </tr>
    <tr>
      <td bgcolor="#FFFFCC">
        <div align="right"><b>Interest (%):</b></div>
      </td>
      <td bgcolor="#CCCCFF">
        <input type="text" name="INTEREST" size="6" maxlength="6">
      </td>
    </tr>
    <tr>
      <td bgcolor="#FFFFCC">
        <div align="right"><b>Term (Months):</b></div>
      </td>
      <td bgcolor="#CCCCFF">
        <input type="text" name="PERIOD" size="4" maxlength="4">
      </td>
    </tr>
    <tr>
      <td bgcolor="#FFFFCC">
        <div align="right"><b>Payment Plan:</b></div>
      </td>
      <td bgcolor="#CCCCFF">
        <select name="FREQUENCY">
          <option value="12">MONTHLY</option>
          <option value="4">QUARTERLY</option>
          <option value="2">BI-ANNUALLY</option>
          <option value="1">ANNUALLY</option>
        </select>
      </td>
    </tr>
    <tr>
      <td bgcolor="#FFFFCC" colspan="2">
        <div align="center">
          <input type="submit" name="Submit" value="Submit">
          <input type="reset" name="Reset" value="Reset">
        </div>
      </td>
    </tr>
  </table>
</form>

<%
String interest = request.getParameter("INTEREST").toString();
String amount = request.getParameter("AMOUNT").toString();
String period = request.getParameter("PERIOD").toString();
String frequency = request.getParameter("FREQUENCY").toString();
float I = 0f;
float A = 0f;
int T = 0;
int F = 0;
%>

<%
if ( interest == null || interest.length()==0){
     System.out.println("Empty String interest");
}
else
{
     I = Float.parseFloat(interest);
}
%>

<%
if ( amount == null || amount.length()==0){
     System.out.println("Empty String amount");
}
else
{
    A = Float.parseFloat(amount);
}
%>

<%
if ( period == null || period.length()==0){
     System.out.println("Empty String period");
}
else
{
     T = Integer.parseInt(period);
}
%>

<%
if ( frequency == null || frequency.length()==0){
     System.out.println("Empty String frequency");
}
else
{
     F = Integer.parseInt(frequency);
}
%>

  <%
  double M = (I/12)/100;
  int P = 12/F;
  double R1 = 1+M;
  double R = Math.pow(R1, P)-1;
  int N = F*T/12;  
  double L1 = 1+R;
  double L2 = Math.log (L1);
  double L3 = -N*L2;
  double L4 = Math.pow(Math.E, L3);
  double L5 = 1-L4;
  double MP = R*A/L5;
  double TP = N*MP;
  DecimalFormat money = new DecimalFormat("###,###.00");
%>

but, I like to know how to convert these codes into servlets.

tia

DanKangAsked:
Who is Participating?
 
koppchaConnect With a Mentor Commented:
It always better to do this kind on stuff on the client side only that is using the javascript.you can write a script in the between head tags which gets called when the user enters some thing in that function you can validate and alert the user to enter the right thing.By doing this the respose will be quick becuase it is on the client side...If you do the same thing on the server side thent he request has to come to the server and the validation should be done and then the response should again be sent to the user (which we can not afford to do for every validation) so JAVASCRIPT is the solution for your case
0
 
DanKangAuthor Commented:
more of JSP codes:

<table border="0" cellspacing="1" cellpadding="1" align="center">
  <tr>
    <td bordercolor="#FFFFCC" bgcolor="#FFCCFF" colspan="2">
      <div align="center"><b>Your Payments are:</b></div>
    </td>
  </tr>
  <tr>
    <td bordercolor="#FFFFCC" bgcolor="#FFFFCC"><b>PAYMENT</b></td>
    <td bgcolor="#CCCCFF"><div align="right"><strong>$</strong><%= money.format(MP) %></div></td>
  </tr>
  <tr>
    <td bordercolor="#FFFFCC" bgcolor="#FFFFCC"><b>TOTAL PAYOUT</b></td>
    <td bgcolor="#CCCCFF"><div align="right"><strong>$</strong><%= money.format(TP) %></div> </td>
  </tr>
</table>
0
 
suprapto45Commented:
Hi,

I will help you.

Regards
Dave
0
Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

 
suprapto45Commented:
Hi,

First, you need to have your Servlet. It is a .java file. A simple servlet will look like below.

package test;

import java.io.*;

import javax.servlet.http.*;
import javax.servlet.*;

public class HelloServlet extends HttpServlet {
  public void doGet (HttpServletRequest req,
                     HttpServletResponse res)
    throws ServletException, IOException
  {
    PrintWriter out = res.getWriter();

    out.println("Hello, world!");
    out.close();
  }
}

After you do that, you need to modify your web.xml.

Best Regards
Dave
0
 
suprapto45Commented:
For example, say that it is your main.jsp

-----------------------------------------------
<form method="post" action="/CalculationServlet">
  <table border="1" cellspacing="1" cellpadding="1" align="center">
    <tr>
      <td bgcolor="#FFFFCC">
        <div align="right"><b>Loan Amount ($):</b></div>
      </td>
      <td bgcolor="#CCCCFF">
        <input type="text" name="AMOUNT">
      </td>
    </tr>
    <tr>
      <td bgcolor="#FFFFCC">
        <div align="right"><b>Interest (%):</b></div>
      </td>
      <td bgcolor="#CCCCFF">
        <input type="text" name="INTEREST" size="6" maxlength="6">
      </td>
    </tr>
    <tr>
      <td bgcolor="#FFFFCC">
        <div align="right"><b>Term (Months):</b></div>
      </td>
      <td bgcolor="#CCCCFF">
        <input type="text" name="PERIOD" size="4" maxlength="4">
      </td>
    </tr>
    <tr>
      <td bgcolor="#FFFFCC">
        <div align="right"><b>Payment Plan:</b></div>
      </td>
      <td bgcolor="#CCCCFF">
        <select name="FREQUENCY">
          <option value="12">MONTHLY</option>
          <option value="4">QUARTERLY</option>
          <option value="2">BI-ANNUALLY</option>
          <option value="1">ANNUALLY</option>
        </select>
      </td>
    </tr>
    <tr>
      <td bgcolor="#FFFFCC" colspan="2">
        <div align="center">
          <input type="submit" name="Submit" value="Submit">
          <input type="reset" name="Reset" value="Reset">
        </div>
      </td>
    </tr>
  </table>
</form>
-------------------------------------------------------

Next your CalculationServlet.java
-------------------------------------------------------
public class CalculationServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       
String interest = request.getParameter("INTEREST").toString();
String amount = request.getParameter("AMOUNT").toString();
String period = request.getParameter("PERIOD").toString();
String frequency = request.getParameter("FREQUENCY").toString();
float I = 0f;
float A = 0f;
int T = 0;
int F = 0;

if ( interest == null || interest.length()==0){
     System.out.println("Empty String interest");
}
else
{
     I = Float.parseFloat(interest);
}

if ( amount == null || amount.length()==0){
     System.out.println("Empty String amount");
}
else
{
    A = Float.parseFloat(amount);
}

if ( period == null || period.length()==0){
     System.out.println("Empty String period");
}
else
{
     T = Integer.parseInt(period);
}

if ( frequency == null || frequency.length()==0){
     System.out.println("Empty String frequency");
}
else
{
     F = Integer.parseInt(frequency);
}

  double M = (I/12)/100;
  int P = 12/F;
  double R1 = 1+M;
  double R = Math.pow(R1, P)-1;
  int N = F*T/12;  
  double L1 = 1+R;
  double L2 = Math.log (L1);
  double L3 = -N*L2;
  double L4 = Math.pow(Math.E, L3);
  double L5 = 1-L4;
  double MP = R*A/L5;
  double TP = N*MP;
  DecimalFormat money = new DecimalFormat("###,###.00");

        response.setContentType("text/html");
        PrintWriter out = response.getWriter();

        String title = "Your Title";

        out.println("<HTML><HEAD><TITLE>"+title+"</TITLE></HEAD>");
        out.println("<BODY>");
        out.println("<table border='0' cellspacing='1' cellpadding='1' align='center'>" +
  "<tr>" +  
.
.
.
       "<td bordercolor="#FFFFCC" bgcolor="#FFFFCC"><b>PAYMENT</b></td>" +
    "<td bgcolor="#CCCCFF"><div align="right"><strong>$</strong>" + money.format(MP) + "</div></td>" +
.
.
.
"</table>");
        out.println("</BODY></HTML>");
       
    }

}
----------------------------------------------------------

Does it help?

Regards
Dave
0
 
suprapto45Commented:
Then...your WEB-INF/web.xml will be similar like.

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
                         "http://java.sun.com/dtd/web-app_2_3.dtd">
<!-- Copyright (c) 2002 by ObjectLearn. All Rights Reserved. -->
<web-app>
    <!-- Remove the comments below to define a servlet.          -->
    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
    <!--     <servlet>                                           -->
    <!--       <servlet-name>MyServlet</servlet-name>            -->
    <!--       <servlet-class>examples.MyServlet</servlet-class> -->
    <!--       <init-param>                                      -->
    <!--         <param-name>myparam</param-name>                -->
    <!--         <param-value>12345</param-value>                -->
    <!--       </init-param>                                     -->
    <!--     </servlet>                                          -->
    <!--                                                         -->
    <!--                                                         -->
    <!--     <servlet-mapping>                                   -->
    <!--           <servlet-name>MyServlet</servlet-name>        -->
    <!--           <url-pattern>/mine/*</url-pattern>            -->
    <!--     </servlet-mapping>                                  -->
    <!-- +++++++++++++++++++++++++++++++++++++++++++++++++++++++ -->
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
    <error-page>
        <error-code>404</error-code>
        <location>/error.jsp</location>
    </error-page>  
     
    <servlet>
        <servlet-name>CalculationServlet</servlet-name>
        <servlet-class>yourpackage.CalculationServlet</servlet-class>
    </servlet>      

    <servlet-mapping>
        <servlet-name>CalculationServlet</servlet-name>
        <url-pattern>/CalculationServlet</url-pattern>
    </servlet-mapping>        

</web-app>

I hope that helps.

Regards
Dave
0
 
koppchaCommented:
suprapto45
   Thats a good one.
0
 
koppchaCommented:
Again in the link i posted in your previous thread it clearly shows how to work with JSP and threads ...please go through it.
0
 
DanKangAuthor Commented:
out.println("<table border="0" cellspacing="1" cellpadding="1" align="center">" +
                            "<tr>"+
                            "<td bordercolor="#FFFFCC" bgcolor="#FFCCFF" colspan="2">"+
                            "<div align="center"><b>Your Payments are:</b></div>"+
                            "</td>"+
                            "</tr>"+
                            "<tr>"+
                            "<td bordercolor="#FFFFCC" bgcolor="#FFFFCC"><b>PAYMENT</b></td>"+
                            "<td bgcolor="#CCCCFF"><div align="right"><strong>$</strong>" +
                            "<%= money.format(MP) %></div></td>"+
                            "</tr>"+
                            "<tr>"+
                            "<td bordercolor="#FFFFCC" bgcolor="#FFFFCC"><b>TOTAL PAYOUT</b></td>"+
                            "<td bgcolor="CCCCFF"><div align="right"><strong>$</strong>" +
                            "<%= money.format(TP) %></div> </td>"+
                            "</tr>" +
                            "</table>");
                out.println("</BODY></HTML>");

I am gettnig:

ClientServerLoanCalc\src\CalculationServlet.java:72: ')' expected
                out.println("<table border="0" cellspacing="1" cellpadding="1" align="center">" +

Also, how do you handle # for i.e. #FFFFCC?  I think it is a special character.

tia
0
 
DanKangAuthor Commented:
\# seems not working.
\ --> escape key
0
 
koppchaCommented:
ok there is some commenting problem here
if you want to escape " you have to write \"
in the string s="<table border="0""
0 will not get in to the string so just skip it by \"0\"
so when it prints it will be =<table border="0"
just make sure you are escaping all the special characters with \
0
 
koppchaCommented:
for some of them you have to use \\   just check i am not sure in particular what they are
0
 
DanKangAuthor Commented:
OK.  This takes care of the error.

                out.println("<table border=\"0\" cellspacing=\"1\" cellpadding=\"1\" align=\"center\">" +
                            "<tr>"+
                            "<td bordercolor=\"#FFFFCC\" bgcolor=\"#FFCCFF\" colspan=\"2\">"+
                            "<div align=\"center\"><b>Your Payments are:</b></div>"+
                            "</td>"+
                            "</tr>"+
                            "<tr>"+
                            "<td bordercolor=\"#FFFFCC\" bgcolor=\"#FFFFCC\"><b>PAYMENT</b></td>"+
                            "<td bgcolor=\"#CCCCFF\"><div align=\"right\"><strong>$</strong>" +
                            "<%= money.format(MP) %></div></td>"+
                            "</tr>"+
                            "<tr>"+
                            "<td bordercolor=\"#FFFFCC\" bgcolor=\"FFFFCC\"><b>TOTAL PAYOUT</b></td>"+
                            "<td bgcolor=\"#CCCCFF\"><div align=\"right\"><strong>$</strong>" +
                            "<%= money.format(TP) %></div> </td>"+
                            "</tr>" +
                            "</table>");
0
 
DanKangAuthor Commented:
I have:

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.*;

public class CalculationServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       

When I complie, I am getting:

init:
deps-jar:
ClientServerLoanCalc\src\CalculationServlet.java:3: package javax.servlet does not exist
import javax.servlet.*;
ClientServerLoanCalc\src\CalculationServlet.java:4: package javax.servlet.http does not exist
import javax.servlet.http.*;
ClientServerLoanCalc\src\CalculationServlet.java:7: cannot resolve symbol
symbol  : class HttpServlet
location: class CalculationServlet
public class CalculationServlet extends HttpServlet {
ClientServerLoanCalc\src\CalculationServlet.java:9: cannot resolve symbol
symbol  : class HttpServletRequest
location: class CalculationServlet
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ClientServerLoanCalc\src\CalculationServlet.java:9: cannot resolve symbol
symbol  : class HttpServletResponse
location: class CalculationServlet
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
ClientServerLoanCalc\src\CalculationServlet.java:9: cannot resolve symbol
symbol  : class ServletException
location: class CalculationServlet
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
6 errors

Am I missing something?

tia
0
 
koppchaCommented:
you have to keep j2ee.jar files in your class path.
0
 
DanKangAuthor Commented:
Can you please explain further?  I am compiling it in my PC and think I have to put the .class file after the compilation.   Then, where do I need to j2ee.jar file?
0
 
koppchaCommented:
ok i assume you have downloaded j2eexxx (some verison) in that folder in the lib directory you will be having j2ee.jar.you have to point to this file in the class path

Setting the class path
----------------------------
Assuming you are working on windows
1>start->settings->control panel-->system->advanced->Environment variables
2>In the user variable click new
3>In the variable name give CLASSPATH
4>In the variable values point the jar files as i said like C:\j2ee1.4sdk\lib\j2ee.jar
5>click ok

then you are all set run the servlet program now
0
 
DanKangAuthor Commented:
OK.  I am using C:\j2sdk1.4.2_07
What do I need to do then?

tia
0
 
DanKangAuthor Commented:
and I am using netBeans IDE 4.0.

tia
0
 
koppchaCommented:
you have to install j2ee some version  from sun web site
http://java.sun.com/j2ee/1.4/download.html
0
 
DanKangAuthor Commented:
I complied and clicked on submit with:

<form method="post" action="servlet/CalculationServlet">

but, I am getting:

type Status report

message HTTP method POST is not supported by this URL

description The specified HTTP method is not allowed for the requested resource (HTTP method POST is not supported by this URL).

What is wrong with this?

tia
0
 
DanKangAuthor Commented:
I got it.   It shoud be <form action="servlet/CalculationServlet">.  Then why?
0
 
koppchaCommented:
we will explain later but for now you can try with this
<form method="get" action="servlet/CalculationServlet">
 

0
 
koppchaCommented:
you can either use post or get
In your servlet you are currently supporting get  only so you got that error
you can write the post method in servlet and inside that method you can call again the get method only.
0
 
DanKangAuthor Commented:
Great!

It works fine and displays the values correctly. However, when strings are entered, I am getting:

type Exception report

message

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

exception

java.lang.NumberFormatException: For input string: "sfdf"
      sun.misc.FloatingDecimal.readJavaFormatString(FloatingDecimal.java:1224)
      java.lang.Float.parseFloat(Float.java:394)
      CalculationServlet.doGet(CalculationServlet.java:33)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      org.apache.catalina.servlets.InvokerServlet.serveRequest(InvokerServlet.java:466)
      org.apache.catalina.servlets.InvokerServlet.doGet(InvokerServlet.java:180)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:743)
      javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
      sun.reflect.GeneratedMethodAccessor101.invoke(Unknown Source)
      sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
      java.lang.reflect.Method.invoke(Method.java:585)
      org.apache.catalina.security.SecurityUtil$1.run(SecurityUtil.java:284)
      java.security.AccessController.doPrivileged(Native Method)
      javax.security.auth.Subject.doAsPrivileged(Subject.java:517)
      org.apache.catalina.security.SecurityUtil.execute(SecurityUtil.java:306)
      org.apache.catalina.security.SecurityUtil.doAsPrivilege(SecurityUtil.java:200)


note The full stack trace of the root cause is available in the Tomcat logs.

tia
0
 
koppchaCommented:
Integer.parseInt() and
Float.parseFloat() would through this exception they they can not parse so you have to catch this exception
try {
Integer.parseInt();
}
catch(NumberFormatException e) {
//I go tthe exception
}
0
 
DanKangAuthor Commented:
I put:

        try {
            Integer.parseInt();
        }
        catch(NumberFormatException e) {
        System.out.println("Invalid Data Entry");
        }
       
       try {
            Float.parseFloat();
        }
        catch(NumberFormatException e) {
        System.out.println("Invalid Data Entry");
        }

I got:

CalculationServlet.java:22: cannot resolve symbol
symbol  : method parseInt ()
location: class java.lang.Integer
            Integer.parseInt();
                   ^
CalculationServlet.java:29: parseFloat(java.lang.String) in java.lang.Float cannot be applied to ()
            Float.parseFloat();
                 ^
2 errors

I must be missing something here.

tia
0
 
DanKangAuthor Commented:
What is have so far is:

import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.text.*;

public class CalculationServlet extends HttpServlet {

    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
       
        String interest = request.getParameter("INTEREST").toString();
        String amount = request.getParameter("AMOUNT").toString();
        String period = request.getParameter("PERIOD").toString();
        String frequency = request.getParameter("FREQUENCY").toString();
        float I = 0f;
        float A = 0f;
        int T = 0;
        int F = 0;

       
        try {
            Integer.parseInt();
        }
        catch(NumberFormatException e) {
        System.out.println("Invalid Data Entry");
        }
       
       try {
            Float.parseFloat();
        }
        catch(NumberFormatException e) {
        System.out.println("Invalid Data Entry");
        }
       
        if ( interest == null || interest.length()==0){
             System.out.println("Empty String interest");
        }
        else
        {
             I = Float.parseFloat(interest);
        }

        if ( amount == null || amount.length()==0){
             System.out.println("Empty String amount");
        }
        else
        {
            A = Float.parseFloat(amount);
        }

        if ( period == null || period.length()==0){
             System.out.println("Empty String period");
        }
        else
        {
             T = Integer.parseInt(period);
        }

        if ( frequency == null || frequency.length()==0){
             System.out.println("Empty String frequency");
        }
        else
        {
             F = Integer.parseInt(frequency);
        }

          double M = (I/12)/100;
          int P = 12/F;
          double R1 = 1+M;
          double R = Math.pow(R1, P)-1;
          int N = F*T/12;  
          double L1 = 1+R;
          double L2 = Math.log (L1);
          double L3 = -N*L2;
          double L4 = Math.pow(Math.E, L3);
          double L5 = 1-L4;
          double MP = R*A/L5;
          double TP = N*MP;
          DecimalFormat money = new DecimalFormat("###,###.00");

                response.setContentType("text/html");
                PrintWriter out = response.getWriter();

                String title = "Loan Calculation";

                out.println("<HTML><HEAD><TITLE>"+title+"</TITLE></HEAD>");
                out.println("<BODY>");
               
                out.println("<table border=\"0\" cellspacing=\"1\" cellpadding=\"1\" align=\"center\">" +
                            "<tr bgcolor=\"#FFCCFF\">" +
                            "<td bordercolor=\"#CCCCCC\" colspan=\"2\">" +
                            "<div align=\"center\"><b><font color=\"#000000\">" +
                            "Your Inputs are:</font></b></div>" +
                            "</td>" +
                            "</tr>" +
                            "<tr>" +
                            "<td bordercolor=\"#CCCCCC\" bgcolor=\"#FFFFCC\">" +
                            "<div align=\"right\"><b><font color=\"#000000\">INTEREST (%):</font></b></div>" +
                            "</td>" +
                            "<td bgcolor=\"#CCCCFF\"><div align=\"right\">" + I + "</div></td>" +
                            "</tr>" +
                            "<tr>" +
                            "<td bordercolor=\"#CCCCCC\" bgcolor=\"#FFFFCC\">" +
                            "<div align=\"right\"><b><font color=\"#000000\">AMOUNT ($):</font></b></div>" +
                            "</td>" +
                            "<td bgcolor=\"#CCCCFF\"><div align=\"right\">" + A + "</div></td>" +
                            "</tr>" +
                            "<tr>" +
                            "<td bordercolor=\"#CCCCCC\" bgcolor=\"#FFFFCC\">" +
                            "<div align=\"right\"><b><font color=\"#000000\">PERIOD:</font></b></div>" +
                            "</td>" +
                            "<td bgcolor=\"#CCCCFF\"><div align=\"right\">" + T + "</div></td>" +
                            "</tr>" +
                            "<tr>" +
                            "<td bordercolor=\"#CCCCCC\" bgcolor=\"#FFFFCC\">" +
                            "<div align=\"right\"><b><font color=\"#000000\">FREQUENCY/YEAR:</font></b></div>" +
                            "</td>" +
                            "<td bgcolor=\"#CCCCFF\"><div align=\"right\">" + F + "</div></td>" +
                            "</tr>" +
                            "</table>" +
                            "<p></p>");
               
                out.println("<table border=\"0\" cellspacing=\"1\" cellpadding=\"1\" align=\"center\">" +
                            "<tr>"+
                            "<td bordercolor=\"#FFFFCC\" bgcolor=\"#FFCCFF\" colspan=\"2\">"+
                            "<div align=\"center\"><b>Your Payments are:</b></div>"+
                            "</td>"+
                            "</tr>"+
                            "<tr>"+
                            "<td bordercolor=\"#FFFFCC\" bgcolor=\"#FFFFCC\"><b>PAYMENT</b></td>"+
                            "<td bgcolor=\"#CCCCFF\"><div align=\"right\"><strong>$</strong>" +
                            money.format(MP) +
                            "</div></td>"+
                            "</tr>"+
                            "<tr>"+
                            "<td bordercolor=\"#FFFFCC\" bgcolor=\"FFFFCC\"><b>TOTAL PAYOUT</b></td>"+
                            "<td bgcolor=\"#CCCCFF\"><div align=\"right\"><strong>$</strong>" +
                            money.format(TP) +
                            "</div> </td>"+
                            "</tr>" +
                            "</table>");
                out.println("</BODY></HTML>");
            }
}
0
 
koppchaCommented:
You should keep those statements where ever you are parsing it not just like that
try {
            Integer.parseInt();
        }
        catch(NumberFormatException e) {
        System.out.println("Invalid Data Entry");
        }
       
       try {
            Float.parseFloat();
        }
        catch(NumberFormatException e) {
        System.out.println("Invalid Data Entry");
        }



In your code where ever your using parseInt or parseFloat it should be encapsulated with the try and catch block mentioned above for instance
here is the change follow the same thing where ever it applies

if ( interest == null || interest.length()==0){
             System.out.println("Empty String interest");
        }
        else
        {
           try{
             I = Float.parseFloat(interest);
            }
             catch(NumberFormatException e) {
           System.out.println("Interest can not be parsed");
           }

        }

0
 
DanKangAuthor Commented:
OK, I got it.

But, why is "System.out.println("Interest can not be parsed");" not showing up the error message (Interest can not be parsed)?

tia
0
 
koppchaCommented:
It will written to you application server console not to the JSP
0
 
DanKangAuthor Commented:
Then, in servlet, what is the best way to let the users know that they entered wrong values (string instead of int or float) or empty values?
0
 
koppchaCommented:
out.println() the same thing what you were doing
not System.out.println it is just out.println here out is object of PrintWriter
0
 
DanKangAuthor Commented:
System.out.println is if { } esle { }

out.println is a part of HTML.  How do you incorporate????

tia
0
 
koppchaCommented:
Even though it is servlet it is a java file so
out.println()will work but it shoulf be wriitten following the HTML tags.Just check the flow it may have some syntax errors.
in the servlet

out.println("<HTML>");
out.println("<head><title>EE</title></head><body>");
if () {
out.println("Wrong values Entered");
}
else
{
out.println("Correct values enteres");
}

out.println("</body></html>");
0
 
DanKangAuthor Commented:
Great!  This takes care of that.

               out.println("<table border=\"0\" cellspacing=\"1\" cellpadding=\"1\" align=\"center\">");
               if ( interest == null || interest.length()==0){
               out.println ("<tr><td><b><font size=\"4\" color=\"#FF0000\">Please enter Interest!</font></b></td></tr>");
               }
               else{    
               }
               if ( amount == null || amount.length()==0){
               out.println ("<tr><td><b><font size=\"4\" color=\"#FF0000\">Please enter Amount!</font></b></td></tr>");
               }
               else{    
               }      
               if ( period == null || period.length()==0){
               out.println ("<tr><td><b><font size=\"4\" color=\"#FF0000\">Please enter Term!</font></b></td></tr>");
               }
               else{    
               }  
               out.println ("</table>");
0
 
DanKangAuthor Commented:
One last thing is:

How do you display when the users enter strings instead of int or float?
I like to display " please enter int" or "please enter number".

tia
0
 
DanKangAuthor Commented:
That is wonderful.
Thank you so much for your help.

0
 
koppchaCommented:
Thanks for the Grade :)

Take it Easy
Good Luck
0
 
DanKangAuthor Commented:
koppcha,
You are the best!
0
 
koppchaCommented:
:)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.