Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Topics
JSP Web components JSP or Servlets JSP syntax, Expression Language Integration of JSPs and Servlets Standard Tag Library, Custom Tag Library
20 February 2008
20 February 2008
20 February 2008
JSP Basics
JSP Life Cycle
JSP is a Web server side programming technology based on Servlet technology. The JSP specification is built on the top of the Servlet API. Any JSP page is translated into a Java Servlet by a JSP engine at runtime so that the JSP life cycle is determined by Servlet technology. The JSP engine is a JSP specification implementation which comes with web servers that implement JSP. Tomcat is one example. When a request comes to a JSP page, it may come from client browser or come from another Web component such as a Servlet or JSP.
20 February 2008
11
20 February 2008
12
JSP termination
<%! public void jspDestroy(){ . . . } %> This method allows developers to specify resource cleanup jobs such as database disconnections.
JSP example
20 February 2008
15
JSP example.
20 February 2008
16
JSP example
We can also use JSP expression language of the JS P 2.0 to rewrite the helloJsp.jsp as follows.
<%@ page isELIgnored = "false" %> <html> <body> Hello, ${param['userName']}! </body> </html>
JSP example.
The expression language makes data access much easier. You can use the expression ${param['userName']} instead of <%= request.getParameter("userName") %> to get the userName parameter on the HTML form.
10
JSP Directives
A JSP directive instructs the JSP engine to configure the structure of the Servlet that is generated from the JSP pages. The JSP directives do not produce any Java target code for the translated Servlet class. The JSP directives only affect the Servlet settings such as page settings, and inclusion of packages, classes, and custom tag libraries. JSP directives are processed at JSP translation time instead of execution time. The general syntax of a JSP directive is: <%@ directive attribute1=value1 attribute2=value2 . . . attributen=valuen %>
11
12
13
14
Declaration Element
The JSP declaration element defines pagescope variables to store information or supporting methods that the rest of a JSP page may need. Syntax: <%! Java Code %> Declaration elements do not produce outputs. The contents of the declaration scripting element will be translated into Servlet Java code and inserted into the body of the translated corresponding Servlet code somewhere outside of any methods. Variable declaration example: <%! int i=0; Double d; %>
15
16
Scriptlet Element
JSP scriptlets are executed at the runtime. The syntax is: <% Java code %> The scriptlets are inserted into the _jspService() method of the translated Servlet code that is invoked by the Servlet service() method. This java code is executed when the request is serviced by the JSP page. Example: <% for (int i=1; i<=4; i++) { %> The counter is now <% = i %> <% } %> mixes a scriplet, an expression and HTML
17
18
JavaBean Component
A JavaBean is a Java class that is constructed following the rules below: Include a zero-argument constructor Do not provide any public instance variables (properties) Provide access to non-boolean properties of a bean through get and set methods: getXXX() and setXXX() public methods where XXX is the name of the property; Provide access to boolean properties through isXXX() and setXXX() methods.
19
JavaBean Component.
The JSP actions can only call set and get methods. JSP scriptlets can also call the ismethods. The bean class file is placed in Tomcat WEBINF/classes along with the Servlet classes. For example, the following Book JavaBean class is stored in <ROOT>/WEBINF/classes/shopping/ directory. Each JavaBean class must have its package just like the shopping package for the Book class in the example.
JavaBean Component..
package shopping; public class Book { String Title; String isbn; float price; int quantity; public Book() { title=""; isbn=""; price=0; quantity=0; } public void setTitle(String name) { title=name; } public String getTitle() { return title; } public void setIsbn(String id) { isbn=id; } public String getIsbn() { return isbn; } public void setPrice(float p) { price=p; } public float getPrice() { return price; } public void setQuantity(int q) { quantity=q; } public int getQuantity() { return quantity; } }
20
21
jsp:setProperty Action
You use jsp:setProperty to assign values to properties of a bean. You can either use jsp:setProperty after, but outside of, a jsp:useBean element or within the body of jsp:useBean action element. Option 1: <jsp:useBean id="myBook" ... /> ... <jsp:setProperty name="myBook" property="price" ... /> In this case, the jsp:setProperty is executed regardless of whether a new bean was instantiated or an existing bean was found.
22
jsp:setProperty Action.
Option 2: <jsp:useBean id="myBook" ... > ... <jsp:setProperty name="myBook" property="price" ... /> </jsp:useBean> In this case, the jsp:setProperty is executed only if a new object is instantiated. When the bean object already has been instantiated, it is ignored.
jsp:setProperty Action..
The required name attribute is the id of the bean specified in <jsp:useBean> action tag. The required property attribute specifies the property you want to set. The valueattribute specifies the value for the property. The String type value will be converted to the correct type for the property. The following action sets myBooks price property to 31.99. <jsp:setProperty name="myBook" property="price" value = 31.99 />
23
jsp:setProperty Action
You can also associate the bean properties with the input parameters. The next example shows that the bookPrice input parameter in the request form or query string is associated with the price property of myBook bean. <jsp:setProperty name="myBook" property="price" param="bookPrice" /> Or you can associate all properties with input request parameters with the * wildcard character. Assume that there are four input request parameters whose names match four property names: title, isbn, price, and quantity. Whatever data you input to the parameters will be assigned to the properties with same names. This is one of the most useful javaBean features. <jsp:setProperty name="myBook" property="*" />
jsp:setProperty Action.
You can't use both value and param at same time, but it is allowed to use neither. If you use neither param nor value, that indicates that a parameter name matches the property name. JSP will take the input from the parameter which has the same name as the property name. jsp:setProperty action is much more powerful than the setXXX() methods in that it can automatically convert String type data to the required data type of the bean property and set multiple properties using only one single command.
24
25
JSP Comments
You can always include HTML comments in JSP pages so that users can view these comments with the browsers view option on HTML source. If you don't want users to see your comments, you should embed them within a JSP comment tag: <%-- comment for server side only --%> A useful feature of JSP is that the JSP comment can be used to selectively block out scriptlets or tags from compilation. Thus, comments can play a significant role during the debugging and testing process.
26
27
28
29
30
31
32
33
34
request context
write read
req1
jsp
write
forward
include
read
jsp
response
Session1
session context
read read
Application
req2
jsp
write
forward
read
jsp
response
application context
write read
Session2
reqn
jsp
jsp
response
35
custom Tag
36
37
Here the <jsp:text> is a tag to enclose all template text in the generated outputs. This JSP XML only displays messages. Lets see the next JSP XML document, second.jspx, which interacts with clients.
38
If you add an attribute contentType=text/html in this jspx file as follows you will see a page displayed in HTML format instead of XML document format.
39
40
References
K. Qian et al., Java Web Development Illuminated, Chapter 4 Designing Enterprise Applications with the Java2EE platform: Ch 4 http://java.sun.com/blueprints/guidelines/designing_en terprise_applications_2e/index.html The Java EE 5 Tutorial CH 3, 4, and 5: http://java.sun.com/javaee/5/docs/tutorial/doc/index.ht ml Sang Shin: Introduction and Web core technologies: http://www.javapassion.com/j2ee/index.html#Web_C ore_Technologies:_Servlets_JSP
20 February 2008
82
41