[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Calculation in Servlets ???

Posted on 2005-04-20
41
Medium Priority
?
297 Views
Last Modified: 2010-04-01
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

0
Comment
Question by:DanKang
  • 20
  • 17
  • 4
41 Comments
 

Author Comment

by:DanKang
ID: 13831025
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
 
LVL 16

Expert Comment

by:suprapto45
ID: 13831152
Hi,

I will help you.

Regards
Dave
0
 
LVL 16

Expert Comment

by:suprapto45
ID: 13831155
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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 16

Expert Comment

by:suprapto45
ID: 13831165
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
 
LVL 16

Expert Comment

by:suprapto45
ID: 13831263
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13833273
suprapto45
   Thats a good one.
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13833287
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
 

Author Comment

by:DanKang
ID: 13833754
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
 

Author Comment

by:DanKang
ID: 13833772
\# seems not working.
\ --> escape key
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13833815
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13833819
for some of them you have to use \\   just check i am not sure in particular what they are
0
 

Author Comment

by:DanKang
ID: 13834028
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
 

Author Comment

by:DanKang
ID: 13834053
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13834210
you have to keep j2ee.jar files in your class path.
0
 

Author Comment

by:DanKang
ID: 13834540
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13834679
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
 

Author Comment

by:DanKang
ID: 13834823
OK.  I am using C:\j2sdk1.4.2_07
What do I need to do then?

tia
0
 

Author Comment

by:DanKang
ID: 13834883
and I am using netBeans IDE 4.0.

tia
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13834918
you have to install j2ee some version  from sun web site
http://java.sun.com/j2ee/1.4/download.html
0
 

Author Comment

by:DanKang
ID: 13835811
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
 

Author Comment

by:DanKang
ID: 13835869
I got it.   It shoud be <form action="servlet/CalculationServlet">.  Then why?
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13835875
we will explain later but for now you can try with this
<form method="get" action="servlet/CalculationServlet">
 

0
 
LVL 8

Expert Comment

by:koppcha
ID: 13835885
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
 

Author Comment

by:DanKang
ID: 13836107
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13836519
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
 

Author Comment

by:DanKang
ID: 13836736
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
 

Author Comment

by:DanKang
ID: 13836755
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13836979
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
 

Author Comment

by:DanKang
ID: 13837862
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13837972
It will written to you application server console not to the JSP
0
 

Author Comment

by:DanKang
ID: 13838134
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
 
LVL 8

Expert Comment

by:koppcha
ID: 13838204
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
 

Author Comment

by:DanKang
ID: 13838331
System.out.println is if { } esle { }

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

tia
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13838435
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
 

Author Comment

by:DanKang
ID: 13839239
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
 

Author Comment

by:DanKang
ID: 13839248
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
 
LVL 8

Accepted Solution

by:
koppcha earned 2000 total points
ID: 13839274
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
 

Author Comment

by:DanKang
ID: 13839290
That is wonderful.
Thank you so much for your help.

0
 
LVL 8

Expert Comment

by:koppcha
ID: 13839304
Thanks for the Grade :)

Take it Easy
Good Luck
0
 

Author Comment

by:DanKang
ID: 13839321
koppcha,
You are the best!
0
 
LVL 8

Expert Comment

by:koppcha
ID: 13839363
:)
0

Featured Post

Prep for the ITIL® Foundation Certification Exam

December’s Course of the Month is now available! Enroll to learn ITIL® Foundation best practices for delivering IT services effectively and efficiently.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Moore’s Law has proven itself time and time again since it was first introduced. So what’s next? Will Moore’s law continue to remain relevant, or will new technology take over and bring us the next big advancement in computing?
LinkedIn blogging is great for networking, building up an audience, and expanding your influence as well. However, if you want to achieve these results, you need to work really hard to make your post worth liking and sharing. Here are 4 tips that ca…
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

829 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