Java Server Pages (JSP)
11-08-2013
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
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
Server w/
JSP Container
GET /hello.jsp
<html>Hello!</html>
Hello.jsp
HelloServlet.java
HelloServlet.class
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!
cs242
$CATALINA_BASE
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
1. Build a directory tree separate from tomcat’s
webproject1
src classes etc
hello.java
web.xml
servlet
source
code
deployment
descriptor
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.
<?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>
2. Build this directory tree under your tomcat install
tomcat
webapps
helloWeb
WEB-INF
classes
web.xml
hello.class
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
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
Both NetBeans and Eclipse have tools to develop web apps
example: NetBeans
services/Servers: tomcat and glassfish
creating a web project
●HelloWorldWeb
●HelloNameWeb
Bean
JSP
Servlet Controller
View
Model
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
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)
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
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
//// 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;
}
}
<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" />
<!-- 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>
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 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 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>
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;
}
}
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