Latest Tutorials| Questions and Answers|Ask Questions?|Site Map



Home Jsp How to use switch statement in jsp code

Related Tutorials


 
 

Share on Google+Share on Google+

How to use switch statement in jsp code

Advertisement
Switch is a type of control statement used to control the flow of program execution.

How to use switch statement in jsp code

     

switch is a type of control statement used to control the flow of program execution. Its body is known as a switch block. The block can have one or more case blocks which contains some statements to be executed when the case is selected. Each case block starts with the keyword 'case' and some expression. The expression is evaluated and if matched correctly with the switch expression then the statements under the case are executed.

Default case : Default case is a special case executed when none of the conditions being tested for in the switch statement are met or executed.

Break Statement : The break statement must be used after each condition because it signifies the end of the block Main reason is to use break statement is find proper output if it is not used then all the conditions from the one met will be executed and that will be an error.

The example below demonstrates how we can use switch statement in our JSP code.

switch_statement_jsp.jsp

<HTML>
<HEAD>
    <TITLE>use switch statement in jsp page</TITLE>
</HEAD>
<BODY bgcolor="#6E6E6E">
    <FORM NAME="form1" ACTION="jsp_with_post_method.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="show"></td></tr></TABLE>
		<br>
		<TABLE bgcolor="#E0ECF8"><tr><td>
    </FORM>
    
    <%
      if (request.getParameter("num") != null &&
          request.getParameter("num")!=""){
	  for(int i=0; i<request.getParameter("num").length(); i++){
		char value = request.getParameter("num").charAt(i);
		// switch statement with some value
		switch(value){
		   case '0':
			out.print("ZERO");
			break;
		   case '1':
			out.print("ONE");
			break;
		   case '2':
			out.println("TWO");
			break;
		   case '3':
			out.println("THREE");
			break;
		   case '4':
			out.println("FOUR");
			break;
		   case '5':
			out.println("FIVE");
			break;
		   case '6':
			out.println("SIX");
			break;
		   case '7':
			out.println("SEVEN");
			break;
		   case '8':
			out.println("EIGHT");
			break;
		   case '9':
			out.println("NINE");
			break;					
                }
	  }
     }
    %>
    </td></tr></TABLE>
</body> 
</html>

Save this code as a .jsp file named "switch_statement_jsp.jsp" in your application directory in Tomcat and run this jsp page with url "http://localhost:8080/user/switch_statement_jsp.jsp" in address bar of the browser.

Enter integer number in text box and click on 'show' button...

Download Source Code

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: July 9, 2008

Related Tutorials

Discuss: How to use switch statement in jsp code  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com