Thursday, 20 December 2012

Formatting the Date

<%@page import="java.util.*" %>
<%@ page import="java.text.*" %>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Displaying Dates</title>
</head>
<body>
<h3>Displaying Today's date in different formats:</h3>
<%!
Date today=new Date();
DateFormat formatFull=DateFormat.getDateInstance(DateFormat.FULL);
DateFormat formatLong=DateFormat.getDateInstance(DateFormat.LONG);
DateFormat formatMedium=DateFormat.getDateInstance(DateFormat.MEDIUM);
DateFormat formatShort=DateFormat.getDateInstance(DateFormat.SHORT);
%>
<p>
Full Format: <%=formatFull.format(today) %>
<br>
Long Format: <%=formatLong.format(today) %>
<br>
Medium Format: <%=formatMedium.format(today) %>
<br>
Short Format: <%=formatShort.format(today) %>
</body>
</html>

Description:


  • Use the import page directive to import the java.text.package.
  • Create a Date object.
  • Create a Date Format object by calling the getDateInstance method and passing DateFormat.FULL as a parameter.
  • Create a Date Format object by calling the getDateInstance method and passing DateFormat.LONG as a parameter.
  • Create a Date Format object by calling the getDateInstance method and passing DateFormat.MEDIUM as a parameter.
  • Create a Date Format object by calling the getDateInstance method and passing DateFormat.SHORT as a parameter.
  • Pass the current date as a parameter to the format method of the FormatFull object.This will display the full date, for example: 21 December 2012.
  • Pass the current date as a parameter to the format method of the FormatLong object.This will display the long date, for example:12/21 /2012.
  • Pass the current date as a parameter to the format method of the FormatMedium object.This will display the medium date, for example:Friday, 21 December 2012.
  • Pass the current date as a parameter to the format method of the FormatShort object.This will display the short date, for example:12/21 /12.

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

Displaying Today's date in different formats:

Full Format: Friday, December 21, 2012
Long Format: December 21, 2012
Medium Format: Dec 21, 2012
Short Format: 12/21/12  


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

Thursday, 6 December 2012

Generating Random Numbers

     Random numbers will help you add an element  of unpredictability to your website.you can retrieve a random number and display a corresponding image, tip, or greeting.you must first create a random object  and then call the nextInt method to return a random number.

Example:

<%@page import="java.util.*" %>
<html>
<head>
<title>Generating Random Numbers</title>
</head>
<body>
<h3>Random Numbers:</h3>
<%!
Random randomValue=new Random();
%>
<p>
Random Number:<%=randomValue.nextInt() %>
<br>
Positive Random Number:<%=Math.abs(randomValue.nextInt()) %>
<br>
Generating a random Number between 1 and 6 :<%=(Math.abs(randomValue.nextInt())%5)+1 %>
<p>
<b>Note:</b>Each time this script is run a different output will be produced.
</body>
</html> 

Description:


  • Use the import page directive to import the java.util package.The Random class is located in this package.
  • Create a random object instance.
  • Call the nextInt method.The nextInt method will return a random number anywhere within the full range of integer values.Be aware that the nextInt method can return a negative number.
  • Pass the generated random number to the abs method.This will convert all generated numbers from negative to positive.
  • Use the modules operator to calculate the remainder between a positive random number and the maximum random number that you require.Add one to this result.


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

Random Numbers:

Random Number:443353488
Positive Random Number:1446394622
Generating a random Number between 1 and 6 :2

Note:Each time this script is run a different output will be produced.
****************************************************************************



Working with Mathematical Functions

                   The Math class provides a variety of methods that can be used to perform mathematical functions.The Math class includes methods for generating random numbers, calculating square roots, and perform trigonometric functions.
                  Mathematical constants such as Pi and logarithmic E are also included in the Math class as read-only variables.These methods are stored in the java.lang.package and are automatically available in your JSP code.

Mathematical Functions:

  • sqrt(value 1)
  • pow(value 1, value 2)
  • exp(value 1)
  • log(value 1)

Trigonometric Functions:

  • sin(value 1)
  • cos(value 1)
  • tan(value 1)
  • asin(value 1)
  • acon(value 1)

Numeric Functions:

  • abs(value 1)
  • max(value 1,value 2)
  • min(value 1,value 2)
  • round(value 1)

Example :

