JSP code to calculate factorial - NESTED CODE || TECH FLOAT

Breaking

Post Top Ad

Post Top Ad

Tuesday, 30 October 2012

JSP code to calculate factorial

First JSP Page

Find out Factorial value by using jsp.

First install Netbeans Software. Then choose new project(web application). Now select a index.jsp page within "Web pages". and paste the following code to create a simple html page(i.e. your index page)

CODE :

<%-- 
    Document   : index
    Created on : 12 Oct, 2012, 7:35:11 PM
    Author     : nested code team
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body><br><br><center>
       <form action="fact.jsp ">
<h1>Enter the no :: <input type=text name=n ><br><br>
<input type=submit value="Submit"></h1>
</form></center>
    </body>
</html>

The Output will be like ::



 

Now we have to create the "fact.jsp" page, so choose again a new jsp page and give the name as "fact.jsp". Now paste the following code.
CODE :

<%-- 
    Document   : fact
    Created on : 12 Oct, 2012, 7:37:19 PM
    Author     : Nested code team
--%>
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
   "http://www.w3.org/TR/html4/loose.dtd">
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Page</title>
    </head>
    <body><center><h1>The required Factorial value is:: </h1>
        <h2>
            <%
            int n=0,i;
            int fact=1;
            String ns= request.getParameter("n");
            n=Integer.parseInt(ns);
            if(n>1)
                {
                fact=1;
                for(i=1;i<=n;i++)
                    {
                    fact=fact*i;
                    }
                }
            out.println("fact= "+fact);
               
           
            %>
           
            </h2></center>
    </body>
</html>



The Output for this code like ::


Note : For any further query, write down in comments box. You already created your first jsp page.  Now go for the next lesson. Thank you. 

Check out our latest blog on Spring boot -> click here -> http://techfloat.in/search/label/springboot

Donate Us click here -> https://imjo.in/t9cEFy
                                                           Next Lesson

No comments:

Post a Comment

Post Bottom Ad