Professional Documents
Culture Documents
CONTENTS OF PRESENTATION
JSP DEFINITION JSP SERVERS WHY WE LIKE JSP IN FIRST PLACE HOW JSP WORKS EXAMPLE OF JSP COMPARISON OF JSP TO OTHER TECHNOLOGIES COMPONENTS OF JSP JSP STANDARD ACTIONS JSP SCRIPTING ELEMENTS JSP DIRECTIVE ELEMENTS STANDARD ACTION TAGS JSP LIFE CYCLE METHODS IMPLICIT OBJECTS JSP CUSTOM TAGS ADVANTAGES AND CONSEQUENCES WHAT IS IRRITATING ABOUT JSP OVERVIEW
JSP DEFINITION:
A text-based document capable of returning both static and dynamic content to a client browser. Static content and dynamic content can be intermixed. Static content HTML, XML, Text Dynamic content Java code Displaying properties of JavaBeans Invoking business logic defined in Custom tags
JSP SERVERS:
Blazix from Desiderata Tomcat from Apache Weblogic from BEA WebSphere from IBM And others
Open standard with support from many vendors The performance and scalability of servlets (for JSP pages compile into servlets) Extensibility (custom tags) Easy integration with other J2EE and Java technologies (Servlets, EJB)
Example JSP
<HTML> <BODY> Hello! The time is now <%= new java.util.Date() %> </BODY> </HTML> Notes: The <%= ... %> tag is used, because we are computing a value and inserting it into the HTML.
Comparison of JSP to
PHP Cold Fusion ASP JSP
Cost
Free
Free
Language In Page
PHP
CFML
VBScript, JScript
Java
OS Platform
Windows 9x, NT, other platforms requires third-party ASP porting products IIS, Personal Web Server (PWS), other servers with thirdparty products (ChiliSoft!)
iPlanet/Netscape Enterprise Server (NSAPI), MS Internet Information Server (IIS), Apache, Zeus, fhttpd, etc. (ver 4)
Supported Database
Portability
Good
Fair
Good
Scalability
Fair
Good
Good
Good
Component Support
COM components
Learning curve
Low
High (Java)
COMPONENTS OF JSP:
Standard actions are specific tags that affect the runtime behavior of the JSP and affect the response sent back to the client. EXAMPLE: <jsp:forward page="relativeURL"/> Here, Forwarding a request means that even the request object is forwarded to the target page.
JSP Directives serve as messages to the JSP container from the JSP. They are used to set global values such as class declaration, methods to be implemented, output content type, etc. All directives have scope of the entire JSP file. A directive is an option setting that affects how the servlet is constructed from a JSP page. The three directives are Page, Include and Taglib. The Page directive can contain the list of all imported packages. Example: <%@ page attribute=value %> Here, attribute can be import, contentType, errorPage, etc. The Include directive is used to physically include the contents of another file. Example: <%@ include file= relativeURL %>
<jsp:useBean> imports a JavaBeans component. <jsp:getProperty> gets a property value and adds it to the response. <jsp:setProperty> sets a property value. <jsp:param> adds a parameter to a request. <jsp:plugin> generates the OBJECT or EMBED tag for an applet. <jsp:forward page=Account.jsp> Terminates execution of current page Forwards the call to a servlet or JSP page for service. Transfers control to the forwardee <jsp:include page=Account.jsp> Executes the specified page Includes the response from a servlet or JSP page that is called for the request. Then returns control to the current page
The javax.servlet.jsp.JspPage interface contains two methods: 1. public void jspInit() - This method is invoked when the JSP is initialized and the page authors are free to provide initialization of the JSP by implementing this method in their JSPs. 2. public void jspDestroy() - This method is invoked when the JSP is about to be destroyed by the container. Similar to above, page authors can provide their own implementation. The javax.servlet.jsp.HttpJspPage interface contains one method: public void _jspService(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException This method generated by the JSP container is invoked, every time a request comes to the JSP. The request is processed and the JSP generates appropriate response. This response is taken by the container and passed back to the client.
Implicit Objects:
JSP Implicit Objects are certain pre-defined variables that can be included in JSP expression and scriptlets and are only visible within the system generated service method. Of the implicit objects, there are three most used implicit objects that are used to handle the current session , the incoming request, and the outgoing response. These three are session, request, and response respectively.
Custom tags are user-defined , re-usable tags that is used to perform repetitive tasks in a JSP page. It can be empty, can contain attributes, body or nest with another tags. A Tag Handler is a java class that implements the functionality of a custom tag. A Tag Library Descriptor (TLD) file defines the functionality of a custom tag. A Taglib element is the root element of a TLD file and it imports a custom tag into current JSP page. Tag Files: nature and purpose Solve difficulty of reusing text/HTML within a tag. And makes it much easier to write simple tags, since you can do so in JSP instead of Java. Stand-alone file with <%@ tag %> directive instead of traditional <%@ page %> directive.
Advantages Efficient Convenient Server engines typically have built-in infrastructures to handle parsing/decoding HTML form data, reading/setting HTTP headers, session tracking Powerful Java behind the scene Portable Write once, run anywhere Inexpensive Various free or inexpensive engines available
Consequences Need for a Server Engine Another engine to maintain Un-acceptably by the client machine Virtual hosting of unrelated sites on a single machine can cause problems (choose your server engine wisely) High Learning Curve Steeper learning curve than Cold Fusion
OVERVIEW OF JSP:
JSP provide excellent server side scripting support for creating database driven dynamic web applications. The Java is enclosed in special tags, such as <% ... %>. JSP files must have the extension .jsp and it is secure, fast and independent of sever platforms. Elements that are processed on server are Directives, Scripting and Actions. JSP lifecycle consists of two phase: TRANSLATION phase and REQUEST PROCESSING phase. In Translation phase, JSP Code is translated into java servlets which is then compiled. It is done by the web container itself. In Request processing phase, a response to the clients request is been sent. JSP Life cycle methods are jspInit(), jspService() and jspDestroy().