Monday, August 24, 2009

Chapter 06: Digging JSP

We discussed some basics of JSP in previous chapter. Let’s try and get along more with JSP :)

Focus for the chapter would be:

  1. Scriptlets
  2. out Object
  3. request Object

1.Scriptlets

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". 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>

<%
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 file. It will not print on the browser.

2. out Object

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
<%
out.println(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.

Greetings ! Example

Ok. Lets try out a more informative example. This is the scenario.

  1. If it’s a morning, it should greet me “Good Morning. Have a nice day”.
  2. If it’s a evening, it should greet me “Good Evening, Good day”.

Ok. Try this example & let me know if you are able to do it…

3. request Object

Another very useful pre-defined variable (or object) 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.

Download Video Source: High-Quality : 15 MB

Click below arrow to download….




4 comments:

  1. java.util.Date date=new java.util.Date();
    String t="PM";
    String Date_format="a";
    java.text.SimpleDateFormat obj=new java.text.SimpleDateFormat(Date_format);
    if(obj.format(date).equalsIgnoreCase(t)) {
    out.println("Good Evening, Good day");}
    else {
    out.println("Good Morning. Have a nice day");
    }

    ReplyDelete
  2. java.util.Date date=new java.util.Date();
    if(date.getHours()>16)
    {
    out.println("Good Evening,Good day");
    }
    else
    {
    out.println("Good Morning.Have a nice day");
    }

    ReplyDelete
  3. you are doing a great job.. Its really useful pa..thank you so much..

    ReplyDelete
  4. Thank u soooooooooooooooooooooooooo much ...

    ReplyDelete

Our Google Group

Our Facebook Group