+ All Categories
Home > Documents > JSP Michelle Johnston, Firebird Services Ltd. JSP Pages HTML page can become a jsp just by changing...

JSP Michelle Johnston, Firebird Services Ltd. JSP Pages HTML page can become a jsp just by changing...

Date post: 26-Mar-2015
Category:
Upload: christopher-donnelly
View: 218 times
Download: 1 times
Share this document with a friend
Popular Tags:
32
JSP JSP Michelle Johnston, Firebird Services Ltd
Transcript
Page 1: 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.

JSPJSP

Michelle Johnston, Firebird Services Ltd

Page 2: 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.

JSP PagesJSP Pages

HTML page can become a jsp just by changing the extension to jsp

Allows java to be run within HTML pages

Page 3: 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.

DirectivesDirectives

<%@page language="java" %> <%@ include file="/header.jsp" %> <%@ taglib uri="tlds/taglib.tld" prefix="mytag" %> <%@page language="java"

import="java.sql.*,mypackage.myclass" %> <%@page language="java" session="true" %> <%@page language="java" session="true"

errorPage="error.jsp" %> <%@page language="java" session="true"

contentType="text/html;charset=ISO-8859-1" %>

Page 4: 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.

DeclarativesDeclaratives Declarations are embedded within <!%= …%> <%@ page import="java.util.*" %> <HTML> <BODY> <%!     Date theDate = new Date();     Date getDate()     {         System.out.println( "In getDate() method" );         return theDate;     } %> Hello!  The time is now <%= getDate() %> </BODY> </HTML>

Page 5: 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.

DeclarativesDeclaratives

Here we are declaring a variable theDateAnd the method getDate()Both will now be available in our scriptletsWarning! Declarations/declaratives are

only evaluated ONCE per page – reload and the date remains the same!

Page 6: 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.

Request methodRequest method

Get request info <%=request.getMethod()%> <%=request.getRequestURI()%> <%=request.getProtocol()%> <%=request.getPathInfo()%> <%=request.getQueryString()%>

Page 7: 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.

Changing Date valueChanging Date value

<HTML> <BODY> Hello!  The time is now <%= new

java.util.Date() %> </BODY> </HTML>Refresh the above page and see

the date change!

Page 8: 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.

Exercise 1Exercise 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.

Page 9: 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.

ScriptletsScriptlets

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

Page 10: 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.

ScriptletsScriptlets

<HTML> <BODY> <% // 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 <%= date %> </BODY> </HTML>

Page 11: 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.

Outputting InfoOutputting 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)

Page 12: 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.

Outputting InfoOutputting Info <HTML> <BODY> <% // 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 )); %> </BODY> </HTML>

Page 13: 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.

Exercise 2Exercise 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.

Page 14: 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.

Using TablesUsing Tables <TABLE BORDER=2> <%     for ( int i = 0; i < n; i++ ) {   %>         <TR>         <TD>Number</TD>         <TD><%= i+1%></TD>   </TR>         <%     } %> </TABLE>

Page 15: 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.

Exercise 3Exercise 3

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

Page 16: 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.

DirectivesDirectives Don’t fully qualify classes, import them! <%@ page import="java.util.*" %> <HTML> <BODY> <%     System.out.println( "Evaluating date now" );     Date date = new Date(); %> Hello!  The time is now <%= date %> </BODY> </HTML>

Page 17: 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.

ImportingImporting

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

<%@ page import="java.util.*,java.text.*" %>

Page 18: 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.

IncludesIncludes

Include puts the full text of the included file embedded into the jsp file (can be html/jsp/anything)

<HTML> <BODY> Going to include hello.jsp... <BR> <%@ include file="hello.jsp" %>

</BODY> </HTML>

Page 19: 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.

TagsTags < not <% <some:tag> body </some:tag> <some:tag/> (no body to this tag – xml like) Predefined tags <jsp:.. <HTML> <BODY> Going to include hello.jsp... <BR> <jsp:include page="hello.jsp"/> </BODY> </HTML>

Page 20: 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.

Exercise 4Exercise 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!)  

Page 21: 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.

Calling JSP in FormsCalling JSP in Forms

<HTML> <BODY> <FORM METHOD=POST

ACTION="SaveName.jsp"> What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20> <P><INPUT TYPE=SUBMIT> </FORM> </BODY> </HTML>

Page 22: 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.

Creating a sessionCreating a session SaveName.jsp saves the name in a session: <%    String name =

request.getParameter( "username" );    session.setAttribute( "theName", name ); %> <HTML> <BODY> <A HREF="NextPage.jsp">Continue</A>

</BODY> </HTML>

Page 23: 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.

Retrieving Session InfoRetrieving Session Info

NextPage.jsp shows how to retrieve the saved name.

<HTML> <BODY> Hello, <%=

session.getAttribute( "theName" ) %> </BODY> </HTML>

Page 24: 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.

Exercise 5Exercise 5

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

Page 25: 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.

Form ProcessingForm Processing Add email and age to the form in GetName.html <HTML> <BODY> <FORM METHOD=POST ACTION="SaveName.jsp"> What's your name? <INPUT TYPE=TEXT NAME=username SIZE=20><BR> What's your e-mail address? <INPUT TYPE=TEXT NAME=email SIZE=20><BR> What's your age? <INPUT TYPE=TEXT NAME=age SIZE=4><P> <INPUT TYPE=SUBMIT> </FORM> </BODY> </HTML>

Page 26: 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.

Using BeansUsing Beans

Create a bean (Java class) with username, email and age as fields

Create setter methods (setUsername, setEmail and setAge)

Create getter methods (getUsername, getEmail and getAge)

Page 27: 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.

UserData classUserData class public class UserData {    String username;     String email;     int age;     public void setUsername( String value)     {         username = value;     }     public void setEmail( String value )     {         email = value;     }    

Page 28: 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.

UserData Class contUserData Class cont

public void setAge( int value )     {         age = value;     }     public String getUsername() { return username; }     public String getEmail() { return email; }     public int getAge() { return age; } }

Page 29: 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.

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

the data. <jsp:useBean id="user" class="UserData"

scope="session"/> <jsp:setProperty name="user" property="*"/>  <HTML> <BODY> <A HREF="NextPage.jsp">Continue</A>

</BODY> </HTML>

Page 30: 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.

Retrieving Bean DataRetrieving Bean Data Let us modify NextPage.jsp to retrieve the data from bean.. <jsp:useBean id="user" class="UserData"

scope="session"/>  <HTML> <BODY> You entered<BR> Name: <%= user.getUsername() %><BR> Email: <%= user.getEmail() %><BR> Age: <%= user.getAge() %><BR> </BODY> </HTML>

Page 31: 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.

Exercise 6Exercise 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.

Page 32: 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.

Recommended