icubesire

revenuehits

Search in Desigs Guru

Saturday, October 27, 2012

Introduction jQuery

http://designsguru.blogspot.in

What You Should Already Know

Before you start studying jQuery, you should have a basic knowledge of:
  • HTML
  • CSS
  • JavaScript
If you want to study these subjects first, find the tutorials on our Home page.

What is jQuery?

jQuery is a lightweight, "write less, do more", JavaScript library.
The purpose of jQuery is to make it much easier to use JavaScript on your website.
jQuery takes a lot of common tasks that requires many lines of JavaScript code to accomplish, and wraps it into methods that you can call with a single line of code.
jQuery also simplifies a lot of the complicated things from JavaScript, like AJAX calls and DOM manipulation.
The jQuery library contains the following features:
  • HTML/DOM manipulation
  • CSS manipulation
  • HTML event methods
  • Effects and animations
  • AJAX
  • Utilities
Tip: In addition, jQuery has plugins for almost any task out there.

Why jQuery?

There are a lots of other JavaScript frameworks out there, but jQuery seems to be the most popular, and also the most extendable.
Many of the biggest companies on the Web use jQuery, like:
  • Google
  • Microsoft
  • IBM
  • Netflix

jQuery

http://designsguru.blogspot.in

jQuery is a JavaScript Library.
jQuery greatly simplifies JavaScript programming.
jQuery is easy to learn.

"Try it yourself" Examples in Each Chapter

With our online editor, you can edit the code, and click on a button to view the result.

Example








If you click on me, I will disappear.

Earn upto Rs. 9,000 pm

http://designsguru.blogspot.in

I have something interesting for you - you can easily earn regular income online via PaisaLive.com!

It’s really amazing! You get paid to open & read the contents of PaisaLive mails. You also receive special discount coupons, promotions and free passes to various events in your city.
Join now and get Rs. 99 instantly, just for joining. What more, as a special bonus you get paid for inviting your friends also!

Create your PaisaLive Account & refer your friends to earn launch referral bonus on every new registration.


PaisaLive - Get Paid to read emails

Make Money With Google

http://designsguru.blogspot.in


Imagine... being paid checks by the largest and most popular search engine, simply for displaying a few ads on your website for FREE?

Well, this is exactly what the Google Adsense program is all about! And when you think about it, it's a revolutionary way of earning yourself a useful online side income. But let's back up a bit...

In the past, many webmasters displayed ads from various companies, via pop-ups, banners, pop-ins and pop-unders. However, visitors to these sites soon got tired of these advertising methods - I mean, who'd like to go to a website where they'd have to close pop-up windows every other minute? This resulted in a dramatic loss of traffic, and in turn made many webmasters to lose profits.

That's when Google announced a novel program – Google Adsense. Instead of having to use banners and pop-ups to advertise companies and gain a commission, website publishers could now earn a decent profit by displaying unobtrusive text ads on the content pages of their website. Since the ads displayed were often directly related to what your visitors are looking for on your site, you had a way to both monetize and subtly enhance your web content.

One of the main reasons for its popularity, is the fact that the Google Adsense program is incredibly accurate. By stepping beyond the boundaries of simple keyword matching, it has quickly become one of the most prominent tools to display accurate advertisements. A list of keywords is still used as the basis of triggering ads, but complex algorithms now ensure that non-relevant ads no longer show up on your site.

Google Adsense also gives you the option to be selective about the type of ads you wish to display. This helps you direct your visitors towards certain type of products and avoid non-relevant or competitor ads. To make it possible for everyone to integrate Adsense into their sites, the program offers a wide variety of settings that allow you to alter the ads' size and appearance.

Google offers their Adsense program to just about all website owners. After signing up for the program, you'll receive an HTML/ XML code to paste on all of your web pages. Then, Google will dynamically generate ads that are relevant to your web content. Whenever a visitor clicks on one of the Adsense ads on your site, Google credits your account with a percentage of money that was paid by the advertiser for that ad.

