Presentation is loading. Please wait.

Presentation is loading. Please wait.

JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP.

Similar presentations


Presentation on theme: "JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP."— Presentation transcript:

1 JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP

2 Overview JavaServerPages are similar to HTML files, but provide the ability to display dynamic content with Web pages. A JavaServer Page (JSP) is an HTML web page that contains embedded Java code. HTML Java Code HTML sample.jsp The HTML displays the static portion of the page (text and graphics). The Java code is used to generate dynamic content for the page (JDBC, RMI). A JSP separates user interfaces from content generation. This allows the separation of web page design from component development.

3 Overview When a JSP is requested by a user, the web server executes the Java code to create the dynamic portion of the page. Web Browser Web Server request response | | | | | HTML Java Code HTML sample.jsp HTML

4 The date is sample.jsp Java code to create a Date object and include it in the page Overview

5 Web Browser Web Server | | | | | How is the JSP file processed? The date is sample.jsp request Client sends HttpRequest using URL of JSP The date is Tue Jun 06 16:03:37 EDT 2000 response JSP returns an HTML page

6 Web Browser JSP File Web Server JSP ParserJava Source Java Compiler JSP Servlet request Web Page (HTML) response How is the JSP file processed?

7 Elements of a JSP Expressions – code fragment who results are placed into the page Scriplets – blocks of Java code embedded in the page Declarations – page-wide variable and method declarations Directives – global information about the page JSPs are composed of standard HTML tags and JSP tags. The JSP tags are categorized as follows:

8 JSP -- Expressions Expressions are variables or constants that are inserted into the data returned by the web server. Syntax The expression is evaluated, converted to a string, and the result is inserted into the page Examples

9 JSP -- Expressions JSP defines several predefined variables which may be used in expressions and scriplets. Here are a few: request – the HttpServletRequest response – the HttpServletResponse session – the HttpSession associated with the session out – the PrintWriter used to send output to the client | | | | | ~~ ~~ ~~ ~~ ~~~ ~~~~ JSP request response out

10 JSP -- Scriplets Scriptlets are blocks of Java code embedded within the JSP page Syntax Example <% int i; for(i = 0; i < 10; i ++ ) out.println( i ); %>

11 JSP -- Scriplets Example Scriplets are often used to conditionally construct parts of the HTML page. <% String answer = request.getParameter("ans"); if( answer.equals(“texas“) ) { %> Your answer is correct! Sorry, your answer is incorrect. scriptlet2.jsp

12 JSP -- Scriplets Where is San Antonio located? question.htm This form will send the data in the textfield to scriptlet2.jsp.

13 <% String answer = request.getParameter("ans"); if( answer.equals(“texas“) ) { %> Your answer is correct! Sorry, your answer is incorrect. scriptlet2.jsp response Your answer is correct! request ans = texas

14 <% String answer = request.getParameter("ans"); if( answer.equals(“texas“) ) { %> Your answer is correct! Sorry, your answer is incorrect. scriptlet2.jsp response Sorry, your answer is incorrect. request ans = nevada

15 JSP -- Declarations A declaration block defines Java variables and methods for subsequent use in expressions or scriptlets. The declared variable or method may be used throughout the page. Syntax Example <%! int cout = 0; private void increment ( ) { count++; } %>

16 JSP -- Declarations Example Here is a JSP that calculates the factorial of a number retrieved from a form. <%! public int fact( int x ) { if( x == 1 ) return 1; else return x * fact( x - 1 ); } %> Factorial Calculator <% String numberString = request.getParameter("number"); int number = Integer.parseInt(numberString); out.println(number + " ! = " + fact(number)); %> Try another one factcalc.jsp declaration

17 This form will send the data in the textfield to factcal.jsp. JSP -- Declarations Factorial Calculator Enter a number : factorial.htm

18 <%! public int fact( int x ) { if( x == 1 ) return 1; else return x * fact( x - 1 ); } %> Factorial Calculator <% String numberString = request.getParameter("number"); int number = Integer.parseInt(numberString); out.println(number + " ! = " + fact(number)); %> Try another one response factcalc.jsp Factorial Calculator 4 ! = 24 Try another one request number = 4

19 JSP -- Declarations Example Here is a JSP that counts the number of times a page is accessed. This page has been accessed times. counter.jsp

20 JSP -- Directives A directive is a global definition that dictates how the code is a JSP file will be processed. Directives are used to set page-level instructions, insert data from external files, and specify custom tag libraries Directives are placed at the beginning of the JSP file???. Syntax: There are three types of directives : page, include, taglib

21 JSP -- Directives page directive The page directive defines page dependent attributes: -- Identifies the scripting language used in the JSP file -- Returns the defined page if an error occurs in the JSP <%@ page import = “java.util.*, java.text.*” -- Defines packages that that will be used by the embedded scripts Other page directives are: extends, session, buffer, autoFlush, isThreadSafe, info, isErrorPage, contentType

22 JSP -- Directives include directive The include directive allows text to be inserted into the page: -- Inserts the contents of copyright.html into the JSP

23 The date is sample2.jsp Scripting language is java Import the package java.util Insert the file copyright.htm Copyright 2000 Allen copyright.htm


Download ppt "JSP Fundamentals Elements of a JSP Using Beans with JSP Integrating Servlets and JSP."

Similar presentations


Ads by Google