Wednesday, 5 December 2012

Commenting Your Code

              Comments are used to document your code.You can use comments to explain code that is difficult to understand as well as the inner workings of any algorithms that are used.Comments provide an important communication tool between you and the programmer who will update and maintain the code at a later date.

<html>
<head>
<title> Simple JSP page </title>
</head>
<body>
<h3>Including Comments in your code</h3>
<p>
<!-- An HTML Comment -->
<%--A JSP Comment --%>
<%
// A Single line comment
for(int i=1;i<7;i++)
{
 /* A multi line comments 
 spans many lines */
 out.println("No of times this code has been repeated:"+i);
 out.println("<br>");
}
%>
</body>
</html>

Description:


  •  An HTML comment is enclosed within the <!-- and --> delimiters.HTML comments aren't displayed in a Web page, but they are included in the generated HTML source code.This can easily be accessed by anyone who views the source code in a Web browser.You should not  place any sensitive information in HTML comments.
                      <!-- An HTML Comment -->
  •  JSP comments are enclosed within the <%-- and --%> delimiters. JSP comments are not sent to the Web browser.
                   <%--A JSP Comment --%>
  • The // delimiter can be used to create a single comment line in a Scriplet tag.                                                                   
                  // A Single line comment        
  • A multi-line comment can be placed within the /*  and */ delimiters.
                          /* A multi line comments
                             spans many lines */

************************************OUTPUT***********************************

Including Comments in your code

No of times this code has been repeated:1
No of times this code has been repeated:2
No of times this code has been repeated:3
No of times this code has been repeated:4
No of times this code has been repeated:5
No of times this code has been repeated:6
*******************************************************************************

Tuesday, 4 December 2012

Using the Scriplet Tag

                       The Scriplet tag allows Java to be mixed with HTML to produce dynamic content.Java code must be embedded within the <%  and %> tag delimiters.Each language statement must end with a semicolon.

<html>
<head>
<title> Simple JSP page </title>
</head>
<body>
<h3>Embedded Java Code within a Web Page </h3>
<p>
<%
for (int i=1;i<7;i++)
{
 out.println("<H"+i+">The Size of H" + i +"</H"+i+">");
 out.println("<br>");
}
%>
</body>
</html>

Description:


  • Type <% to indicate the beginning of the Scriplet tag.
  • Type the applicable Java code after the <% delimiter.
  • Use the out.println method to generate HTML.
  • Type %> to close the Scriplet tag.

The Same program can also be written as :


<html>
<head>
<title> Simple JSP page </title>
</head>
<body>
<h3>Embedded Java Code within a Web Page </h3>
<p>
<%
for (int i=1;i<7;i++)
{
%>
 <H<%=i%>>The Size of H<%=i%> </H<%=i%>>
 <br>
<%
}
%>
</body>
</html>

you can mix HTML and Java code in any combination but you must remember to close the Scriplet tag before you start HTML and then reopen the Scriplet tag when you want to write more Java code.


*********************************OUTPUT****************************************

Embedded Java Code within a Web Page


The Size of H1



The Size of H2



The Size of H3



The Size of H4



The Size of H5


The Size of H6

********************************************************************************



Monday, 3 December 2012

Declaring Methods (Method to calculate percentages)

                A method stores a group of Java statements that you can execute from more than one location in your JSP page.This saves you from repeating the same code and allows you to update the code in a central location.You can call a methods as many times as you like.
         
              A method can accept arguments and return a result to the code from which it was called, providing greater flexibility.

Simple JSP Program Using Methods:


<html>
<head>
<title> Simple JSP page </title>
</head>
<body>
<h3>Method to calculate Percentage </h3>
<p>
<%!
float calculatePercentage(float a,float b)
{
 float result=a/b * 100;
 return result;
}
%>
<p>
Percentage of (15/20) is <%=calculatePercentage(15,20) %>%
<br>
Percentage of (10/20) is <%=calculatePercentage(10,20)%>%
<br>
Percentage of (5/20) is <%=calculatePercentage(5,20) %>%
<br>
</body>
</html>

Description:

  • Type <%! to open the declaration tag.
  • Type the keyword for the type of data that the method will return.(In the above example. "float" is used).If you type void the method will not return a value.
  • Type the name of the method(calculatePercentage)
  • Type opening and closing parentheses after the method name.({ and })
  • Declare the arguments that the method can accept.(float a, float b).A method can accept multiple arguments.
  • Insert the reusable code within braces.
             float result=a/b * 100;
             return result;
  • Type the keyword return  before the value or variable to be passed back to the calling method code.
              return result;
  • Type %> to close the declaration tag.
  • Use the Expression tag to print the result returned by the method.
  • Type the method name between the expression tag delimiters.
             calculatePercentage
  • Pass the Parameters to the method in the order that they were declared.
             <%=calculatePercentage(15,20) %>