To sign up for this terrific program, hop over to

Friday, October 26, 2012

jsp What is a Cookie?

http://designsguru.blogspot.in

A cookie is a variable that is stored on the visitor's computer. Each time the same computer requests a page with a browser, it will send the cookie too. With JavaScript, you can both create and retrieve cookie values.
Examples of cookies:
  • Name cookie - The first time a visitor arrives to your web page, he or she must fill in her/his name. The name is then stored in a cookie. Next time the visitor arrives at your page, he or she could get a welcome message like "Welcome John Doe!" The name is retrieved from the stored cookie
  • Date cookie - The first time a visitor arrives to your web page, the current date is stored in a cookie. Next time the visitor arrives at your page, he or she could get a message like "Your last visit was on Tuesday August 11, 2005!" The date is retrieved from the stored cookie

Create and Store a Cookie

In this example we will create a cookie that stores the name of a visitor. The first time a visitor arrives to the web page, he or she will be asked to  fill in her/his name. The name is then stored in a cookie. The next time the visitor arrives at the same page, he or she will get welcome message.
First, we create a function that stores the name of the visitor in a cookie variable:
function setCookie(c_name,value,exdays)
{
var exdate=new Date();
exdate.setDate(exdate.getDate() + exdays);
var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString());
document.cookie=c_name + "=" + c_value;
}
The parameters of the function above hold the name of the cookie, the value of the cookie, and the number of days until the cookie expires.
In the function above we first convert the number of days to a valid date, then we add the number of days until the cookie should expire. After that we store the cookie name, cookie value and the expiration date in the document.cookie object.
Then, we create another function that returns a specified cookie:
function getCookie(c_name)
{
var i,x,y,ARRcookies=document.cookie.split(";");
for (i=0;i{
  x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("="));
  y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1);
  x=x.replace(/^\s+|\s+$/g,"");
  if (x==c_name)
    {
    return unescape(y);
    }
  }
}
The function above makes an array to retrieve cookie names and values, then it checks if the specified cookie exists, and returns the cookie value.
Last, we create the function that displays a welcome message if the cookie is set, and if the cookie is not set it will display a prompt box, asking for the name of the user, and stores the username cookie for 365 days, by calling the setCookie function:
function checkCookie()
{
var username=getCookie("username");
  if (username!=null && username!="")
  {
  alert("Welcome again " + username);
  }
else
  {
  username=prompt("Please enter your name:","");
  if (username!=null && username!="")
    {
    setCookie("username",username,365);
    }
  }
}
All together now:

Example







Try it yourself »
The example above runs the checkCookie() function when the page loads.

The Throw Statement

http://designsguru.blogspot.in

The throw statement allows you to create an exception. If you use this statement together with the try...catch statement, you can control program flow and generate accurate error messages.

Syntax

throw exception
The exception can be a string, integer, Boolean or an object.
Note that throw is written in lowercase letters. Using uppercase letters will generate a JavaScript error!

Example

The example below determines the value of a variable called x. If the value of x is higher than 10, lower than 5, or not a number, we are going to throw an error. The error is then caught by the catch argument and the proper error message is displayed:

Example




JavaScript - Catching Errors

http://designsguru.blogspot.in

When browsing Web pages on the internet, we all have seen a JavaScript alert box telling us there is a runtime error and asking "Do you wish to debug?". Error message like this may be useful for developers but not for users. When users see errors, they often leave the Web page.
This chapter will teach you how to catch and handle JavaScript error messages, so you don't lose your audience.

The try...catch Statement

The try...catch statement allows you to test a block of code for errors. The try block contains the code to be run, and the catch block contains the code to be executed if an error occurs.

Syntax

try
  {
  //Run some code here
  }
catch(err)
  {
  //Handle errors here
  }
Note that try...catch is written in lowercase letters. Using uppercase letters will generate a JavaScript error!

Examples

