java server pages (jsp) 11-08-2013 - ftp directory listing
TRANSCRIPT
![Page 1: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/1.jpg)
Java Server Pages (JSP)
11-08-2013
![Page 2: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/2.jpg)
JSP
JavaBeans
Read:
Java EE Tutorial on Servlets & JSP ●Section 17 Java Servlet Technology ●Tutorial on JSP
Java EE API
Exam#2 is scheduled for Tues., Nov. 19, 7:00 pm, Snell 213
review session: Monday, 11/11, 5:30 to 7:00 pm, ITL
![Page 3: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/3.jpg)
![Page 4: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/4.jpg)
Environment variables
CATALINA_HOME
path to the directory where tomcat is installed
(e.g. C:\apache-tomcat-7.0.23)
JAVA_HOME
path to the Java JDK
(e.g. C:\Program files\Java\jkd1.7.0_09)
JRE_HOME
path to the Java Runtime Environment
(e.g. C:\Program Files\Java\jre7)
Two of the subfolders in $CATALINA_HOME are:
bin – startup and shutdown
webapps – docs and examples
![Page 5: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/5.jpg)
Server w/
JSP Container
GET /hello.jsp
<html>Hello!</html>
Hello.jsp
HelloServlet.java
HelloServlet.class
![Page 6: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/6.jpg)
01: <html>
02: <head>
03: <title>HelloDate JSP</title>
04: </head>
05: <body>
06: <h1>HelloDate JSP</h1>
07: <p>The current time is:
08: <%= new java.util.Date() %>
09: </p>
10: </body>
11: </html>
Where you put this file is very important!
![Page 7: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/7.jpg)
cs242
$CATALINA_BASE
![Page 8: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/8.jpg)
1. Type the JSP file into a text editor
2. If you use Tomcat, you may want to create a subdirectory for the JSP file in $CATALINA_HOME\webapps
c:\apache-tomcat-7.0.23\webapps\cs242
4. Place the date.jsp file into that directory
5. Startup Tomcat
6. Point your browser to
localhost:8080/cs242/date.jsp
![Page 9: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/9.jpg)
1. Build a directory tree separate from tomcat’s
webproject1
src classes etc
hello.java
web.xml
servlet
source
code
deployment
descriptor
![Page 10: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/10.jpg)
Extensible Markup Language (XML) is a
cross-platform, extensible, text-based standard for representing data. Parties that exchange XML data can create their own tags to describe the data, set up schemas to specify which tags can be used in a particular kind of XML document, and use XML style sheets to manage the display and handling of the data.
For example, a web service can use XML and a schema to produce price lists, and companies that receive the price lists and schema can have their own style sheets to handle the data in a way that best suits their needs.
![Page 11: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/11.jpg)
<?xml version=“1.0” encoding=“ISO-8859-1” ?.
<web-app xmlns=http://java.sun.com/xml/ns/j2ee
xmlns:xsi=http://www.w3.org/2001/XMLSchema-instance
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd”
version=“2.4”>
<servlet>
<servlet-name>Hello Date Servlet</servlet-name>
<servlet-class>helloDate</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>Hello Date Servlet</servlet-name>
<url-pattern>/helloDate</url-pattern>
</servlet-mapping.
</web-app>
![Page 12: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/12.jpg)
2. Build this directory tree under your tomcat install
tomcat
webapps
helloWeb
WEB-INF
classes
web.xml
hello.class
![Page 13: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/13.jpg)
3. Type the following command from your webproject1 directory:
(note – this is one long line)
javac –classpath /your path/tomcat/common/lib/servlet-api.jar –d classes src/hello.java This creates hello.class in the webproject1/classes directory
![Page 14: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/14.jpg)
4. copy hello.class to WEB-INF/classes and
copy web.xml to WEB-INF
5. from the tomcat directory, start tomcat
% bin/startup.sh
6. launch your browser and type in
http://localhost:8080/helloWeb/helloDate
Note: the webapp is named helloWeb (matches folder name) and the servlet is named helloDate (matches name in the xml file
7. remember to shutdown tomcat
![Page 15: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/15.jpg)
Both NetBeans and Eclipse have tools to develop web apps
example: NetBeans
services/Servers: tomcat and glassfish
creating a web project
●HelloWorldWeb
●HelloNameWeb
![Page 16: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/16.jpg)
Bean
JSP
Servlet Controller
View
Model
![Page 17: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/17.jpg)
![Page 18: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/18.jpg)
Most professional web pages need input from two different
experts:
A programmer who understands how to compute the
results the page will display
A graphics designer who determines how to display the
results
It is best to keep the Java code & the HTML tags separate
Any nontrivial computation should be carried out in a
separate Java class
You connect one or more JavaBeans to a JSP page
![Page 19: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/19.jpg)
A JavaBean is a Java class with the following properties:
By convention the name of the bean ends in Bean
It must have a default constructor (i.e. no arguments)
A JavaBean exposes its “properties” through get and set
methods, which follow a naming convention
If the property name is propertyName and the type is Type accessor method: Type getPropertyName()
mutator method: void setpropertyName(Type newValue)
A boolean property uses a different convention boolean isPropertyName() void setPropertyName(boolean newValue)
![Page 20: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/20.jpg)
To use a bean in a JSP page, use the jsp:useBean directive
The following directive invokes the default constructor of
StudentBean, and makes an object with the name user
<jsp:useBean id ="user" class=“StudentBean"/>
To set a property in the bean, use the setProperty directive
<jsp:setProperty name="user" property=“major" value=“36"/>
To get a property in the bean, use the getProperty directive
<jsp:getProperty name="user" property="major"/>
This returns a string that becomes part of the HTML page
![Page 21: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/21.jpg)
int getCount()
void setCount(int c)
String getS()
void setS(String s)
int[] getFoo()
void setFoo(int[] f)
int count;
String s;
int[] foo;
MyBean
![Page 22: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/22.jpg)
//// MagicBean.java
//// A simple bean containing a "magic" string.
public class MagicBean {
private String magic;
public MagicBean(String str) {
magic = str;
}
public MagicBean() {
magic = “Julian Delphiki"; // default
}
public String getMagic() {
return magic;
}
public void setMagic(String magic) {
this.magic = magic;
}
}
![Page 23: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/23.jpg)
<jsp:useBean id="myBean" class="com.foo.MyBean“ scope="request"/>
<jsp:getProperty name="myBean“ property="lastChanged" />
<jsp:setProperty name="myBean“ property="lastChanged" value="<%= new Date()%>"/>
Example
<jsp:usebean id="bean" class="MagicBean" />
<jsp:getProperty name="bean" property="magic" />
![Page 24: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/24.jpg)
<!-- bean.jsp -->
<hr>
<h3>Bean JSP</h3>
<p>
Behold -- I bring forth the magic property from the
Magic Bean...
<!-- bring in the bean under the name "bean" -->
<jsp:usebean id="bean" class="MagicBean" />
<table border=1>
<tr>
<td bgcolor=blue>
<font size=+3>
<!-- the following effectively does
bean.getMagic() -->
<jsp:getProperty name="bean" property="magic" />
</font>
</td>
</tr>
</table>
![Page 25: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/25.jpg)
1. The JSP container reads the requested JSP page and
transforms it into an HTML page
2. Regular HTML tags are left unchanged
3. JSP tags ( <%= . . . %> ) are processed
4. Expressions enclosed in JSP tags are evaluated and converted
to a string using toString method
5. The string is inserted into the HTML page
6. The resulting document contains only HTML
7. The web server sends the document to the browser
![Page 26: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/26.jpg)
![Page 27: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/27.jpg)
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello Name JSP Page</title>
</head>
<body>
<h1>Entry Form</h1>
<form name="Name InputForm" action="response.jsp">
Enter your name:
<input type="text" name="name"/>
<input type="submit" value="OK" />
</form>
</body>
</html>
![Page 28: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/28.jpg)
<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Hello Name JSP Page 2</title>
</head>
<body>
<jsp:useBean id="mybean" scope="session"
class="org.mypackage.hello.NameHandler" />
<jsp:setProperty name="mybean" property="name" />
<h1>Hello, <jsp:getProperty name="mybean" property="name" />!
</h1>
</body>
</html>
![Page 29: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/29.jpg)
package org.mypackage.hello;
public class NameHandlerBean {
private String name;
public NameHandler() {
name = null;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
![Page 30: Java Server Pages (JSP) 11-08-2013 - FTP Directory Listing](https://reader030.vdocument.in/reader030/viewer/2022032816/6206150c8c2f7b1730046d69/html5/thumbnails/30.jpg)
It is a good idea to put the bean directive at the beginning of
the JSP file, before the HTML
Both index.jsp, response.jsp and
NameHandlerBean must be deployed to the proper
directories
index.jsp & response.jsp into $CATALINA\webapps\cs242
NameHandlerBean into $CATALINA\webapps\cs242\WEB-INF\classes