|
JSP - Part 1
This document describes those features introduced in phase 1. It is recommended that you take a look at the example JSP projects available from our website's 'Resources' page. These include a JSP version of 'Duke's Book Store' to parallel the servlet based version. TJI makes the development of JSP projects easy by providing an integrated Web Server that requires no setup and that updates automatically when a JSP page or tag handler is changed; there is no need to reload it by hand or by button, and still less need to restart the server! The Java IDE jar (ide.jar) includes all the required JSP packages - so users without an 'Enterprise Edition' SDK can develop and run JSP projects without needing to download any extras. JSP projects run on a server. Normally (after deployment), this will be a server on the web or a network. However, during development your JSP projects can run in the integrated Kinabaloo Web Server and you can see their results (responses) either in a browser (such as Netscape or Internet Explorer) or in the integrated Web Browser (the 'Web' tab of The Java IDE). If you set the starting page (servlet, JSP or HTML) - by setting it as the project's 'main' or 'html' file, the integrated Web Browser will automatically be selected and then load that page when you select 'Run'. If you wish to use TomCat or other web server you may do so by copying your project to the the required location. For example, project JSP1 is located in TJI at homepath/web/JSP1. Simply copy directory JSP1 and paste it in (i.e. under) the webapps directory of your TomCat installation; the sub-directory structure (WEB-INF/classes) is the same in TJI and TomCat. TomCat handles the core tags automatically. You will need to copy any custom tags used to TomCat and set up TomCat to use them by writing an XML structured Tag Library Descriptor (.tld) file. You may also need to modify TomCat's web.xml file. See the TomCat documentation and examples for further help on this topic.
Introduction JavaServer Pages (JSP) lets you separate the dynamic part of your pages (the 'business logic' - java code) from the static HTML (the 'presentation'). Whereas servlets are written as java source files which produce HTML output, JSP files are basically HTML files - but dynamic ones that are altered by Java code that is embedded using special tags. The most basic types of JSP tag start with "<%" and end with "%>". A JSP enabled server will handle these JSP tags and a modified version of the HTML will then be sent to the client's browser. For example, here is a section of a JSP page that results in something like "Thanks for ordering TJI !" for a URL of: http://host/OrderConfirm.jsp?product=TJIversion1.2 <html><body><p>Thanks for ordering :<b> JSP files have a .jsp extension, and typically are installed in the same location on the server as html files. Although what you write often looks more like a regular HTML file than a servlet, behind the scenes the JSP page is converted to a normal servlet, with the static HTML simply being sent out to the browser (if the coding dictates). This conversion process is called 'translation'. On a dedicated server, this translation is normally done the first time that the page is requested, and developers can simply request the page themselves when first installing it if they want to be sure that the first real user doesn't get a momentary delay while the JSP page is translated to a servlet and the servlet is compiled and loaded. (Note that many Web servers let you define 'aliases' that so that a URL that appears to reference an HTML file really points to a servlet or JSP page.) In TJI, a JSP page is translated to a servlet and that servlet compiled when the project containing it is built (as required). If the TJI Web Server detects that a requested JSP page has not yet been translated and compiled - or that the JSP source file has since been changed - it too will translate and compile the JSP page. At this point, all loaded servlets are reloaded. In many cases, a large percent of your JSP page just consists of static HTML, known as 'template text'. In just about all respects, this HTML looks just like normal HTML, follows all the same syntax rules, and is simply "passed through" to the client by the servlet created to handle the page. Aside from the regular HTML, there are three main types of JSP constructs that you can embed in a JSP page:
Scripting elements let you specify Java code that will become part of the resultant servlet, directives let you control the overall structure of the servlet, and actions let you specify existing components that should be used, and otherwise control the behavior of the JSP engine. To simplify the scripting elements, you have access to a number of predefined variables such as request in the snippet above. Note that phase 1 of TJI's support for JSP covers most of the features of version 1.0 of the JSP specification. Version 2.0 of JSP takes the functionality of JSP further, allowing scriptlets to be off page and referenced by simple looking tags. This will be covered in TJI's JSP parts 2 and 3. Note that well-written JSP pages try to avoid using scriptlets, and rely more on tags and beans to much better separate business logic from presentation. By using the standard tags and custom tags, one can better separate the java code into off-page 'tag handlers' which are called automatically by the special HTML-like JSP tags. This is a natural progression for JSP. More on this in part 2.
Predefined variables (Implicit Objects)
The predefined variables are available inside both scriptlets and expressions. Comments
Everything inside - even other tags - is ignored. Expressions
An expression like this is evaluated and sent to the output stream. That is, the Java expression is evaluated, converted to a string, and inserted into the page. This evaluation is performed at run-time (when the page is requested and the expression is processed), and thus has full access to information about the request.
A second use of expressions - with custom tags - is described in JSP Part 2. Scriptlets
The java code is inserted in the servlet's service / doGet method. If you want to do something more complex than insert a simple expression, JSP scriptlets let you insert arbitrary code into the servlet method that will be built to generate the page. Scriptlets have access to the same automatically defined variables as expressions. So, for example, if you want output to appear in the resultant page, you would use the predefined variable out. <%String queryData = request.getQueryString(); Note that code inside a scriptlet gets inserted exactly as written, and any static HTML (template text) before or after a scriptlet gets converted to print statements. This means that scriptlets need not contain complete Java statements, and blocks left open can affect the static HTML outside of the scriptlets. For example, the following JSP fragment, containing mixed template text and scriptlets :
will get converted to something like:
Declarations
Notice the exclamation mark! Code is inserted in the servlet outside of the service / doGet method - that is, it will persist as long as the servlet is loaded. In other words, a JSP declaration lets you define methods or fields that get inserted into the main body of the servlet class (outside of the method processing the request). Hence this is a way to define persistent variables and add methods. Since declarations do not generate any output, they are normally used in conjunction with JSP expressions or scriptlets. For example, here is a JSP fragment that prints out the number of times the current page has been requested since the server was last booted (or the servlet class was changed and reloaded):
Page Directives
jsp:include Action
The included file should be either an html or jsp file; TJI supports both. This action lets you insert files into the page as it is being generated at run-time ('dynamic include'). Unlike the include directive, which inserts the file at the time the JSP page is translated into a servlet, this action inserts the file at the time the page is requested. This pays a small penalty in efficiency, but it gains significantly in flexibility. For example, imagine a JSP page that inserts two different snippets into a "What's New?" Web page. Each time the headlines change, authors only need to update the two included files and can leave the main JSP page unchanged.
jsp:forward Action
The forwarded file should be either an html or jsp file. This action lets you forward the request to another page. It has a single attribute, page, which should consist of a relative URL. This could be a static value, or could be computed at request time, as in the two examples below :
jsp:useBean Action
Finds or creates an instance of (creates if not already created with the given id) the specified java bean class and names this instance the value of id. For example :
The bean will exist for the duration of the specified scope. If no scope is specified, the default scope is used - 'page' scope. The four possible values are : "page", "request", "session" and "application".
Even if a bean's scope is defined as "session", say, you will still need to place a jsp:useBean tag on each page that will use it. This is why the tag name is useBean and not createBean. A new bean is created only when a bean with the specified id does not already exist - such as would be the case after a session expires for a session-scope bean, whereupon a new bean will be created by jsp:useBean. You can read bean properties using a JSP expression, by using a scriptlet that calls the appropriate getXxx method, or more commonly, by using the jsp:getProperty action. Note: jsp:useBean must be called before trying to set or get its properties.
jsp:setProperty Action
Sets bean properties, by calling the appropriate setter method of the bean. For example, if the property is 'size' the bean's setSize() method will be called.
Special Case :
When the property is set to "*", it means "set every bean property whose name matches each of the request parameters". Request parameters are mostly set by HTML/JSP forms and so this useful functionality allows form data to be stored in a Java Bean very simply. The Java Bean can then perform complex validation or other processing on the data.
jsp:getProperty Action
This element retrieves the value of a bean property, converts it to a string, and inserts it into the output. The two required attributes are name, the name of a bean previously referenced with jsp:useBean (its id, not class), and property, the property whose value should be retrieved and inserted into the output. Here is an example : <jsp:useBean id="itemBean" class="ItemBean" scope="session" />...<ul><li>Number of items:<jsp:getProperty name="itemBean" property="numItems" /></li><li>Cost of each item:<jsp:getProperty name="itemBean" property="unitCost" /></li></ul>...
<html><body><jsp:useBean id="test" class="SimpleBean" session="request" /><jsp:setProperty name="test" property="message" value="Hello WWW" /><p align="center">Message:<b><jsp:getProperty name="test" property="message" /></b></p></body></html>
This might make the use of Beans seem trivial - but remember that a 'getter' method could retrieve the returned object / value from a database or be calculated in some other way; a 'setter' method can validate the object or value that it receives. Hence, although a bean seems like a simple black box from the outside, it can be complex on the inside. Indeed, that's the whole purpose of Java Beans - to hide complexity and present a standard way of interacting with the outside world. |