SE 452 Lecture Notes


Week 6: Using Java Beans With JSP & Introduction to Ant



Admin Stuff [1/11]
Finish JSP Includes From Last Week [2/11]
A Little Background Regarding Reflection [3/11]
Reflection Sample Code [4/11]
JavaBeans [5/11]
Using Java Beans in JSP [6/11]
Set and Get Bean Properties [7/11]
Bean Sample Code [8/11]
Ant Build and Deploy Tool [9/11]
Intalling Ant [10/11]
build.xml [11/11]

Admin Stuff [1/11]




Finish JSP Includes From Last Week [2/11]



Intro JSP Slides
Printable Copy
This will conclued our use of the university notes, a bit too distracting to switch formats

A Little Background Regarding Reflection [3/11]



Reflection Sample Code [4/11]


import java.lang.reflect.*; import java.util.Date; class SampleMethod { public static void main(String[] args) { Date d = new Date(); showMethods(d); } static void showMethods(Object o) { Class c = o.getClass(); Method[] theMethods = c.getMethods(); for (int i = 0; i < theMethods.length; i++) { String methodString = theMethods[i].getName(); System.out.println("Name: " + methodString); String returnString = theMethods[i].getReturnType().getName(); System.out.println(" Return Type: " + returnString); Class[] parameterTypes = theMethods[i].getParameterTypes(); System.out.print(" Parameter Types:"); for (int k = 0; k < parameterTypes.length; k ++) { String parameterString = parameterTypes[k].getName(); System.out.print(" " + parameterString); } System.out.println(); } } } SampleMethod.java

JavaBeans [5/11]



Using Java Beans in JSP [6/11]



Set and Get Bean Properties [7/11]



Bean Sample Code [8/11]


package myBeans; public class SimpleBean { private String theString = "hello, world!"; public String getTheString() { return theString; } public void setTheString(String theString) { this.theString = theString; } }
SimpleBean.java

<html><title>Simple Bean JSP</title> <%@page import="myBeans.SimpleBean" %> <body> <jsp:useBean id="simpleBeanPageScope" class="myBeans.SimpleBean" /> <jsp:useBean id="simpleBeanApplicationScope" class="myBeans.SimpleBean" scope="application" /> <jsp:useBean id="simpleBeanSessionScope" class="myBeans.SimpleBean" scope="session" /> <h1>Simple Beans Initial Page</h1> <ul> <li>Initial value of simpleBeanPageScope: <b><jsp:getProperty name="simpleBeanPageScope" property="theString" /></b> <li>Using scriplets for simpleBeanPageScope: <b><%= simpleBeanPageScope.getTheString() %></b> <jsp:setProperty name="simpleBeanPageScope" property="theString" value="<%= \"page scope\" %>" /> <li>After set for simpleBeanPageScope:<b> <jsp:getProperty name="simpleBeanPageScope" property="theString" /></b> <% SimpleBean simpleBean1Copy = (SimpleBean)pageContext.getAttribute("simpleBeanPageScope"); %> <li>Copy from page context: <b><%= simpleBean1Copy.getTheString() %></b> <jsp:setProperty name="simpleBeanApplicationScope" property="theString" value="<%= \"application scope\" %>" /> <jsp:setProperty name="simpleBeanSessionScope" property="theString" value="<%= \"session scope\" %>" /> <li>After set for simpleBeanApplicationScope: <b> <jsp:getProperty name="simpleBeanApplicationScope" property="theString" /></b> <li>After set for simpleBeanSessionScope: <b> <jsp:getProperty name="simpleBeanSessionScope" property="theString" /></b> </uL> <form id="theForm" method="post" action="/jjungman/servlet/myBeans.ForwardServlet"> <input type="hidden" name="from" value="initial" /> <input type="submit" value="Go To Servlet"/> </form> </body></html>
SimpleBeanJSP.jsp

