JavaTechie

Its all about Technology

Jsp November 3, 2007

javatechie @ 12:03 pm

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:
<a href=”http://tomcat.apache.org/”>TomCat</a> from Apache (Approx 6 Megabytes)

Once you have a JSP capable web-server or application server, you need to know the following information about it:

* Where to place the files
* How to access the files from your browser (with an http: prefix, not as file:)

You should be able to create a simple file, such as

<blockquote>
<pre><tt><HTML>
<BODY>
Hello, world
</BODY>
</HTML></tt></pre>

</blockquote>

know where to place this file and how to see it in your browser with an http:// prefix.

Adding dynamic content via expressions

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.

<pre><HTML>
<BODY>
Hello!  The time is now <%= new java.util.Date() %>
</BODY>
</HTML></pre>

Notice that each time you reload the page in the browser, it comes up with the current time.

The character sequences <%= and %> enclose Java expressions, which are evaluated at run time.
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”. 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.

<pre><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></pre>

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.

<pre><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></pre>

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:

<pre><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></pre>

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.

 

Leave a Reply