Showing posts with label basics jsp tutorial. Show all posts
Showing posts with label basics jsp tutorial. Show all posts

Thursday, August 22, 2013

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>