The example below is supposed to alert "Welcome guest!" when the button is clicked. However, there's a typo in the message() function. alert() is misspelled as adddlert(). A JavaScript error occurs. The catch block catches the error and executes a custom code to handle it. The code displays a custom error message informing the user what happened:

Example









Try it yourself »
The next example uses a confirm box to display a custom message telling users they can click OK to continue viewing the page or click Cancel to go to the homepage. If the confirm method returns false, the user clicked Cancel, and the code redirects the user. If the confirm method returns true, the code does nothing:

Example









Try it yourself »


The throw Statement

The throw statement can be used together with the try...catch statement, to create an exception for the error. Learn about the throw statement in the next chapter.

Complete RegExp Object Reference

http://designsguru.blogspot.in

For a complete reference of all the properties and methods that can be used with the RegExp object, go to our complete RegExp object reference.
The reference contains a brief description and examples of use for each property and method!

What is RegExp?

A regular expression is an object that describes a pattern of characters.
When you search in a text, you can use a pattern to describe what you are searching for.
A simple pattern can be one single character.
A more complicated pattern can consist of more characters, and can be used for parsing, format checking, substitution and more.
Regular expressions are used to perform powerful pattern-matching and "search-and-replace" functions on text.

Syntax

var patt=new RegExp(pattern,modifiers);

or more simply:

var patt=/pattern/modifiers;
  • pattern specifies the pattern of an expression
  • modifiers specify if a search should be global, case-sensitive, etc.

RegExp Modifiers

Modifiers are used to perform case-insensitive and global searches.
The i modifier is used to perform case-insensitive matching.
The g modifier is used to perform a global match (find all matches rather than stopping after the first match).

Example 1

Do a case-insensitive search for "w3schools" in a string:
var str="Visit W3Schools";
var patt1=/w3schools/i;
The marked text below shows where the expression gets a match:
Visit W3Schools

Try it yourself »

Example 2

Do a global search for "is":
var str="Is this all there is?";
var patt1=/is/g;
The marked text below shows where the expression gets a match:
Is this all there is?

Try it yourself »

Example 3

Do a global, case-insensitive search for "is":
var str="Is this all there is?";
var patt1=/is/gi;
The marked text below shows where the expression gets a match:
Is this all there is?

Try it yourself »


test()

The test() method searches a string for a specified value, and returns true or false, depending on the result.
The following example searches a string for the character "e":

Example

var patt1=new RegExp("e");
document.write(patt1.test("The best things in life are free"));
Since there is an "e" in the string, the output of the code above will be:
true

Try it yourself »


exec()

The exec() method searches a string for a specified value, and returns the text of the found value. If no match is found, it returns null.
The following example searches a string for the character "e":

Example 1

var patt1=new RegExp("e");
document.write(patt1.exec("The best things in life are free"));
Since there is an "e" in the string, the output of the code above will be:
e

Complete Math Object Reference

http://designsguru.blogspot.in

For a complete reference of all the properties and methods that can be used with the Math object, go to our complete Math object reference.
The reference contains a brief description and examples of use for each property and method!

Math Object

The Math object allows you to perform mathematical tasks.
The Math object includes several mathematical constants and methods.
Syntax for using properties/methods of Math:
var x=Math.PI;
var y=Math.sqrt(16);
Note: Math is not a constructor. All properties and methods of Math can be called by using Math as an object without creating it.

Mathematical Constants

JavaScript provides eight mathematical constants that can be accessed from the Math object. These are: E, PI, square root of 2, square root of 1/2, natural log of 2, natural log of 10, base-2 log of E, and base-10 log of E.
You may reference these constants from your JavaScript like this:
Math.E
Math.PI
Math.SQRT2
Math.SQRT1_2
Math.LN2
Math.LN10
Math.LOG2E
Math.LOG10E


Mathematical Methods

