Language Evaluation Criteria

Download Report

Transcript Language Evaluation Criteria

11. Java-Based Web: JSP, JSF
2
Motto:
Rule 1: Our client is always right
Rule 2: If you think our client is wrong, see Rule 1.
- Anonymous
3
•
•
•
•
•
Servlets
JSP (JavaServer Pages)
JSF (JavaServer Faces)
JavaBeans components
Netbeans IDE (Integrated Development Environment) for
Java-based web applications
• JSP Example
4
Servlets
• Java-based components that use the HTTP request-response model
of client-server communication
• Servlet "container" executes servlets and manages their life cycle
•
Servlet APIs
– javax.servlet and javax.servlet.http
5
Servlet Container
1.
Container Receives an HTTP request from a client
2.
Container forwards the request to the corresponding servlet
3.
Servlet processes the request and generates XHTML document
4.
Container returns the page to the client
6
Servlet Life Cycle
• Servlet must implement javax.servlet.Servlet interface
• I.e., it must implements methods called by servlet container
when the servlet’s life cycle changes
– init()
• called only the first time there is a request for the servlet
– service()
• called for each request
• receives the request, processes it and generates a response
– destroy()
• called when servlet is terminated
• releases all resources
7
JSP Concepts
• JSP (JavaServer Pages) are web page templates that
correspond to servlets
• JSP allow to
– separate presentation from content
– create dynamic content
• predefined components
• server-side scripting for interaction with the components
• JSP container converts a JSP page into a servlet
– upon the first request for the page
– the servlet handles all requests to the JSP
• JSP use the same request/response mechanism as servlets
• JSP APIs
–
javax.servlet.jsp and javax.servlet.jsp.tagext
8
JSP Components
• Key components
–
–
–
–
–
directives
actions
scripting elements
tag libraries
static content
• JavaBeans and custom tag libraries
– encapsulate complex, dynamic functionality
– hide code for complex operations
• e.g., database access
– allow web-page designers to add dynamic content to web pages
• even without knowledge of Java
• JSTL (JavaServer Pages Standard Tag Library)
– provide many common web application tasks
9
JSP Components (cont.)
•
Directives
– define page settings and content from other resources
– add custom tag libraries
– send messages to JSP container
•
Actions
– implement functionality of predefined tags
– implement client requests
– create Java objects needed in JSP
•
Scripting elements
– Java code that interacts with JSP components and process requests
•
Tag libraries
– let programmers create custom tags
– JSTL provide standard, predefined tags
•
Static content
– XHTML or XML markup
– text: translated to a String object in the corresponding servlet
10
JSF
• JavaServer Faces
– simplify the design of an application’s GUI
– separate a web application’s presentation from its
business logic
• JSF custom tag libraries
– user interface components
– APIs for handling component events
• The programmer defines
– look-and-feel of a JSF page by adding custom tags to a JSP file
and setting the tag's attributes
– page’s behavior in a separate Java code file
11
Netbeans
• IDE (Integrated Development Environment) for Java
– from Sun, but free
– similar, but different from Eclipse
– plugins for web applications development
• Netbeans includes
– visual designer for dropping components onto a page
– text editor editing its .jsp file manually
• Netbeans web applications
– JSPs built with JSF
• .jsp file contains also page’s GUI elements
– a page bean (i.e. a JavaBean class) for each JSP page
12
Page Beans
• Page bean defines
– properties of page elements
• getProperty()and setProperty() methods
– event handlers
– methods
• page life-cycle methods
• supporting code
• Netbeans web application is composed of
–
–
–
–
page beans
RequestBean
SessionBean
ApplicationBean
13
Web Application Beans
• RequestBean
– request scope
– exists only during an HTTP request
• SessionBean
– session scope
– exists while the user is browsing or until the session times out
– unique for each user
• ApplicationBean
– application scope
– shared by all instances of the application
• regardless of the number of open sessions
– exists while the application is deployed
– application-wide data storage and processing
14
Example
• copyright 2007 Pearson Education
15
Example's JSP
<?xml version = "1.0" encoding = "UTF-8"?>
<!–- Textbook Fig. 26.1: Time.jsp copyright 2007 Pearson Education -->
<jsp:root version = "1.2" xmlns:f = "http://java.sun.com/jsf/core"
xmlns:h = "http://java.sun.com/jsf/html" xmlns:jsp = "http://java.sun.com/JSP/Page"
xmlns:webuijsf = "http://www.sun.com/webui/webuijsf">
<jsp:directive.page contentType = "text/html;charset=UTF-8" pageEncoding = "UTF-8" />
<f:view>
<webuijsf:page binding = "#{Time.page}" id = "page">
<webuijsf:html binding = "#{Time.html}" id = "html">
<webuijsf:head binding = "#{Time.head}" id = "head" title = "Web Time">
<webuijsf:link binding = "#{Time.link}" id = "link" url = "/resources/time.css"/>
<webuijsf:meta content = "60" httpEquiv = "refresh" />
</webuijsf:head>
<webuijsf:body binding = "#{Time.body}" id = "body" style = "-rave-layout: grid">
<webuijsf:form binding = "#{Time.form}" id = "form">
<webuijsf:staticText binding = "#{Time.timeHeader}" id = "timeHeader"
style = "font-size: 18px; left: 24px; top: 24px; position: absolute"
text = "Current time on the web server:" />
<webuijsf:staticText binding = "#{Time.clockText}" id = "clockText"
style = "background-color: black; color: yellow; font-size: 18px; left: 24px;
top: 48px; position: absolute" />
</webuijsf:form>
</webuijsf:body>
</webuijsf:html>
</webuijsf:page>
</f:view>
</jsp:root>
16
Example's Java Code
// Textbook Fig. 26.3: Time.java copyright 2007 Pearson Education
// Page bean file that sets clockText to the time on the Web server.
package webtime;
import com.sun.rave.web.ui.appbase.AbstractPageBean;
import com.sun.webui.jsf.component.*;
import java.text.DateFormat;
import java.util.Date;
import javax.faces.FacesException;
public class Time extends AbstractPageBean {
private
private
private
private
private
private
private
private
private
Page page = new Page ();
Html html = new Html ();
Head head = new Head ();
Link link = new Link ();
Body body = new Body ();
Form form = new Form ();
StaticText timeHeader = new StaticText ();
StaticText clockText = new StaticText ();
Meta meta = new Meta ();
public Page getPage () {return page;}
public void setPage (Page page) {this.page = page;}
public Html getHtml () {return html;}
public void setHtml (Html html) {this.html = html;}
public Head getHead () {return head;}
public void setHead (Head head) {this.head = head;}
17
Example's Java Code (cont.)
public Link getLink () {return link;}
public void setLink (Link link) {this.link = link;}
public Body getBody () {return body;}
public void setBody (Body body) {this.body = body;}
public Form getForm () {return form;}
public void setForm (Form form) {this.form = form;}
public StaticText getTimeHeader () {return timeHeader;}
public void setTimeHeader (StaticText text) {this.timeHeader = text;}
public StaticText getClockText () {return clockText;}
public void setClockText (StaticText text) {this.clockText = text;}
public Meta getMeta () {return meta;}
public void setMeta (Meta meta) {this.meta = meta;}
/* Constructs new Time */
public Time () {}
/* Initializes page */
public void init () {super.init ();}
18
Example's Java Code (cont.)
/* Called on postback */
public void preprocess () {}
/* Sets the clock. Called before page is rendered */
public void prerender () {
DateFormat formatter = DateFormat.getTimeInstance (DateFormat.LONG);
clockText.setValue (formatter.format (new Date ()));
}
/* Called after page is rendered */
public void destroy () {}
/* Returns a request bean */
protected RequestBean1 getRequestBean1 () {
return (RequestBean1) getBean ("RequestBean1");
}
/* Returns a session bean */
protected SessionBean1 getSessionBean1 () {
return (SessionBean1) getBean ("SessionBean1");
}
/* Returns an application bean */
protected ApplicationBean1 getApplicationBean1 () {
return (ApplicationBean1) getBean ("ApplicationBean1");
}
}