JSP Tutorial  Hot PDF Print E-mail
Tag it:
Delicious
Furl it!
Digg
NewsVine
Reddit
YahooMyWeb
Technorati
Articles Reviews JSP
Written by Bogdan V   
Friday, 01 September 2006
Article Index
JSP Tutorial  Hot
Scriptlets
JSP Directives
JSP Sessions
Beans and Form processing
Tag libraries
Techniques for form editing
Protecting your website with a login page
Sending e-mail from JSP
The prerequisites for the tutorial are:
    * HTML.  You should be able to put together HTML pages.
    * Java.  You should be able to program in Java.


For best progress, it is recommended that you type in all the examples presented and get them working.  The early examples might seem very simple; please have patience.  By actually typing in the examples and getting them to work, you will gain familiarity with the actual practical issues.  Also spend some time on the exercises, these are designed to solidify your grasp of the material. 

If you do not have a JSP capable web-server (sometimes known as application servers for configuration reasons), 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 (6.7 Megabytes)    WebLogic from BEA Systems (44 Megabytes, JSP, Servlets and EJBs)
   
   WebSphere from IBM (105 Megabytes, JSP, Servlets and EJBs)

If you do not already have a server, it is recommended that you download Blazix because it includes a tag library that is used later in this tutorial in the tag library chapter.

Blazix is also very small and can be easily downloaded even over a modem, will work on all kinds of systems including Windows 98, and can be installed in less than ten minutes.

To truly learn JSP, it is really very important that you try out the examples with a real server.   This web-site doesn't have running examples because running examples really don't do a good teaching job.  The best way to learn the technology is to get hands-on experience. 

If you don't have a server, please go get one and install it now!

Once you have a web-server, you need to know the following information about your web-server:

    * 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

                <HTML>

        <BODY>

        Hello, world

        </BODY>

        </HTML>

        

know where to place this file and how to see it in your browser with an http:// prefix. Since this step is different for each web-server, you would need to see the web-server documentation to find out how this is done.  Once you have completed this step, proceed to the next tutorial.

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.

What is happening behind the scenes is that your JSP is being turned into a Java file, compiled and loaded.  This compilation only happens once, so after the first load, the file doesn't take long to load anymore. 

(But everytime you change the JSP file, it will be re-compiled again.) Of course, it is not very useful to just write HTML pages with a .jsp extension!  We now proceed to see what makes JSP so useful.

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.

<HTML>

<BODY>

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

</BODY>

</HTML>

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.

This is what makes it possible to use JSP to generate dyamic HTML pages that change in response to user actions or vary from user to user.

Exercise: 

Write a JSP to output the values returned by System.getProperty for various system properties such as java.version, java.home, os.name, user.name, user.home, user.dir etc.



Last Updated ( Thursday, 06 November 2008 )
 
< Prev