<html>
<head>
<title>Mathematical Functions</title>
</head>
<body>
<h3>Calculating the Radius of a Circle:</h3>
<p>
The radius of a circle with a area of 500m<superscript>2</superscript> = <%=Math.sqrt(500.00/Math.PI) %>m
</body>
</html> 

Description:


This example uses the sqrt method to calculate the radius of a circle that has an area of 500msquare


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

Calculating the Radius of a Circle:

The radius of a circle with a area of 500m2 = 12.6156626101008m 

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

Using Increment and Decrement Operators

              The increment operator (++) provides a shortcut to increase the value of a variable by one.The decrement operator (--) simply subtracts a value of one from a variable.The increment and decrement operators are helpful when used with loop counter variables.

<html>
<head>
<title>Increment and Decrement Operator</title>
</head>
<body>
<h3>Using Increment Operator</h3>
<%
int i=4;
%>
<p>
The value of <b>i</b> is <%=i%>
<br>
The value of <b>++i</b> is <%=++i %>
<p>
<h3>Using Decrement Operator</h3>
<p>
The value of <b>i</b> is <%=i %>
<br>
The value of <b>--i</b> is <%=--i%>
</body>
</html>

Description:

  • Assign an initial integer value to a variable.This variable will be used to illustrate the increment and decrement operators.
  • Type the increment operator (++) before the variable name and use the Expression tag to print the result to the Web page.The variable will be incremented by one before it is displayed.
  • Type the decrement operator (--) before the variable name and use the Expression tag to print the result to the Web page.The variable will be decreased by one before it is displayed.

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

Using Increment Operator

The value of i is 4
The value of ++i is 5

Using Decrement Operator

The value of i is 5
The value of --i is 4


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

















Wednesday, 5 December 2012

Changing the Order of Precedence

             Multiplication and division always take precedence over addition  and subtraction when an expression is calculated. However, force parts of an expression to be calculated first with the aid of parentheses.

Example:

<html>
<head>
<title>Operator Precedence</title>
</head>
<body>
<h3>Operator Precedence</h3>
<p>
15 - 4 * 2 + 8 / 2 = <%=15-4*2+8/2 %>
<br>
(15 - 4) * 2 + 8 / 2 = <%=(15-4)*2+8/2 %>
<p>
</body>
</html>


Description:

Type an expression that includes addition,subtraction,multiplication and division.Use the expression tag to print the result to the Web page.

Place parentheses around values that need to be calculated first.Parentheses change the order of precedence.

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

Operator Precedence

15 - 4 * 2 + 8 / 2 = 11
(15 - 4) * 2 + 8 / 2 = 26 

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

Performing Basic Mathematical Operations

<html>
<head>
<title>Basic Maths Operations </title>
</head>
<body>
<h3>Addition, Subtraction, Multiplication and Division</h3>
<p>
5 + 2 = <%=5+2 %>
<br>
5 - 2 = <%=5-2 %>
<br>
5 * 2 = <%=5*2 %>
<br>
5 / 2 = <%=5/2 %>
<br>
5 % 2 = <%=5%2 %>
<p>

</body>
</html>

Description:


  • Type + between two values or variables and use the expression tag to print the result to the Web page.The + operator performs addition.
           5 + 2 = <%=5+2 %>
  • Type - between two values or variables and use the expression tag to print the result to the Web page.The + operator performs subtraction.
             5 - 2 = <%=5-2 %>
  • Type * between two values or variables and use the expression tag to print the result to the Web page.The * operator performs multiplication.
             5 * 2 = <%=5*2 %>
  • Type / between two values or variables and use the expression tag to print the result to the Web page.The / operator performs division.
            5 / 2 = <%=5/2 %>
  • Type % between two values or variables and use the expression tag to print the result to the Web page.The % operator returns the remainder when integer arithmetic is performed.
            5 % 2 = <%=5%2 %>

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

Addition, Subtraction, Multiplication and Division

5 + 2 = 7
5 - 2 = 3
5 * 2 = 10
5 / 2 = 2
5 % 2 = 1
****************************************************************************


Using Implicit Variables

           The JSP specification defines several implicit variables that are not necessary to declare.Implicit variables or objects can only be used within scriplets and expressions.They provide a simplified way to access the Servlet API.You can use these variables to write response headers,retieve posted from data, track sessions, and handle exceptions.

Implicit JSP variables are :


Request    - Retrieves all the information that is sent from a Web browser to a Web server.

Response - Sends information back to the Web browser.

Session     - Stores and retrieves session information for each visitor to your Web Site.

Exception - Provides an exception handling mechanism.