Thursday, August 22, 2013

Introduction to jsp:- All basic know-how of jsp

JavaServer Pages technology allows Web developers and designers to rapidly develop and easily maintain, information-rich, dynamic Web pages that leverage existing business systems. As part of the Java family, JSP technology enables rapid development of Web-based applications that are platform independent. JavaServer Pages technology separates the user interface from content generation enabling designers to change the overall page layout without altering the underlying dynamic content.(Sun)
  • Used to build dynamic web pages
  • Server side scripting language
  • Platform independent
  • Technologically more advanced than ASP
  • Text files that are a mix of HTML and Java code
  • Jsp stands for “Java server page”.
  • JSP are built by SUN Microsystem.
  • JSP enable the developers to directly insert java code into jsp file, this makes the development process very simple and its maintenance also becomes very easy.
  • JSP simply place JAVA inside the HTML pages means it’s an embedded of html tag and jsp’s own tag.
  • The extension of jsp page is ".jsp"
  • Java is known for its characteristic of "write once, run anywhere." and JSP page is a combination of java code into html file so we can say that the JSP pages are platform independent means .jsp pages can run in any platform.

Basic Lesson 1- Getting Familiar with your JSP server

If you do not have a JSP capable web-server or application server, the first step is to download one.  There are many such servers available, most of which can be downloaded for free evaluation and/or development.  Some of them are:
Blazix from Desiderata Software (1.5 Megabytes, JSP, Servlets and EJBs)
TomCat from Apache (Approx 6 Megabytes)
WebLogic from BEA Systems (Approx 40 Megabytes, JSP, Servlets and EJBs)
WebSphere from IBM (Approx 100 Megabytes, JSP, Servlets and EJBs)
To take the best advantage of this tutorial, it recommended that all the material should be tried out with a real server.

Basic lesson 2:-Your first JSP hello world program

JSP simply puts Java inside HTML pages.  You can take any existing HTML page and change its extension to ".jsp" instead of ".html".  In fact, this is the perfect exercise for your first JSP.
Take the HTML file you used in the previous exercise.  Change its extension from ".html" to ".jsp".  Now load the new file, with the ".jsp" extension, in your browser.
You will see the same output, but it will take longer!  But only the first time.  If you reload it again, it will load normally.

Basic lesson 3:-Adding dynamic content via expressions in jsp

As we saw in the previous section, any HTML file can be turned into a JSP file by changing its extension to .jsp.  Of course, what makes JSP useful is the ability to embed Java.  Put the following text in a file with .jsp extension (let us call it hello.jsp), place it in your JSP directory, and view it in a browser.
<HTML>
<BODY>
Hello!  The time is now <%= new java.util.Date() %>
</BODY>
</HTML>

basic Lesson 4:- Scriptlets in jsp

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.

Basic Lesson 5:-Mixing Scriptlets and HTML in jsp

We have already seen how to use the "out" variable to generate HTML output from within a scriptlet.  For more complicated HTML, using the out variable all the time loses some of the advantages of JSP programming.  It is simpler to mix scriptlets and HTML.
Suppose you have to generate a table in HTML.  This is a common operation, and you may want to generate a table from a SQL table, or from the lines of a file.  But to keep our example simple, we will generate a table containing the numbers from 1 to N.  Not very useful, but it will show you the technique.

Here is the JSP fragment to do it:

Basic lesson 6:- Directives in JSP

We have been fully qualifying the java.util.Date in the examples in the previous sections.  Perhaps you wondered why we don't just import java.util.*;
It is possible to use "import" statements in JSPs, but the syntax is a little different from normal Java.  Try the following example:
<%@ page import="java.util.*" %>
<HTML>
<BODY>
<%
    System.out.println( "Evaluating date now" );
    Date date = new Date();
%>
Hello!  The time is now <%= date %>
</BODY>
</HTML>

Basic Lesson 7:- Declarations in Jsp

The JSP you write turns into a class definition.  All the scriptlets you write are placed inside a single method of this class.
You can also add variable and method declarations to this class.  You can then use these variables and methods from your scriptlets and expressions.
To add a declaration, you must use the <%! and %> sequences to enclose your declarations, as shown below.
<%@ page import="java.util.*" %>
<HTML>
<BODY>
<%!
    Date theDate = new Date();
    Date getDate()
    {
        System.out.println( "In getDate() method" );
        return theDate;
    }
