• Skip to main content
  • Skip to primary sidebar
  • Skip to secondary sidebar
  • Skip to footer

Computer Notes

Library
    • Computer Fundamental
    • Computer Memory
    • DBMS Tutorial
    • Operating System
    • Computer Networking
    • C Programming
    • C++ Programming
    • Java Programming
    • C# Programming
    • SQL Tutorial
    • Management Tutorial
    • Computer Graphics
    • Compiler Design
    • Style Sheet
    • JavaScript Tutorial
    • Html Tutorial
    • Wordpress Tutorial
    • Python Tutorial
    • PHP Tutorial
    • JSP Tutorial
    • AngularJS Tutorial
    • Data Structures
    • E Commerce Tutorial
    • Visual Basic
    • Structs2 Tutorial
    • Digital Electronics
    • Internet Terms
    • Servlet Tutorial
    • Software Engineering
    • Interviews Questions
    • Basic Terms
    • Troubleshooting
Menu

Header Right

Home » JSP » Elements » Servlet to JSP Communication
Next →
← Prev

Servlet to JSP Communication

By Dinesh Thakur

The intention is to introduce the communication in between the servlet and JSP. The welcome page is designed by the HTML code.

                                                                   

    <html>

    <title>Srv-Jsp Communication</title>

    <form action=”a.jsp”>

    <body>

       <center><font color=”black” size=”8″>Enter A Number:</font>

          <input type=’text’size=’50’ name=”t1″><br><br>

          <input type=’submit’ value=’submit’ name=”b1″ />

          &nbsp;<input type=’reset’ value=’reset’ name=’b2’/>

       </center>

   </body>

  </form>

 </html>

Output:

       

“a.jsp”

 

<html>

  <%

  try

    {

       int no=Integer.parseInt(request.getParameter(“t1”));

       int res=no*no;

       if(res>=100)

          {

            out.println(“<center><font color=’red’ size=’8′>”);

            out.println(“Square of The Number is=”+res);

            out.println(“</font></center>”);

         }

           else

                  {

                        out.println(“<br><br><center><font color=’red’ size=’6′>Your selection Does   

                       not Vary The Condition..”);

                 }

    }

        catch(Exception e)

           {

               System.out.println(e);

           }

    %>

  </html>

Output:

         

Explanation of the above program

The aim of this program is to find out the square of the number if the result is greater than 100, then it goes to the JSP program, otherwise it goes to servlet program.

As the JSP program communicates, with the servlet program, the URL pattern of the servlet program is passed into the form action.

• int no=Integer.parseInt(request.getParameter(“tl”));

The number which is passed that is read by the getParameter() and assign in the no variable of int type.

• int res=no*no;

The square of the number is calculated and assigned to the res variable of int type.

• if(res>=100)

{

out.println(“<center><font color=’red’ size=’8′>”) ;

out.println(“Square Of The Number Is=”+res);

out.println(“</font></center>”) ;

}

If the result requested is greater than 100, then it calculates the square of the number.

• else

{

RequestDispatcher=request.getRequestDispatcher{“/Srv·);

rd.forward(request,response);

}

If the result is less than 100,we pass the request then through the RequestDispatcher object to the servlet program, where the cube of the number is calculated.

• catch(Exception e)

{

System.out.println(e) ;

}

The exception is handled by the catch block.

The square of the number 25 is 625 and it is greater than 100, so the request goes to the SP page and processing is done.

But if the square of the number is greater than or less than 100, then the request goes to the servlet program and calculates the cube of the number.

This time we pass the number 2 and the square of the number is 4 and it is less than 100, so the request goes to the Servlet program through the RequestDispatcher object that’s why we pass the URL pattern of the servlet program in RequestDispatch method.

You’ll also like:

  1. Jsp To Servlet Communication
  2. Html To JSP Communication
  3. Database To JSP Communication
  4. Why is it optional to configure web.xml file in case of JSP as it is mandatory for servlet program?
  5. Html to Servlet Communication
Next →
← Prev
Like/Subscribe us for latest updates     

About Dinesh Thakur
Dinesh ThakurDinesh Thakur holds an B.C.A, MCDBA, MCSD certifications. Dinesh authors the hugely popular Computer Notes blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps.

Dinesh Thakur is a Freelance Writer who helps different clients from all over the globe. Dinesh has written over 500+ blogs, 30+ eBooks, and 10000+ Posts for all types of clients.


For any type of query or something that you think is missing, please feel free to Contact us.


Primary Sidebar

Java Server Pages

Java Server Pages

  • JSP - Home
  • JSP - Advantages
  • JSP - Architecture
  • JSP - Life Cycle
  • JSP - Servlet Disadvantages
  • JSP - PrintWriter Vs JspWriter
  • JSP - Session Tracking
  • JSP - Stateless Protocol
  • JSP - Hidden Form Fields
  • JSP - Program Structure
  • JSP - Bill Discount Program
  • JSP - Scripting Elements
  • JSP - fn:length()
  • JSP - fn:split()
  • JSP - fmt formatNumber Tag
  • JSP - Servlet Communication
  • JSP - fn:replace()
  • JSP - fmt:parseNumber Tag
  • JSP - fmt:parseDate Tag
  • JSP - fn:substring()

Other Links

  • JSP - PDF Version

Footer

Basic Course

  • Computer Fundamental
  • Computer Networking
  • Operating System
  • Database System
  • Computer Graphics
  • Management System
  • Software Engineering
  • Digital Electronics
  • Electronic Commerce
  • Compiler Design
  • Troubleshooting

Programming

  • Java Programming
  • Structured Query (SQL)
  • C Programming
  • C++ Programming
  • Visual Basic
  • Data Structures
  • Struts 2
  • Java Servlet
  • C# Programming
  • Basic Terms
  • Interviews

World Wide Web

  • Internet
  • Java Script
  • HTML Language
  • Cascading Style Sheet
  • Java Server Pages
  • Wordpress
  • PHP
  • Python Tutorial
  • AngularJS
  • Troubleshooting

 About Us |  Contact Us |  FAQ

Dinesh Thakur is a Technology Columinist and founder of Computer Notes.

Copyright © 2025. All Rights Reserved.

APPLY FOR ONLINE JOB IN BIGGEST CRYPTO COMPANIES
APPLY NOW