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.

<HTML>
<BODY>
<%
    // 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 %>
</BODY>
</HTML>
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.
<HTML>
<BODY>
<%
    // 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 ));
%>
</BODY>
</HTML>
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:
<HTML>
<BODY>
<%
    // 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( "<BR>Your machine's address is " );
    out.println( request.getRemoteHost());
%>
</BODY>
</HTML>
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.

No comments:

Post a Comment