Results 1 to 6 of 6
  1. #1
    sandraW is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Default JSP factorial of number and NumberFormatException

    The HTML that follows B displays a form prompting the user to enter a name and an integer. On clicking on the Submit button, Factorial.jsp is called.

    <html>
    <head>
    <title>Compute Factorial</title>
    </head>
    <body>
    <form method = "get"
    action = "http://localhost:8080/jsp-examples\Factorial.jsp">
    Enter Name
    <input type = "text" name = "name" /><br />
    Enter Integer
    <input type = "text" name = "number" /><br />
    <p>
    <input type = "submit" name = "Submit"/>
    <input type = "reset" value = "Reset" /></p>
    </form>
    </body>
    </html>



    I need help in writing the code for Factorial.jsp, which displays the user name and the factorial of the number entered in the HTML.

    Do I use the computeFactorial( ) method to calculate the factorial in Factorial.jsp and if so, how? :


    public long computeFactorial(int n) {
    if (n == 0)
    return 1;
    else
    return n * computeFactorial(n - 1);
    }


    If the user enters an incorrect number format or text when asked for an integer a NumberFormatException is thrown in Factorial.jsp.

    \How do i implement the JSP required to handle this error and display a relevant error page.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Don't do this in a JSP, do it in a Servlet and simply use the JSP to display the results/errors.

    It's a lot easier to structure your code in a sensible way if you avoid doing it in a JSP.

  3. #3
    sandraW is offline Member
    Join Date
    Aug 2010
    Posts
    11
    Rep Power
    0

    Default

    Thank you for your suggestion but I need to use JSP as it is a college assignment and that is what's expected of me. :)

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,015
    Rep Power
    20

    Default

    Tell your college they're twits.

    I don't touch code in JSPs if I can avoid it.

  5. #5
    arun9683 is offline Member
    Join Date
    Jun 2010
    Location
    Bangalore,India
    Posts
    70
    Rep Power
    0

    Default

    First of all the your first jsp is coded very badly..

    It is always good to use post method instead of get.

    in the action just another jsp page name is enough. not needed to use localhost ....

    As Tolls told it is not good practice to use JSP for business logic. Actually its not ment for that. If at all you want to do it in jsp you use the declarative tag to write the method.
    Arun K R,Bangalore,India
    :)

  6. #6
    murali.java is offline Member
    Join Date
    Oct 2010
    Posts
    7
    Rep Power
    0

    Smile

    i HOPE THIS CODE MIGHT BE USEFUL TO FULFILL YOUR REQUIREMENT.But its a good practise not to write the business logic in jsp file

    JSP FILE---------
    <html>
    <head>
    <title>Compute Factorial</title>
    </head>
    <body>
    <form method = "get" action = "/Factorial.jsp">
    Enter Name
    <input type = "text" name = "name" /><br />
    Enter Integer
    <input type = "text" name = "number" /><br />
    <p>
    <input type = "submit" name = "Submit"/>
    <input type = "reset" value = "Reset" /></p>
    </form>
    </body>
    </html>





    Factorial.jsp-------------



    <%!
    public long computeFactorial(int n) {
    if (n == 0)
    return 1;
    else
    return n * computeFactorial(n - 1);
    }
    %>




    <%
    String name=request.getParameter("name");
    int number=Integer.parseInt(request.getParameter("numb er"));
    %>


    ENTERED NAME=<%= name%>
    Factorial Of number is=<%= computeFactorial(number)%>

    %>

Similar Threads

  1. Factorial console program
    By er1c550n20 in forum New To Java
    Replies: 6
    Last Post: 04-13-2010, 02:07 PM
  2. Using method to call Factorial
    By hydride in forum New To Java
    Replies: 2
    Last Post: 03-02-2010, 02:16 AM
  3. Factorial
    By Anindo in forum New To Java
    Replies: 4
    Last Post: 07-28-2009, 09:46 PM
  4. NumberFormatException
    By Manfizy in forum New To Java
    Replies: 13
    Last Post: 07-09-2009, 11:59 AM
  5. [SOLVED] overflow when calculate factorial
    By ravinda in forum New To Java
    Replies: 6
    Last Post: 05-05-2009, 05:07 PM

Tags for this Thread

Posting Permissions

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