package myBeans; import java.io.IOException; import java.io.PrintWriter; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.RequestDispatcher; public class ForwardServlet extends HttpServlet { public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String address = null; if(request.getParameter("from").equals("initial")) { address = "/SimpleBeanJSP2.jsp"; } else { request.getSession().invalidate(); address = "/SimpleBeanJSP3.jsp"; } RequestDispatcher rd = getServletContext().getRequestDispatcher(address); rd.forward(request, response); } public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doGet(request, response); } }
ForwardServlet.java

<html><title>Simple Bean JSP 2</title> <%@page import="myBeans.SimpleBean" %> <body> <jsp:useBean id="simpleBeanPageScope" class="myBeans.SimpleBean" /> <jsp:useBean id="simpleBeanApplicationScope" class="myBeans.SimpleBean" scope="application" /> <jsp:useBean id="simpleBeanSessionScope" class="myBeans.SimpleBean" scope="session" /> <h1>Simple Beans Second Page</h1> <ul> <li>Value of simpleBeanPageScope: <b><jsp:getProperty name="simpleBeanPageScope" property="theString" /></b> <li>Value of simpleBeanApplicationScope: <b><jsp:getProperty name="simpleBeanApplicationScope" property="theString" /></b> <li>Value of simpleBeanSessionScope: <b><jsp:getProperty name="simpleBeanSessionScope" property="theString" /></b> </uL> <form id="theForm" method="post" action="/jjungman/servlet/myBeans.ForwardServlet"> <input type="hidden" name="from" value="second" /> <input type="submit" value="Go To Servlet"/> </form> </body></html>
SimpleBeanJSP2.jsp

<html><title>Simple Bean JSP 3</title> <%@page import="myBeans.SimpleBean" %> <body> <jsp:useBean id="simpleBeanPageScope" class="myBeans.SimpleBean" /> <jsp:useBean id="simpleBeanApplicationScope" class="myBeans.SimpleBean" scope="application" /> <jsp:useBean id="simpleBeanSessionScope" class="myBeans.SimpleBean" scope="session" /> <h1>Simple Beans Third Page</h1> <ul> <li>Value of simpleBeanPageScope: <b><jsp:getProperty name="simpleBeanPageScope" property="theString" /></b> <li>Value of simpleBeanApplicationScope: <b><jsp:getProperty name="simpleBeanApplicationScope" property="theString" /></b> <li>Value of simpleBeanSessionScope: <b><jsp:getProperty name="simpleBeanSessionScope" property="theString" /></b> </uL> </body></html>
SimpleBeanJSP3.jsp

code running

Ant Build and Deploy Tool [9/11]



Intalling Ant [10/11]


Assume we downloaded and extracted Ant into C:\jakarta-ant-1.5.1 on some Windows box

We would need to create an environment variable
ANT_HOME=C:\jakarta-ant-1.5.1

And then add Ant to our path variable
set PATH=%PATH%;%ANT_HOME%\bin

To verify ant is installed our shell should now recognize the command Ant
 
C:\anttest>Ant

Buildfile: build.xml does not exist!Build failed

C:\anttest>
  


build.xml [11/11]


 
<?xml version="1.0"?> <!-- Build file for our first application --> <project name="Ant test project" default="build" basedir="."> <property name="src" value="src"/> <property name="dst" value="build/src"/> <target name="init"> <mkdir dir="${dst}"/> </target> <target name="build" depends="init" > <javac srcdir="${src}" destdir="${dst}" debug="true" includes="**/*.java" /> </target> </project>

Let's do a very simple example

 
class test1{

public static void main (String args[]){

System.out.println("This is example 1");

}

}

  

  1. Download the code of this example and unzip it in c:\
  2. files will be unzipped in anttest directory
  3. Run the Ant utility by going to c:\anttest\example1 and issuing ant command.
  4. You will receive the following out put
 
C:\anttest\example1>ant

Buildfile: build.xml

build:

[javac] Compiling 1 source file to C:\anttest\example1\build\src

BUILD SUCCESSFUL

Total time: 4 seconds

C:\anttest\example1>