1 / 40

Pre-assessment Questions Identify the correct statement.

Pre-assessment Questions Identify the correct statement. a) The Request Dispatcher object can only be used to include the output of another document. b) The Request Dispatcher object can only be used to forward the request to another document.

chrisreed
Download Presentation

Pre-assessment Questions Identify the correct statement.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Pre-assessment Questions • Identify the correct statement. • a) The Request Dispatcher object can only be used to include the output of another document. • b) The Request Dispatcher object can only be used to forward the request to another document. • c) The Request Dispatcher object can be used to forward the request to another document or include the output of another document. • d) The Request Dispatcher object can neither be used to forward the request to another document nor can be used to include the output of another document. J2EE Web Components

  2. Pre-assessment Questions (Contd.) • 2. Identify the servlet attribute that is inherently thread safe. • a) Local variables • b) Class variables • c) Context attributes • d) Session attributes J2EE Web Components

  3. Pre-assessment Questions (Contd.) • 3. Which method is not included in the life cycle method of a Servlet filter? • a) init() • b) start() • c) doFilter() • d) destroy() • 4. Identify the false statement. • a) A servlet filter is an object that intercepts the requests and responses that flow between a client and a servlet. • b) A servlet filter can modify the headers and contents of a request coming from a Web client and forward it to the target servlet. • c) A servlet filter can also intercept and manipulate the headers and contents of the response that the servlet sends back. • d) A servlet filter can process multiple client requests using a single servlet filter instance. J2EE Web Components

  4. Pre-assessment Questions (Contd.) • 5. Consider the following statements: • Statement A: The include() method of RequestDespatcher class is used to include the output of another document. • Statement B: The forward() method of RequestDespatcher class is used to forward the output to another document. • Identify the correct option. • a) Both, statements A and B are true • b) Statement A is true and B is false • c) Statement A is false and B is true • d) Both, statements A and B are false J2EE Web Components

  5. Solutions to Pre-assessment • Questions • 1. c. The Request Dispatcher object can be used to forward the request to another document or include the output of another document. • 2. a. local variables • 3. b. start() • 4. d. A servlet filter can process multiple client requests using a single servlet filter instance. • 5. b. A is true and B is false J2EE Web Components

  6. Objectives • In this lesson, you will learn about: • Need for JSP • Life cycle of JSP application • Components of JSP • Classes in JSP API • Steps to create and deploy a JSP application J2EE Web Components

  7. JSP Technology • Introduction to JSP Technology • JSP technology facilitates the segregation of the work profiles of a Web designer and a Web developer. • A Web designer can design and formulate the layout for a Web page by using HTML. • A Web developer, working independently, can use Java code and other JSP specific tags to code the business logic. J2EE Web Components

  8. JSP Technology (Contd.) • Differences between servlets and JSP are: • Servlets tie up files to independently handle the static presentation logic and the dynamic business logic. On the other hand, JSP allows Java to be embedded directly into an HTML page by using special tags. • Servlet programming involves extensive coding. Any changes made to the code requires identification of the static code content and dynamic code content to facilitate incorporation of the changes. On the other hand, a JSP page, by virtue of the separate placement of the static and dynamic content, facilitates both Web developers and the Web designer to work independently. J2EE Web Components

  9. JSP Technology (Contd.) • JSP Life Cycle is represented as shown: J2EE Web Components

  10. JSP Technology (Contd.) • JSP life cycle methods are: • jspInit(): Is invoked at the time when the servlet is initialized. • jspService(): Is invoked when request for the JSP page is received. • jspDestroy(): Is invoked before the servlet is removed from the service. J2EE Web Components

  11. JSP Technology (Contd.) • The following code shows the sample structure of a JSP Page: • <%--This is the HTML content--%> • <%@ page language=”java” %> • <HTML> • <HEAD><TITLE>Simple JSP example></TITLE></HEAD> • <BODY> • <H1>This is a code within the JSP tags to display the server time</H1> • <%--This is the JSP content that displays the server time by using the Date class of the java.util package--%> • <% java.util.Date now=new java.util.Date(); %> • <H2><%= now.getHours() %>:<% =now.getMinutes() %>:<% =now.getSeconds() %></H2> • </BODY> • </HTML> J2EE Web Components

  12. Components of a JSP Page • The three components of a JSP page are: • JSP directives • JSP scripting • JSP actions J2EE Web Components

  13. Components of a JSP Page (Contd.) • A directive element in a JSP page provides global information about a particular JSP page and is of three types: • page Directive • taglib Directive • include Directive • The syntax for defining a directive is: • <%@ directive attribute=”value” %> J2EE Web Components

  14. Components of a JSP Page (Contd.) • The page Directive • Defines attributes that notify the Web container about the general settings of a JSP page. • The syntax of the page directive is: • <%@ page attribute_list %> J2EE Web Components

  15. Attribute Name Description language Defines the scripting language of the JSP page. extends Defines the extended parent class of the JSP generated servlet. import Imports the list of packages, classes, or interfaces into the generated servlet. session Specifies if the generated servlet can access the session or not. An implicit object, session, is generated if the value is set to true. The default value of session attribute is true. • Components of a JSP Page (Contd.) • The following table describes the attributes supported by the page directive: J2EE Web Components

  16. Attribute Name Description buffer Specifies the size of the out buffer. If size is set to none, no buffering is performed. The default value of buffer size is 8 KB. autoFlush Specifies that the out buffer be flushed automatically if the value is set to true. If the value is set to false, an exception is raised when the buffer is full. The default value of autoFlush attribute is true. isThreadSafe Specifies whether a JSP page is thread safe or not. • Components of a JSP Page (Contd.) • Attributes supported by the page directive are (Contd.): J2EE Web Components

  17. Attribute Name Description errorPage Specifies that any un-handled exception generated will be directed to the URL. isErrorPage Specifies that the current JSP page is an error page, if the attribute value is set to true. The default value of isErrorPage attribute is false. contentType Defines the Multipurpose Internal Mail Extension (MIME) type for a response. The default value of the contentType attribute is text/html. • Components of a JSP Page (Contd.) • Attributes supported by the page directive are (Contd.): J2EE Web Components

  18. Components of a JSP Page (Contd.) • The include Directive • Specifies the names of the files to be inserted during the compilation of the JSP page. • Creates the contents of the included files as part of the JSP page. • Inserts a part of the code that is common to multiple pages. • The syntax of the include directive is: • <%@ include file = ”URLname” %> J2EE Web Components

  19. Components of a JSP Page (Contd.) • The taglib Directive • Imports a custom tag into the current JSP page. • Associates itself with a URI to uniquely identify a custom tag. • Associates a tag prefix string that distinguishes a custom tag with the other tag library used in a JSP page. • The syntax to import a taglib directive in the JSP page is: • <%@ taglib uri=“tag_lib_URI” prefix=”prefix” %> J2EE Web Components

  20. Attribute Description uri Locates the TLD file of a custom tag. prefix Defines a prefix string to be used for distinguishing a custom tag instance. • Components of a JSP Page (Contd.) • The following table describes the attributes of the taglib directive: J2EE Web Components

  21. Components of a JSP Page (Contd.) • JSP Scripting Elements • Embed Java code directly into an HTML page. • Include various scripting elements, which are: • Declarations: Provide a mechanism to define variables and methods. Declarative statements are placed within <%! and %> symbols and always end with a semicolon. • Expressions: Insert values directly into the output. The syntax to include a JSP expressions in the JSP file is: • <%= expression%> • Scriptlets: Consists of valid Java code snippets that are enclosed within <% and %> symbols. The syntax to declare JSP scriptlets to include valid Java code is: • <% Java code %> J2EE Web Components

  22. Components of a JSP Page (Contd.) • JSP Implicit Objects are: • Pre-defined variables that can be included in JSP expressions and scriptlets. • Implemented from servlet classes and interfaces. J2EE Web Components

  23. Implicit Object Class Description application javax.servlet.ServletContext The application object defines a Web application. Usually, it is the application in the current Web context. config javax.Servlet.ServletConfig Represents the object of a ServletConfig class. exception java.lang.Throwable Represents the Throwable exception, in a JSP page. • Components of a JSP Page (Contd.) • The following table describes various JSP implicit objects: J2EE Web Components

  24. Implicit Object Class Description out javax.servlet.jsp.JspWriter Represents an object of JspWriter to send response to the client. JspWriter extends the PrintWriter class and is used by JSP pages to send client responses. page java.lang.Object Represents the current instance of the JSP page that in turn is used to refer to the current instance of the generated servlet. session javax.servlet.http.HttpSession Represents a session object of HttpSession interface. • Components of a JSP Page (Contd.) • Various JSP implicit objects are (Contd.): J2EE Web Components

  25. Implicit Object Class Description response javax.servlet.http.HttpServletResponse Represents a response object of HttpServletResponse that is used to send an HTML output to the client. request javax.servlet.http.HttpServletRequest Represents a request object of HttpServletRequest. It is used to retrieve data submitted along with a request. pageContext javax.servlet.jsp.PageContext Represents a page context for a JSP page. • Components of a JSP Page (Contd.) • Various JSP implicit objects are (Contd.): J2EE Web Components

  26. Components of a JSP Page (Contd.) • JSP Actions • Perform tasks, such as insertion of files, reusing beans, forwarding a user to another page, and instantiating objects. • The syntax to use a JSP action in a JSP page is: • <jsp:attribute> J2EE Web Components

  27. JSP Action Description Attribute Description of Attributes <jsp:useBean> Invokes and searches for an existing bean. id class scope beanName Uniquely identifies the instance of the bean. Identifies the class from which the bean objects are to be implemented. Defines the scope of the bean. Defines the referential name for the bean. • Components of a JSP Page (Contd.) • The following table describes various JSP action tags: J2EE Web Components

  28. JSP Action Description Attribute Description of Attributes <jsp:getProperty> Retrieves the property of a bean. name property Defines the name for the bean. Defines the property from which the values are to be retrieved. • Components of a JSP Page (Contd.) • Various JSP action tags are (Contd.): J2EE Web Components

  29. JSP Action Description Attribute Description of Attributes <jsp:setProperty> Used to set the property for a bean name property value param Specifies a name for the bean. Defines the property for which values are to be set. Defines an explicit value for the bean property. Defines the name of the request parameter to be used. • Components of a JSP Page (Contd.) • Various JSP action tags are (Contd.): J2EE Web Components

  30. JSP Action Description Attribute Description of Attributes <jsp:forward> Used to forward a request to a target page. page Specifies the URL of the target page. <jsp:include> Includes a file in the current JSP page. page flush Specifies the URL of the resource to be included. Specifies whether the buffer should be flushed or not. The flush value can be either true or false. • Components of a JSP Page (Contd.) • Various JSP action tags are (Contd.): J2EE Web Components

  31. JSP Action Description Attribute Description of Attributes <jsp:param> Defines a parameter to be passed to an included or forwarded page. name value Defines the name of the reference parameter. Defines the value of the specified parameter. <jsp:plugin> Executes a Java applets or a JavaBean. type code codebase Defines the type of plug-in to be included. Defines the name of the class to be executed by the plug-in. Defines the path of the code. • Components of a JSP Page (Contd.) • Various JSP action tags are (Contd.): J2EE Web Components

  32. Programming in JSP • Classes of JSP API • The ErrorData Class • The JSPWriter Class • The PageContext Class J2EE Web Components

  33. Programming in JSP (Contd.) • The ErrorData Class • Defines error information for error pages. • Sets the value of the page directive, isErrorPage to true, to indicate that a page is an error page. • Contains various methods, which are: • getRequestURL(): Returns the requested URL in the form of a string. • setServletName(): Returns the name of the servlet invoked in the form of a string. • getStatusCode(): Returns the status code of the error in the form of an integer. • getThrowable(): Returns the Throwable exception that caused the error. J2EE Web Components

  34. Programming in JSP (Contd.) • The JspWriter Class • Writes action and template data in a JSP page. • Refers the object of JspWriter class by the implicit variable, out. • Contains various methods, which are: • clear() • close() • flush() • getBufferSize() • print() • println() J2EE Web Components

  35. Programming in JSP (Contd.) • The PageContext Class • Provides context information when JSP is used in the servlet environment • Contains various methods, which are: • forward() • getPage() • getRequest() • getResponse() • getServletConfig() • getServletContext() • getSession() • include() J2EE Web Components

  36. Programming in JSP (Contd.) • Steps to Create a JSP Application • Create a user interface using HTML: Accepts inputs from the user. The user inputs are passed as a request to the JSP page. • Create a JSP page: Provides the business logic to process the request passed from the HTML interface. In addition, the JSP page sends the response back to the Web browser. • Package the user interface and JSP page: Packs the HTML and JSP page using the deploytool utility to create a JSP application. • Deploy the package you have created: Deploys the JSP application on the J2EE1.4 Application Server to access its services. • Access the application using a Web browser: Accesses the JSP application using a Web browser. J2EE Web Components

  37. Demonstration-Developing a JSP Application • Problem Statement • John Barrett, the Chief Technology Officer (CTO) has entrusted the development team with the task of creating an application that validates the customer id and password of every customer before they can access their account details. The customer id has to be in a numeric form. He also wants that an error message should be displayed to the customer, if the entered customer id or password is incorrect. Before the changes can be made to the entire application, John wants to test this functionality by making a sample application for a specific customer. Larry Williams, the programmer has been assigned the task of implementing this functionality. Larry decides to use JSP for developing this application and incase any error occurs, the details of the error should be displayed. J2EE Web Components

  38. Demonstration-Developing a JSP Application (Contd.) • Solution • To solve the given problem, perform the following tasks: • 1. Create a Login Page Using HTML. • 2. Create an Authentication Page Using JSP. • 3. Create an Error Page Using JSP. • 4. Package the JSP Application. • 5. Deploy the JSP Application. • 6. Access the JSP Application. J2EE Web Components

  39. Summary • In this lesson, you learned: • JSP technology has facilitated the segregation of the work profiles of a Web designer and a Web developer. • A JSP page, after compilation, generates a servlet and therefore incorporates all servlet functionalities. • The request-response cycle of JSP essentially comprises of two phases: • Translation phase • Request-processing phase • A JSP page consists of regular HTML tags representing the static content and the code enclosed within special tags representing the dynamic content. • Different elements that are used in JSP page are: • JSP directives • JSP actions • JSP scripting J2EE Web Components

  40. Summary (Contd.) • Objects in JSP can be created either implicitly by using directives, explicitly by using standard actions, or directly by declaring them within scriptlets. • JSP actions are used to perform tasks, such as insertion of files, reusing beans, forwarding a user to another page, and instantiating objects. • JSP page uses various JSP API classes, such as ErrorData, JspWriter, and PageContext that are defined in the javax.servlet.jsp package. • You need to follow the following steps to develop a JSP application: • Create a user interface. • Create a JSP page. • Package the user interface and JSP page into a Web application. • Deploy the Web application on the J2EE 1.4 Application Server. • Access the Web application in the Web browser. • JSP applications are developed using the JSP directives, JSP actions, and JSP scripting tags. J2EE Web Components

More Related