Presentation is loading. Please wait.

Presentation is loading. Please wait.

JSP Michelle Johnston, Firebird Services Ltd. JSP Pages HTML page can become a jsp just by changing the extension to jsp Allows java to be run within.

Similar presentations


Presentation on theme: "JSP Michelle Johnston, Firebird Services Ltd. JSP Pages HTML page can become a jsp just by changing the extension to jsp Allows java to be run within."— Presentation transcript:

1 JSP Michelle Johnston, Firebird Services Ltd

2 JSP Pages HTML page can become a jsp just by changing the extension to jsp Allows java to be run within HTML pages

3 Directives

4 Declaratives Declarations are embedded within <%! Date theDate = new Date(); Date getDate() { System.out.println( "In getDate() method" ); return theDate; } %> Hello! The time is now

5 Declaratives Here we are declaring a variable theDate And the method getDate() Both will now be available in our scriptlets Warning! Declarations/declaratives are only evaluated ONCE per page – reload and the date remains the same!

6 Request method Get request info

7 Changing Date value Hello! The time is now Refresh the above page and see the date change!

8 Exercise 1 Exercise: Write a JSP to output the values returned by System.getProperty for various system properties such as java.version, java.home, os.name, user.name, user.home, user.dir etc.

9 Scriptlets Get a parameter <% //java code String userName=null; userName=request.getParameter("userName"); %>

10 Scriptlets <% // This is a scriptlet. Notice that the "date" // variable we declare here is available in the // embedded expression later on. System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is now

11 Outputting Info Note that System.out.println writes to stdout (console) To write to the html page, we put the value in Another way would be to use out (an object 'given' to you that allows you to write to html)

12 Outputting Info <% // This scriptlet declares and initializes "date" System.out.println( "Evaluating date now" ); java.util.Date date = new java.util.Date(); %> Hello! The time is now <% // This scriptlet generates HTML output out.println( String.valueOf( date )); %>

13 Exercise 2 Exercise: Write a JSP to output the entire line, "Hello! The time is now..." but use a scriptlet for the complete string, including the HTML tags.

14 Using Tables <% for ( int i = 0; i < n; i++ ) { %> Number <% } %>

15 Exercise 3 Write a JSP to output all the values returned by System.getProperties with " " embedded after each property name and value. Do not output the " " using the "out" variable.

16 Directives Dont fully qualify classes, import them! <% System.out.println( "Evaluating date now" ); Date date = new Date(); %> Hello! The time is now

17 Importing To import more than one package, simply comma separate them..

18 Includes Include puts the full text of the included file embedded into the jsp file (can be html/jsp/anything) Going to include hello.jsp...

19 Tags < not <% body (no body to this tag – xml like) Predefined tags

20 Exercise 4 Change the jsp:include to jsp:forward and note the difference. Write a JSP to do either a forward or an include, depending upon a boolean variable (hint: The concepts of mixing HTML and scriptlets work with JSP tags also!)

21 Calling JSP in Forms What's your name?

22 Creating a session SaveName.jsp saves the name in a session: <% String name = request.getParameter( "username" ); session.setAttribute( "theName", name ); %> Continue

23 Retrieving Session Info NextPage.jsp shows how to retrieve the saved name. Hello,

24 Exercise 5 Add another attribute age to the whole of this example (all three pages)

25 Form Processing Add and age to the form in GetName.html What's your name? What's your address? What's your age?

26 Using Beans Create a bean (Java class) with username, and age as fields Create setter methods (setUsername, set and setAge) Create getter methods (getUsername, get and getAge)

27 UserData class public class UserData { String username; String ; int age; public void setUsername( String value) { username = value; } public void set ( String value ) { = value; }

28 UserData Class cont public void setAge( int value ) { age = value; } public String getUsername() { return username; } public String get () { return ; } public int getAge() { return age; } }

29 Bean Compilation Compile bean Put in the classpath of web server Now let us change "SaveName.jsp" to use a bean to collect the data. Continue

30 Retrieving Bean Data Let us modify NextPage.jsp to retrieve the data from bean.. You entered Name: Age:

31 Exercise 6 1) Write a JSP/HTML set that allows a user to enter the name of a system property, and then displays the value returned by System.getProperty for that property name (handle errors appripriately.) 2) Go back to the exercises where you manually modified boolean variables. Instead of a boolean variable, make these come from a HIDDEN form field that can be set to true or false.

32


Download ppt "JSP Michelle Johnston, Firebird Services Ltd. JSP Pages HTML page can become a jsp just by changing the extension to jsp Allows java to be run within."

Similar presentations


Ads by Google