Java Server Pages

Download Report

Transcript Java Server Pages

Java Server Pages
• Allows the embedding of Java commands in a
page of HTML. Popular for UI heavy solutions.
• These commands are then interpreted by a JSP
engine in the webserver (like ASP/PHP).
• Therefore any webserver must have a JSP engine
installed and attached in order to process JSP
pages (IIS does not have a default engine installed
but one such as TomCat can be attached).
1
JSP
• JSP is a Server Side Include (SSI) mechanism.
• A JSP can contain standalone Java code, i.e. no
other elements are needed to interpret that page.
• A JSP can contain Java code which contacts other
elements, e.g. the Java code in your JSP page
could create objects, contact a database, etc.
2
JSP vs Servlet
• A servlet is a Java class which is instantiated and
executed when its URL is called.
• A JSP is a web page which has Java commands
embedded in it.
• Servlets are used for situations where a great deal
of functionality is required. Servlets can also
perform some operations and access resources
JSPs can’t.
• JSPs are used in situations where relatively simple
code is needed and a servlet would be viewed as
overkill.
3
Procedure
1. Name your page with the extension JSP.
2. Put in Java expressions which will be evaluated
into the tag format:
e.g. <%= expression %>. The result of the evaluated
expression will replace this tag in the final page.
(The result of the expression can also be discarded).
HTML comments can be used to comment the JSP
code.
<!-- comment -->
4
Objects in a JSP
• Can create, and use, an object of any available
Java class type in a JSP
• Example, assume the class ‘classname’ has been
compiled and the bytecode is visible to the page
containing the following tag
<jsp:useBean id="objectVariableName"
class="className" scope="session"/>
A session is a continuous communication session
between a client (e.g. Browser) and the server (e.g.
Tomcat/Glassfish)
5
Using an Object in a JSP
• To call a method in the object which will return no
result use the tag:
<% objectVariableName.methodName(parameter); %>
Note that the statement ends with a semi-colon.
• If the method returns a value which you would
like inserted into the web page then use the tag:
<%= objectVariableName.methodName(parameter) %>
Note there is no semi-colon in this statement.
6
Example
<jsp:useBean id="objectVariableName"
class="classUsedByJSP" scope="session"/>
<HTML>
<BODY>
<!-- Set the name in the object -->
<% objectVariableName.setName("Frank"); %>
<!-- get the name from the object -->
Name: <%= objectVariableName.getName() %><BR>
</BODY>
</HTML>
7
Example
public class classUsedByJSP
{
String name;
public String getName()
{ return name;
} // End returnName
public void setName(String nameSupplied)
{ name = nameSupplied;
} // End setName
} // End classUsedByJSP
8
Using JavaBeans in JSP
• A Java Bean is a Java class that’s written in
a particular way.
• One aspect of a Bean is that methods which
obtain the value of a variable and alter the
value of a variable are written using the
words ‘get’ and ‘set’ which is always
followed by the name of the variable which
has the first letter capitalised.
9
Example of a basic JavaBean
class beanEg
{
private int amount;
// Note the capital letter in the get and set methods
public void setAmount(int value)
{}
public int getAmount()
{}
} // End class beanEg
10
JSP Forms and Beans
• Can write a class using some of the
characteristics of a Bean.
• This will allow form component values to
be automatically placed in an object of this
class by the VM.
• Conversion to different data types is also
done automatically.
11
Procedure
• Write the HTML form and name the form
elements.
• Write the class definition and use same
names in the class for variables as was used
in the form. Methods which alter and access
the elements should be named get and set.
• Write the JSP file and tell VM to setup an
object of the class type
12
Procedure
• Insert an instruction in the JSP file to tell the
VM to call the methods in the object which
match the names of the form components
supplied.
• The VM automatically inserts values using
methods according to names and you are
now free to use the relevant object in your
JSP page.
13
Example – Basic JSP
<jsp:useBean id="dataObject"
class="dataClass" scope="session"/>
<!-- extract form components from URL and
setup object with the resulting values -->
<jsp:setProperty name="dataObject"
property="*"/>
14
Example – Class definition
public class dataClass
{
private String string;
private int value;
public void setString(String stringSupplied)
{ string = stringSupplied;} // End setString
public String getString()
{ return string; } // End getString
public void setValue(int valueSupplied)
{ value = valueSupplied; } // End setValue
public int getValue()
{ return value; } // End getValue
} // End dataClass
15
16
Cookies
• Can set and get cookie via JSP pages (and
servlets)
• Cookies not popular due to prejudice on
consumers part.
17
Cookies-setting
<%
Cookie authorised = new
Cookie("makeCookie", "true");
authorised.setMaxAge(60*60*24*365);
response.addCookie(authorised);
%>
18
Cookies-getting
<%
Cookie[] cookieList = request.getCookies();
for(int count=0;count<cookieList.length;count++)
out.println(cookieList[count].getName()+"<br>");
%>
19
Tag Libraries
• Custom JSP tags available if JSP is run on
specific web server
• Tag libraries generally offer useful features
which allow rapid development of JSP
pages, like simplified database access.
20
JSP Execution
• JSP pages are actually executed as servlets.
• When a JSP engine receives a JSP page it
creates a standard servlet from a template
and inserts the embedded code into this
servlet. It then executes the servlet and
replaces the JSP code with the results.
21