%>
Hello!  The time is now <%= getDate() %>
</BODY>
</HTML>

Basic Lesson 8:- Tags in JSP

Another important syntax element of JSP are tags.  JSP tags do not use <%, but just the < character.  A JSP tag is somewhat like an HTML tag.  JSP tags can have a "start tag", a "tag body" and an "end tag".  The start and end tag both use the tag name, enclosed in < and > characters.  The end starts with a / character after the < character.  The tag names have an embedded colon character : in them, the part before the colon describes the type of the tag.  For instance:
<some:tag>
body
</some:tag>

Basic Leson 9:-JSP Sessions

On a typical web site, a visitor might visit several pages and perform several interactions.
If you are programming the site, it is very helpful to be able to associate some data with each visitor.  For this purpose, "session"s can be used in JSP.
A session is an object associated with a visitor.  Data can be put in the session and retrieved from it, much like a Hashtable.  A different set of data is kept for each visitor to the site.
Here is a set of pages that put a user's name in the session, and display it elsewhere.  Try out installing and using these.
First we have a form, let us call it GetName.html
<HTML>
<BODY>
<FORM METHOD=POST ACTION="SaveName.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

Basic Lesson 10:-Beans and Form processing in jsp

Forms are a very common method of interactions in web sites.  JSP makes forms processing specially easy.
The standard way of handling forms in JSP is to define a "bean".  This is not a full Java bean.  You just need to define a class that has a field corresponding to each field in the form.  The class fields must have "setters" that match the names of the form fields.  For instance, let us modify our GetName.html to also collect email address and age.
The new version of GetName.html is
<HTML>
<BODY>
<FORM METHOD=POST ACTION="SaveName.jsp">
What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR>
What's your e-mail address? <INPUT TYPE=TEXT NAME=email SIZE=20><BR>
What's your age? <INPUT TYPE=TEXT NAME=age SIZE=4>
<P><INPUT TYPE=SUBMIT>
</FORM>
</BODY>
</HTML>

Basic lesson 11:- Controllers in jsp

Sometimes, some pre-processing needs to be done after the user has submitted a form. The result of this pre-processing decides where the control flow should go next. Such pre-processing code is frequently referred to as a "controller".
JSPs are very highly suited for writing simple controllers. For more complex controller logic, JSPs can easily call Java code in other classes. A sample controller is shown below:


<jsp:useBean id="user" class="user.UserData" scope="session"/>
<jsp:setProperty name="user" property="*"/>

Basic lesson 12:-Tag Libraries in jsp

JSP 1.1 introduces a method of extending JSP tags, called "tag libraries".  These libraries allow addition of tags similar to jsp:include or jsp:forward, but with different prefixes other than jsp: and with additional features.
To introduce you to tag libraries, in this tutorial we use the Blazix tag library as an example.  This tag library comes bundled with the Blazix server. If you are not using the Blazix Server, you may just want to review the material to get familiar with the syntax, and continue on to the next page.
Each tag-library will have its own tag-library specific documentation.  In order to use the tag library, you use the "taglib" directive to specify where your tag library's "description" resides.  For the Blazix tag library,  the (recommended) directive is as follows
<%@ taglib prefix="blx" uri="/blx.tld" %>
The "uri" specifies where to find the tag library description.   The "prefix" is unique for the tag library.  This directive is saying that we will be using the tags in this library by starting them with blx:
The Blazix tag library provides a blx:getProperty tag.  This tag can be used to allow the user to edit form data.  In our GetName.jsp file, we will now add a jsp:useBean and place the form inside blx:getProperty.
The new GetName.jsp is

Basic lesson 13:-Techniques for Form Editing in jsp

A tag library such as the one that comes with the Blazix server, may not be available in your environment.  How can you allow similar features without using a tag library?
It is a little tedious, but it can be done.  Basically, you must edit each HTML tag yourself, and put in a default value.  The following examples shows how we modify GetName.jsp to provide features similar to blx:getProperty but with manual HTML tag editing:
<jsp:useBean id="user" class="user.UserData" scope="session"/>

basic lesson 14:Protecting your website with a login page


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 <FORM> tag, with the METHOD set to POST and the ACTION set to "j_security_check".

<FORM METHOD=POST ACTION=j_security_check>