In addition to the mathematical constants that can be accessed from the Math object there are also several methods available.
The following example uses the round() method of the Math object to round a number to the nearest integer:
document.write(Math.round(4.7));
The code above will result in the following output:
5
The following example uses the random() method of the Math object to return a random number between 0 and 1:
document.write(Math.random());
The code above can result in the following output:
0.3045910941436887
The following example uses the floor() and random() methods of the Math object to return a random number between 0 and 10:
document.write(Math.floor(Math.random()*11));
The code above can result in the following output:
5

Monday, October 22, 2012

JavaScript Boolean Object

http://designsguru.blogspot.in

Boolean Object Properties

PropertyDescription
constructorReturns the function that created the Boolean object's prototype
prototypeAllows you to add properties and methods to a Boolean object

Boolean Object Methods

MethodDescription
toString()Converts a Boolean value to a string, and returns the result
valueOf()Returns the primitive value of a Boolean object

Array Object in java

http://designsguru.blogspot.in

Array Object Properties

PropertyDescription
constructorReturns the function that created the Array object's prototype
lengthSets or returns the number of elements in an array
prototypeAllows you to add properties and methods to an Array object

Array Object Methods

MethodDescription
concat()Joins two or more arrays, and returns a copy of the joined arrays
indexOf()Search the array for an element and returns it's position
join()Joins all elements of an array into a string
lastIndexOf()Search the array for an element, starting at the end, and returns it's position
pop()Removes the last element of an array, and returns that element
push()Adds new elements to the end of an array, and returns the new length
reverse()Reverses the order of the elements in an array
shift()Removes the first element of an array, and returns that element
slice()Selects a part of an array, and returns the new array
sort()Sorts the elements of an array
splice()Adds/Removes elements from an array
toString()Converts an array to a string, and returns the result
unshift()Adds new elements to the beginning of an array, and returns the new length
valueOf()Returns the primitive value of an array


Protecting your website with a login page

http://designsguru.blogspot.in

Some sites require that all users log-in using a username and password, before being able to visit any page.
This can be done using JSP sessions or servlets, and in fact this was a common technique for a while.  But starting with a new release of Servlets specifications (2.2) from Sun, this feature is now very simple to implement.  
It is no longer necessary to use JSP techniques to provide login/password protection, but it is still a very common requirement of web-sites, therefore a brief overview is provided here.
To password-protect your site, you just need to design a login page.  This page can be as simple or complicated as you need it to be.  It must contain a 
 tag, with the METHOD set to POST and the ACTION set to "j_security_check".
The target j_security_check is provided by the application server, and does not need to be coded.
The form must contain two  fields, named j_username and j_password respectively for the username and password.  Typically, the username field will be a TEXTinput field, and the password field will be a PASSWORD input field.

After this, you must tell your application server to password protect your pages using the login page you have provided.  The details will vary from server to server, but a good implementation will provide you hooks that you can use, for example, to match usernames and passwords against a database.  (E.g., in Blazix you can supply an implementation of the interface desisoft.deploy.AuthCheck to check usernames and passwords against a database or other sources.) 
 

Thursday, October 18, 2012

Scriptlets in jsp

http://designsguru.blogspot.in
We have already seen how to embed Java expressions in JSP pages by putting them between the <%= and %> character sequences.
But it is difficult to do much programming just by putting Java expressions inside HTML.
JSP also allows you to write blocks of Java code inside the JSP.  You do this by placing your Java code between <% and %> characters (just like expressions, but without the = sign at the start of the sequence.)
This block of code is known as a "scriptlet".  By itself, a scriptlet doesn't contribute any HTML (though it can, as we will see down below.)  A scriptlet contains Java code that is executed every time the JSP is invoked.
Here is a modified version of our JSP from previous section, adding in a scriptlet.


<%
    // This is a scriptlet.  Notice that the "date"
    // variable we declare here is available in the
    // embedded expression later on.
    System.out.println( "Evaluating date now" );
    java.util.Date date = new java.util.Date();
%>
Hello!  The time is now <%= date %>