********************************OUTPUT****************************************

Method to calculate Percentage

Percentage of (15/20) is 75.0%
Percentage of (10/20) is 50.0%
Percentage of (5/20) is 25.0% 


********************************************************************************

Declaring String Variables

A String is composed of words and characters and must be enclosed in quotation marks.A String is an object that has its own class.The String class provides methods for string manipulation.

Simple Program Using Strings:


<html>
<head>
<title> Simple JSP page </title>
</head>
<body>
<h3>Using the Declaration tag declare page level variables: </h3>
<p>
<%!
String myname;
String surname="Manikandan";
String Email,Street,City;
%>
<p>
<b>Notes:</b>The Declaration tag does not produce any output.
<p>
<%
myname="Saranya";
%>
My Name is <%=myname%> <%=surname%>
</body>
</html>

Description:

  • Type <%! to open the Declaration tag.
  • Type the String class name followed by the variable name and a semicolon.This initializes a single variable as a string object.(String myname;)
  • Use = to assign a value to the variable when it is initialized.This is explicit initialization.(String surname="Manikandan";)
  • The value assigned to a String must be enclosed in quotation marks.
  • You can initialize more than variable of same type by separating with a comma.(String Email,Street,City;)
  • Type %> to close the Declaration tag.
  • Type = to assign a value to a variable.The value must be placed on the right side of the equals sign. (myname="Saranya";)
  • Use the expression tag to print the value of a variable.(<%=myname%> <%=surname%>)


***************************OUTPUT**********************************************

Using the Declaration tag declare page level variables:

Notes:The Declaration tag does not produce any output.
My Name is Saranya Manikandan

*********************************************************************************

Declaring Integer Variables

Integers are used to store numeric data and declared with the keyword int.There are four standard types of integers, all which can holdd both positive and negativeValues.


Simple Program Using Integers:


<html>
<head>
<title> Simple JSP page </title>
</head>
<body>
<h3>Using the Declaration tag declare page level variables: </h3>
<p>
<%!
int noDays;
int year=2001;
int January, February, March;
%>
<p>
<b>Notes:</b>The Declaration tag does not produce any output.
<p>
<%
noDays=365;
%>
The Number of days in a year is <%=noDays%>
</body>
</html>

Description:

  • Type <%! to open the Declaration tag.
  • Type int followed by the variable name and a semicolon.ths declares a single variable as an integer.(int noDays;)
  • Use the equal sign(=) to assign a value to the variable when it is being initialized.This is known as explicit initialization.(int year=2001;)
  • You can initialize more than one variable of same type by separating each variable name with a comma.(int January, February, March;)
  • Type %>  to close the Declaration tag.
  • Type = sign to assign a value to a variable.The value must be placed on the right side of the equal sign.(noDays=365;)
  • Use the expression tag to print the value of a variable.(<%=noDays%>)


*************************OUTPUT*****************************************

Using the Declaration tag declare page level variables:

Notes:The Declaration tag does not produce any output.
The Number of days in a year is 365

************************************************************************** 








Declaration Tag In JSP



Declaring Variables:
Declare  an Integer - int variablename;
Declare a String  - String variablename;

<html>
<head>
<title> Simple JSP page </title>
</head>
<body>
<h3>Using the Declaration tag declare page level variables: </h3>
<p>
<%!
int year;
%>
<p>
<b>Notes:</b>The Declaration tag does not produce any output.
</body>
</html>
Description:
  • Type <! to open declaration tag.The <%! delimiter indicates the beginning  of the Declaration tag.
  • Type the data type keyword of the variable that needs to be declared.(can declare as integer or a String)
  • Type the name of the variable.
  • Type %> after the declaration.This will end the Declaration tag.
****************************Output***************************************

Using the Declaration tag declare page level variables:

Notes:The Declaration tag does not produce any output. 

**************************************************************************

Simple JSP to display Maths Functions


Simple JSP Page 2 :

<html>
<head>
<title> Simple JSP page </title>
</head>
<body>
<h3>Using Expression tag to display the result of the expression </h3>
<p>
<ul>
<li>2*6 = <%=2*6%></li>
<li>sqrt(8)=<%=Math.sqrt(8) %></li>
<li>The Current Date and time = <%=new java.util.Date() %></li>
</ul>
</body>
</html>

****************************Output***************************************

Using Expression tag to display the result of the expression

  • 2*6 = 12
  • sqrt(8)=2.8284271247461903
  • The Current Date and time = Mon Dec 03 15:32:33 CST 2012 
**************************************************************************