Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?
Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Use Compound Statement in JSP Code 
 

In this section, ypou will learn how to use of Compound Statement in JSP.

Use Compound Statement in JSP Code

                         

A compound statement is collection of statements enclosed with in braces. In general, statement works without braces but it only execute single statement and rest of the statements takes as normal statement and execute them.

Example :

if( boolean condition)
   statement1;
   statement2;
   statement3;

Here in the above code only statement1 will be execute if boolean condition gets true and rest statements execute as a normal statement.

if( boolean condition) {
   statement1;
   statement2;
   statement3;
}

In the code above all the three statements are executed if boolean condition gets true because all the statements are bound with braces in if condition.

Compound statements are used with:

if statement
while statement
for statement
try statement
method definition
class definition etc.

Create an application directory named "user" in the tomcat-6.0.16/webapps. The following jsp code will show you how to use compound statement.

compound_statement.jsp

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<HTML>
<HEAD>
    <TITLE>Compound statement in jsp</TITLE>
</HEAD>
<BODY bgcolor="#6E6E6E">
    <FORM NAME="form1" ACTION="compound_statement.jsp" METHOD="get">           
        <TABLE bgcolor="#D8D8D8">
        <tr>
            <td> Enter number </td>
            <td><input type="text" name="num"></td>
        </tr>
        <tr align="center"><td></td>
        <td><INPUT TYPE="submit" VALUE="check"></td></tr></TABLE>
	<br>
	<TABLE bgcolor="#E0ECF8"><tr><td>
    </FORM>
    
    <% try {
	   // Here if statement is used for more than one statement.
           if (request.getParameter("num") != null && 
               request.getParameter("num")!=""){
	       // Compound statement is used in 'if' statement
               if (Long.parseLong(request.getParameter("num"))>0 ){
                   out.println("You have given a positive number.<br>");
                   out.println("Absolute value is = " + 
		         Math.abs(Long.parseLong(request.getParameter("num"))));
              }
	       if (Long.parseLong(request.getParameter("num"))<0 ){
                   out.println("You have given a negative number.<br>");
                   out.println("Absolute value is = " + 
		   Math.abs(Long.parseLong(request.getParameter("num"))));
               }
	       // only single statement is used in 'if' statement
	       if (Long.parseLong(request.getParameter("num"))==0 )
                   out.println("You have entered ZERO.");
	      }    
	  }
	  catch(Exception ex){
		  out.println("<font color=\"red\">You have entered 
                  wrong input value</font>");
		  out.println("<font color=\"green\">Try again</font>");
	  }
    %>
 </BODY>
</HTML>

Save this code as a .jsp file named "compound_statement.jsp" in the directory Tomcat-6.0.16/webapps/user/. Run tomcat server by clicking on startup.bat file in bin directory of tomcat. Run this jsp page with following url in address bar of the browser: http://localhost:8080/user/compound_statement.jsp

This is the first page, user enter valid digit in the text box and click check button. This application check that given number is positive or negative or zero and show appropriate message.

When user click on check button.......

If user enters invalid value in text box then this application shows an error message.....

Download Source Code

                         

» View all related tutorials
Related Tags: c exception error jsp io count page using invoke js handle if to ci counter e directive can pe ce

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.