If you run the above example, you will notice the output from the "System.out.println" on the server log.  This is a convenient way to do simple debugging (some servers also have techniques of debugging the JSP in the IDE.  See your server's documentation to see if it offers such a technique.)By itself a scriptlet does not generate HTML.  If a scriptlet wants to generate HTML, it can use a variable called "out".  This variable does not need to be declared.  It is already predefined for scriptlets, along with some other variables.  The following example shows how the scriptlet can generate HTML output.


<%
    // This scriptlet declares and initializes "date"
    System.out.println( "Evaluating date now" );
    java.util.Date date = new java.util.Date();
%>
Hello!  The time is now
<%
    // This scriptlet generates HTML output
    out.println( String.valueOf( date ));
%>

Here, instead of using an expression, we are generating the HTML directly by printing to the "out" variable.  The "out" variable is of type javax.servlet.jsp.JspWriter.Another very useful pre-defined variable is "request".  It is of type javax.servlet.http.HttpServletRequest
A "request" in server-side processing refers to the transaction between a browser and the server.  When someone clicks or enters a URL, the browser sends a "request" to the server for that URL, and shows the data returned.  As a part of this "request", various data is available, including the file the browser wants from the server, and if the request is coming from pressing a SUBMIT button, the information the user has entered in the form fields.
The JSP "request" variable is used to obtain information from the request as sent by the browser.  For instance, you can find out the name of the client's host (if available, otherwise the IP address will be returned.)  Let us modify the code as shown:


<%
    // This scriptlet declares and initializes "date"
    System.out.println( "Evaluating date now" );
    java.util.Date date = new java.util.Date();
%>
Hello!  The time is now
<%
    out.println( date );
    out.println( "
Your machine's address is " );
    out.println( request.getRemoteHost());
%>

A similar variable is "response".  This can be used to affect the response being sent to the browser.  For instance, you can call response.sendRedirect( anotherUrl ); to send a response to the browser that it should load a different URL.  This response will actualy go all the way to the browser.  The browser will then send a different request, to "anotherUrl".  This is a little different from some other JSP mechanisms we will come across, for including another page or forwarding the browser to another page.Exercise:  Write a JSP to output the entire line, "Hello!  The time is now ..." but use a scriptlet for the complete string, including the HTML tags.

Scriptlets in jsp

http://designsguru.blogspot.in
We have already seen how to embed Java expressions in JSP pages by putting them between the <%= and %> character sequences.
But it is difficult to do much programming just by putting Java expressions inside HTML.
JSP also allows you to write blocks of Java code inside the JSP.  You do this by placing your Java code between <% and %> characters (just like expressions, but without the = sign at the start of the sequence.)
This block of code is known as a "scriptlet".  By itself, a scriptlet doesn't contribute any HTML (though it can, as we will see down below.)  A scriptlet contains Java code that is executed every time the JSP is invoked.
Here is a modified version of our JSP from previous section, adding in a scriptlet.


<%
    // This is a scriptlet.  Notice that the "date"
    // variable we declare here is available in the
    // embedded expression later on.
    System.out.println( "Evaluating date now" );
    java.util.Date date = new java.util.Date();
%>
Hello!  The time is now <%= date %>

If you run the above example, you will notice the output from the "System.out.println" on the server log.  This is a convenient way to do simple debugging (some servers also have techniques of debugging the JSP in the IDE.  See your server's documentation to see if it offers such a technique.)By itself a scriptlet does not generate HTML.  If a scriptlet wants to generate HTML, it can use a variable called "out".  This variable does not need to be declared.  It is already predefined for scriptlets, along with some other variables.  The following example shows how the scriptlet can generate HTML output.


<%
    // This scriptlet declares and initializes "date"
    System.out.println( "Evaluating date now" );
    java.util.Date date = new java.util.Date();
%>
Hello!  The time is now
<%
    // This scriptlet generates HTML output
    out.println( String.valueOf( date ));
%>

Here, instead of using an expression, we are generating the HTML directly by printing to the "out" variable.  The "out" variable is of type javax.servlet.jsp.JspWriter.Another very useful pre-defined variable is "request".  It is of type javax.servlet.http.HttpServletRequest
A "request" in server-side processing refers to the transaction between a browser and the server.  When someone clicks or enters a URL, the browser sends a "request" to the server for that URL, and shows the data returned.  As a part of this "request", various data is available, including the file the browser wants from the server, and if the request is coming from pressing a SUBMIT button, the information the user has entered in the form fields.
The JSP "request" variable is used to obtain information from the request as sent by the browser.  For instance, you can find out the name of the client's host (if available, otherwise the IP address will be returned.)  Let us modify the code as shown:


<%
    // This scriptlet declares and initializes "date"
    System.out.println( "Evaluating date now" );
    java.util.Date date = new java.util.Date();
%>
Hello!  The time is now
<%
    out.println( date );
    out.println( "
Your machine's address is " );
    out.println( request.getRemoteHost());
%>

A similar variable is "response".  This can be used to affect the response being sent to the browser.  For instance, you can call response.sendRedirect( anotherUrl ); to send a response to the browser that it should load a different URL.  This response will actualy go all the way to the browser.  The browser will then send a different request, to "anotherUrl".  This is a little different from some other JSP mechanisms we will come across, for including another page or forwarding the browser to another page.Exercise:  Write a JSP to output the entire line, "Hello!  The time is now ..." but use a scriptlet for the complete string, including the HTML tags.

Scriptlets in jsp

http://designsguru.blogspot.in
We have already seen how to embed Java expressions in JSP pages by putting them between the <%= and %> character sequences.
But it is difficult to do much programming just by putting Java expressions inside HTML.
JSP also allows you to write blocks of Java code inside the JSP.  You do this by placing your Java code between <% and %> characters (just like expressions, but without the = sign at the start of the sequence.)
This block of code is known as a "scriptlet".  By itself, a scriptlet doesn't contribute any HTML (though it can, as we will see down below.)  A scriptlet contains Java code that is executed every time the JSP is invoked.
Here is a modified version of our JSP from previous section, adding in a scriptlet.


<%
    // This is a scriptlet.  Notice that the "date"
    // variable we declare here is available in the
    // embedded expression later on.
    System.out.println( "Evaluating date now" );
    java.util.Date date = new java.util.Date();
%>
Hello!  The time is now <%= date %>

If you run the above example, you will notice the output from the "System.out.println" on the server log.  This is a convenient way to do simple debugging (some servers also have techniques of debugging the JSP in the IDE.  See your server's documentation to see if it offers such a technique.)By itself a scriptlet does not generate HTML.  If a scriptlet wants to generate HTML, it can use a variable called "out".  This variable does not need to be declared.  It is already predefined for scriptlets, along with some other variables.  The following example shows how the scriptlet can generate HTML output.


<%
    // This scriptlet declares and initializes "date"
    System.out.println( "Evaluating date now" );
    java.util.Date date = new java.util.Date();
%>
Hello!  The time is now
<%
    // This scriptlet generates HTML output
    out.println( String.valueOf( date ));
%>

Here, instead of using an expression, we are generating the HTML directly by printing to the "out" variable.  The "out" variable is of type javax.servlet.jsp.JspWriter.Another very useful pre-defined variable is "request".  It is of type javax.servlet.http.HttpServletRequest
A "request" in server-side processing refers to the transaction between a browser and the server.  When someone clicks or enters a URL, the browser sends a "request" to the server for that URL, and shows the data returned.  As a part of this "request", various data is available, including the file the browser wants from the server, and if the request is coming from pressing a SUBMIT button, the information the user has entered in the form fields.
The JSP "request" variable is used to obtain information from the request as sent by the browser.  For instance, you can find out the name of the client's host (if available, otherwise the IP address will be returned.)  Let us modify the code as shown:


<%
    // This scriptlet declares and initializes "date"
    System.out.println( "Evaluating date now" );
    java.util.Date date = new java.util.Date();
%>
Hello!  The time is now
<%
    out.println( date );
    out.println( "
Your machine's address is " );
    out.println( request.getRemoteHost());
%>

A similar variable is "response".  This can be used to affect the response being sent to the browser.  For instance, you can call response.sendRedirect( anotherUrl ); to send a response to the browser that it should load a different URL.  This response will actualy go all the way to the browser.  The browser will then send a different request, to "anotherUrl".  This is a little different from some other JSP mechanisms we will come across, for including another page or forwarding the browser to another page.Exercise:  Write a JSP to output the entire line, "Hello!  The time is now ..." but use a scriptlet for the complete string, including the HTML tags.

Scriptlets in jsp

http://designsguru.blogspot.in
We have already seen how to embed Java expressions in JSP pages by putting them between the <%= and %> character sequences.
But it is difficult to do much programming just by putting Java expressions inside HTML.
JSP also allows you to write blocks of Java code inside the JSP.  You do this by placing your Java code between <% and %> characters (just like expressions, but without the = sign at the start of the sequence.)
This block of code is known as a "scriptlet".  By itself, a scriptlet doesn't contribute any HTML (though it can, as we will see down below.)  A scriptlet contains Java code that is executed every time the JSP is invoked.
Here is a modified version of our JSP from previous section, adding in a scriptlet.


<%
    // This is a scriptlet.  Notice that the "date"
    // variable we declare here is available in the
    // embedded expression later on.
    System.out.println( "Evaluating date now" );
    java.util.Date date = new java.util.Date();
%>
Hello!  The time is now <%= date %>

If you run the above example, you will notice the output from the "System.out.println" on the server log.  This is a convenient way to do simple debugging (some servers also have techniques of debugging the JSP in the IDE.  See your server's documentation to see if it offers such a technique.)By itself a scriptlet does not generate HTML.  If a scriptlet wants to generate HTML, it can use a variable called "out".  This variable does not need to be declared.  It is already predefined for scriptlets, along with some other variables.  The following example shows how the scriptlet can generate HTML output.


<%
    // This scriptlet declares and initializes "date"
    System.out.println( "Evaluating date now" );
    java.util.Date date = new java.util.Date();
%>
Hello!  The time is now
<%
    // This scriptlet generates HTML output
    out.println( String.valueOf( date ));
%>

Here, instead of using an expression, we are generating the HTML directly by printing to the "out" variable.  The "out" variable is of type javax.servlet.jsp.JspWriter.Another very useful pre-defined variable is "request".  It is of type javax.servlet.http.HttpServletRequest
A "request" in server-side processing refers to the transaction between a browser and the server.  When someone clicks or enters a URL, the browser sends a "request" to the server for that URL, and shows the data returned.  As a part of this "request", various data is available, including the file the browser wants from the server, and if the request is coming from pressing a SUBMIT button, the information the user has entered in the form fields.
The JSP "request" variable is used to obtain information from the request as sent by the browser.  For instance, you can find out the name of the client's host (if available, otherwise the IP address will be returned.)  Let us modify the code as shown:


<%
    // This scriptlet declares and initializes "date"
    System.out.println( "Evaluating date now" );
    java.util.Date date = new java.util.Date();
%>
Hello!  The time is now
<%
    out.println( date );
    out.println( "
Your machine's address is " );
    out.println( request.getRemoteHost());
%>

A similar variable is "response".  This can be used to affect the response being sent to the browser.  For instance, you can call response.sendRedirect( anotherUrl ); to send a response to the browser that it should load a different URL.  This response will actualy go all the way to the browser.  The browser will then send a different request, to "anotherUrl".  This is a little different from some other JSP mechanisms we will come across, for including another page or forwarding the browser to another page.Exercise:  Write a JSP to output the entire line, "Hello!  The time is now ..." but use a scriptlet for the complete string, including the HTML tags.

LinkWithin

Related Posts Plugin for WordPress, Blogger...