Docstoc

J2EE

Document Sample
J2EE Powered By Docstoc
					Introduction to JSP
Basics
Java Server Pages (JSP) provide a facility whereby you can write sever-side scripted pages using the full power of the Java programming language and the rich set of class libraries associated with Java. However there is a price to pay in that JSP, like most things to do with Java, is significantly more complex than other techniques.

How JSP works
JSP pages are, at first glance, rather similar to ASP and PHP pages in that special pseudo-tags are used to mark out parts of otherwise normal HTML pages that are to handled specially. The difference is that, whereas ASP and PHP are processed by a server plug-in, JSP is handled by a separate server process. For JSP this process runs software known as Tomcat which takes the JSP page and converts the entire page into a Java programme which is then compiled. The raw HTML in the original page is wrapped in Java output methods. On requesting a JSP page, the request is transferred from the normal WWW server (e.g. Apache) to Tomcat which first checks whether it has cached an already compiled version of the Java programme associated with the page. Recompilation takes place if the last modification date stamp on the JSP page is more recent than the last modification date stamp on any cached compiled version.

The local Tomcat installation
In order to create a JSP page it is not sufficient to create a syntactically correct page in a file whose name ends in ".jsp". Tomcat requires that all JSP pages be in subdirectories of the Tomcat installation, this can be tricky to arrange on a multi-user installation such as the SCIT server clun.scit.wlv.ac.uk and the SCIT Tomcat server has been configured to regard the whole of the user disc space as lying within the Tomcat installation hierarchy. As a consequence of this the URLs of JSP pages are rather long-winded. For example,

the second example of the demonstration set is actually stored in the file
/home/staff/acad/jphb/public_html/sst/jsp/ex2.jsp

To access this page, you need to quote the URL
http://www.scit.wlv.ac.uk/jsp/home/staff/acad/jphb/publi c_html/sst/jsp/ex2.jsp

You may also be interested to know that the Java programme built by Tomcat for the page quoted above is stored in the file
/util/jakartatomcat/work/localhost_8080/_0002fjsp_0002fhome_0002fstaf f_0002facad_0002fjphb_0002fpublic_0005fhtml_0002fsst_000 1fjsp_0002fex_00032_0002ejspex2_jsp_3.java

You'll see similar file names when you make a mistake in your JSP. Unfortunately on our system these files are not publically accessible which is annoying if you are an experienced Java programmer and want to see what is going on (and can type in that long a file name without making a mistake !).

The Pseudo-Tags
Unlike ASP and PHP which make do with a single style of pseudo-tagging to indicate the parts of a page which need to be interpreted, JSP uses five different sorts of pseudo-tag. Pesudo-tag
<%-- ... --%> >JSP comment. <%! .... %> <%@ .... %> <%= .... %> <% ..... %> Declaration of variables or methods Include files, define attributes and tag libraries Convert value of expression to string and write to output Code (which can, of course, include declarations)

Meaning

The tag <jsp:....> is also understood with a variety of meanings (via the tag library). Defined attributes etc., are of page-wide significance and can include things such as importing standard Java class packages.

Standard Classes
Our Tomcat installation imports the following standard class packages, if you want methods from any other class you'll need to import them explicitly.
import java.io.PrintWriter; import java.io.IOException; import java.io.FileInputStream; import java.io.ObjectInputStream; import java.util.Vector; import java.beans.*;

However the JSP documentation suggests that the following are imported by default.
import java.lang.*; import javax.servlet.*; import javax.servlet.jsp.*; import javax.servlet.http.*;

Other Classes
Remember that the Tomcat server operates by building a Java programme from your JSP page and then compiling that programme (using javac) and running it (using java) on request. It follows that the CLASSPATH and standard class libraries used are Tomcat's standard configuration and not your configuration, there is, unfortunately, no way to change this. Only the system administrator can install extra class libraries for use by Tomcat (contact via the helpdesk). Currently the only extra class library installed is the MySQL database package JDBC driver library.

If you want to use the MySQL JDBC driver in a Java programme of your own you'll need to include
/home/staff/acad/jphb/myjars/mysql.jar

in your CLASSPATH.

General Points
The previous refer specifically to the Tomcat set up at the University of Wolverhampton's School of Computing. For a variety of reasons this set up is not typical. In general Tomcat will be installed in a sub-directory of /usr/local called tomcat. Beneath the tomcat directory will be found a subdirectory called webapps. Within the webapps directory there will be a set of subdirectories for particular applications, you may well find one called ROOT. Within the subdirectories of webapps you will find directories called lib and classes. These are the locations where Tomcat looks for user supplied classes and .jar files. Author Peter Burden


				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:180
posted:1/15/2008
language:English
pages:5