jsp michelle johnston, firebird services ltd. jsp pages html page can become a jsp just by changing...

Post on 26-Mar-2015

219 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

JSPJSP

Michelle Johnston, Firebird Services Ltd

JSP PagesJSP Pages

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

Allows java to be run within HTML pages

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" %>

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>

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!

Request methodRequest method

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

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!

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.

ScriptletsScriptlets

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

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>

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)

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>

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.

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

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.

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>

ImportingImporting

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

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

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>

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>

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

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>

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>

Retrieving Session InfoRetrieving Session Info

NextPage.jsp shows how to retrieve the saved name.

<HTML> <BODY> Hello, <%=

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

Exercise 5Exercise 5

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

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>

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)

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;     }    

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; } }

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>

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>

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.

top related