Intro To JSP - eBarster

thickbugSoftware and s/w Development

Oct 28, 2013 (3 years and 9 months ago)

73 views

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 HTML pages


Directives


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


Declaratives


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>

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!

Request method


Get request info


<%=request.getMethod()%>


<%=request.getRequestURI()%>


<%=request.getProtocol()%>


<%=request.getPathInfo()%>


<%=request.getQueryString()%>



Changing 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 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.


Scriptlets


Get a parameter


<%



//java code



String userName=null;



userName=request.getParameter("userName");


%>


Scriptlets


<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 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 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 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 Tables


<TABLE BORDER=2>


<%





for ( int i = 0; i < n; i++ )



{




%>





<TR>




<TD>Number</TD>





<TD><%= i+1%></TD>




</TR>





<%





}



%>



</TABLE>

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



Directives


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>

Importing


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


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



Includes


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>

Tags


< 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 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 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 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 Info


NextPage.jsp shows how to retrieve the
saved name.


<HTML>


<BODY>


Hello, <%= session.getAttribute(
"theName" ) %>


</BODY>


</HTML>

Exercise 5


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

Form 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 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 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 cont


public void setAge( int value )




{


age = value;




}




public String getUsername()


{ return username; }




public String getEmail()


{ return email; }




public int getAge()


{ return age; }


}


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.


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