Example : How to Display Colorful  Message in JSP?
// To display colorful text messages.
  out.println("<span style='color:red'>Codershelpline to all</span>");
  
//to display colorful background.   
  out.println("<span style='background:green'>Codershelpline to all</span>"); 
Example : Write a JSP program to create a Simple Calculator.
<%-- 
    Document   : newjsp
    Created on : 8 Mar, 2024, 8:31:16 PM
    Author     : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<%               
    String sub1=request.getParameter("sub");    
    
    try
    {
        if(sub1!=null)
        {    
            int val1=Integer.parseInt(request.getParameter("num1"));
            int val2=Integer.parseInt(request.getParameter("num2"));
            
            //String value1=(request.getParameter("num1"));
            //String value2=(request.getParameter("num2"));            
            //int val1=Integer.parseInt(value1);
            //int val2=Integer.parseInt(value2);
            
            if(sub1.equals("Addition"))
            {
               int result1= val1 + val2;
               out.println("The addition result is = " + result1);
            }
            
            else if(sub1.equals("Subtraction"))
            {
                int result2= val1 - val2;
                out.println("The subtraction result is = " + result2);                
            }  
            
            else if(sub1.equals("Multiplication"))
            {
                int result3= val1 * val2;
                out.println("The multiplication result is = " +result3);                
            }
            
            else if(sub1.equals("Division"))
            {               
                int result4= val1 / val2;
                out.println("The division result is = " +result4); 
            }
            
            else
            {               
                int result5= val1 % val2;
                out.println("The remainder division result is = " +result5); 
            } 
        }
    }

    catch(Exception e)
    {
        out.println(e);        
    }    

%>

<!DOCTYPE html>
<html>

    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Simple Calculator</title>
        <h1><center>Basic Calculator Program</center></h1>
    </head>

    <body>
        <center>
            <form action="#" method="post">

                    <label><b>Number 1</b></label>
                    <input type="text" name ="num1" placeholder="Enter First Number"><br><br>
                    <label><b>Number 2</b></label>
                    <input type="text" name="num2" placeholder="Enter Second Number"><br><br>

                    <input type ="submit" name = "sub" value="Addition">
                    <input type = "submit" name = "sub" value="Subtraction">
                    <input type="submit" name="sub" value ="Multiplication">
                    <input type = "submit" name="sub" value="Division">
                    <input type = "submit" name="sub" value="Mod Division">
                    
                    <input type="reset" value="Reset">
            </form>
	</center>
    </body>
</html>

-----------------  OR  -------------------

<%-- 
    Document   : newjsp
    Created on : 8 Mar, 2024, 8:31:16 PM
    Author     : admin
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <title>JSP Simple Calculator</title>
        <h1><center>Basic Calculator Program</center></h1>
    </head>
    <body>        
        
        <%            
            try
            {
                if (request.getParameter("sub") != null)
                {    
                    int val1=Integer.parseInt(request.getParameter("num1"));
                    int val2=Integer.parseInt(request.getParameter("num2"));

                    //String value1=(request.getParameter("num1"));
                    //String value2=(request.getParameter("num2"));

                    //int val1=Integer.parseInt(value1);
                    //int val2=Integer.parseInt(value2);

                    if(request.getParameter("sub").equals("Addition"))
                    {
                       int result1= val1+val2;
                       out.println("The addition result is = " + result1);
                    }

                    else if(request.getParameter("sub").equals("Subtraction"))
                    {
                        int result2= val1-val2;
                        out.println("The subtraction result is = " + result2);                
                    }  

                    else if(request.getParameter("sub").equals("Multiplication"))
                    {
                        int result3= val1*val2;
                        out.println("The multiplication result is = " +result3);                
                    }

                    else if(request.getParameter("sub").equals("Division"))
                    {               
                        int result4= val1/val2;
                        out.println("The division result is = " +result4); 
                    }

                    else
                    {               
                        int result5= val1%val2;
                        out.println("The remainder division result is = " +result5); 
                    } 
                }
            }
            catch(Exception e)
            {
                out.println(e);        
            }
        %>        
        
        <center>
            <form action="#" method="post">

                    <label><b>Number 1</b></label>
                    <input type="text" name ="num1" placeholder="Enter First Number"><br><br>
                    <label><b>Number 2</b></label>
                    <input type="text" name="num2" placeholder="Enter Second Number"><br><br>

                    <input type ="submit" name = "sub" value="Addition">
                    <input type = "submit" name = "sub" value="Subtraction">
                    <input type="submit" name="sub" value ="Multiplication">
                    <input type = "submit" name="sub" value="Division">
                    <input type = "submit" name="sub" value="Mod Division">
                    
                    <input type="reset" value="Reset">
            </form>
	</center>
    </body>
</html>

Loading

Categories: JSP

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.