jsp – java server pages

105
JSP – Java Server JSP – Java Server Pages Pages Representation and Management of Data on the Internet

Upload: ulla-berry

Post on 02-Jan-2016

19 views

Category:

Documents


0 download

DESCRIPTION

JSP – Java Server Pages. Representation and Management of Data on the Internet. Introduction. What is JSP Good For?. Servlets allow us to write dynamic Web pages Easy access to request, session and context data Easy manipulation of the response (cookies, etc.) And lots more... - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: JSP – Java Server Pages

JSP – Java Server PagesJSP – Java Server Pages

Representation and Management of

Data on the Internet

Page 2: JSP – Java Server Pages

IntroductionIntroduction

Page 3: JSP – Java Server Pages

What is JSP Good For?What is JSP Good For?

• Servlets allow us to write dynamic Web pages

- Easy access to request, session and context data

- Easy manipulation of the response (cookies, etc.)

- And lots more...

• It is not convenient to write and maintain long

static HTML using Servlets

out.println("<h1>Bla Bla</h1>" + "bla bla bla bla"

+ "lots more here...")

Page 4: JSP – Java Server Pages

JSP IdeaJSP Idea

• Use HTML for most of the page

• Write Servlet code directly in the HTML page,

marked with special tags

• The server automatically translates a JSP page to

a Servlet class and the latter is actually invoked

- In Tomcat 5.0 you can find the generated Servlet

code under $CATALINA_BASE/work/

Page 5: JSP – Java Server Pages

RelationshipsRelationships

• Servlets: HTML code is printed from Java code

• JSP: Java code is embedded in HTML code

Java

HTMLJava

HTML

Page 6: JSP – Java Server Pages

ExampleExample

<HTML>

<HEAD>

<TITLE>Hello World</TITLE>

</HEAD>

<BODY>

<H2><I><%= new java.util.Date() %></I></H2>

<H1>Hello World</H1>

</BODY>

</HTML>

Tomcat 5.0 Generated Servlet

Page 7: JSP – Java Server Pages
Page 8: JSP – Java Server Pages

JSP Limitations and AdvantagesJSP Limitations and Advantages

• JSP can only do what a Servlet can do

• Easier to write and maintain HTML

• Easier to separate HTML from code

• Can use a "reverse engineering technique":

create static HTML and then replace static data

with Java code

Page 9: JSP – Java Server Pages

Basic JSP ElementsBasic JSP Elements

Page 10: JSP – Java Server Pages

Elements in a JSP fileElements in a JSP file

• HTML code: <html-tag>content</html-tag>

• HTML comment: <!-- comment -->

• JSP Comment: <%-- comment --%>

• Expressions: <%= expression %>

• Scriptlets: <% code %>

• Declarations: <%! code %>

• Directives: <%@ directive attribute="value" %>

• Actions: discussed later

Page 11: JSP – Java Server Pages

JSP ExpressionsJSP Expressions

• A JSP expression is used to insert Java values directly into the output

• It has the form: <%= expression %> , where expression can be a Java object, a numerical expression, a method call that returns a value, etc...

• For example:

<%= new java.util.Date() %>

<%= "Hello"+" World" %>

<%= (int)(100*Math.random()) %>

Page 12: JSP – Java Server Pages

JSP ExpressionsJSP Expressions

• A JSP Expression is evaluated

• The result is converted to a string

• The string is inserted into the page

• This evaluation is performed at runtime (when

the page is requested), and thus has full access to

information about the request, the session, etc...

Page 13: JSP – Java Server Pages

Expression TranslationExpression Translation

<H1>A Random Number</H1><%= Math.random() %>

public void _jspService(HttpServletRequest request, HttpServletResponse response)

throws java.io.IOException, ServletException { ... response.setContentType("text/html"); ... out.write("<H1>A Random Number</H1>\r\n"); out.print( Math.random() );

out.write("\r\n"); ...

}

Page 14: JSP – Java Server Pages

Predefined Variables (Implicit Objects)Predefined Variables (Implicit Objects)

• The following predefined variables can be used:

- request: the HttpServletRequest

- response: the HttpServletResponse

- session: the HttpSession associated with the request

- out: the PrintWriter (a buffered version of type

JspWriter) used to fill the response content

- application: The ServletContext

• These variables and more will be discussed in details

Page 15: JSP – Java Server Pages

<HTML>

<HEAD> <TITLE>JSP Expressions</TITLE></HEAD>

<BODY>

<H2>JSP Expressions</H2>

<UL>

<LI>Current time: <%= new java.util.Date() %>

<LI>Your hostname: <%= request.getRemoteHost() %>

<LI>Your session ID: <%= session.getId() %>

<LI>The <CODE>testParam</CODE> form parameter:

<%= request.getParameter("testParam") %>

</UL>

</BODY>

</HTML>

Page 16: JSP – Java Server Pages
Page 17: JSP – Java Server Pages
Page 18: JSP – Java Server Pages

JSP ScripletsJSP Scriplets

• JSP scriptlets let you insert arbitrary code into the

Servlet service method ( _jspService )

• Scriptlets have the form: <% Java Code %>

• The code is inserted verbatim into the service method,

according to the location of the scriplet

• Scriptlets have access to the same automatically defined

variables as expressions

Page 19: JSP – Java Server Pages

<%= foo() %> <% bar(); %>

public void _jspService(HttpServletRequest request, HttpServletResponse response)throws ServletException, IOException {

...response.setContentType("text/html");...out.print(foo());bar();...

}

Scriptlet TranslationScriptlet Translation

Page 20: JSP – Java Server Pages

An Interesting ExampleAn Interesting Example

Scriptlets don't have to be complete code blocks:

<% if (Math.random() < 0.5) { %> You <B>won</B> the game! <% } else { %> You <B>lost</B> the game! <% } %>

if (Math.random() < 0.5) { out.write("You <B>won</B> the game!"); } else { out.write("You <B>lost</B> the game!"); }

Page 21: JSP – Java Server Pages

JSP DeclarationsJSP Declarations

• A JSP declaration lets you define methods or members that get inserted into the Servlet class (outside of all methods)

• It has the following form: <%! Java Code %>

• For example: <%! private int someField = 5; %>

<%! private void someMethod(...) {...} %>

• It is usually of better design to define methods in a separate Java class...

Page 22: JSP – Java Server Pages

Declaration ExampleDeclaration Example

• Print the number of times the current page has been

requested since the server booted (or the Servlet class

was changed and reloaded): <%! private int accessCount = 0; %>

<%! private synchronized int incAccess() {

return ++accessCount;

} %>

<H1>Accesses to page since server reboot:

<%= incAccess() %> </H1>

Page 23: JSP – Java Server Pages
Page 24: JSP – Java Server Pages
Page 25: JSP – Java Server Pages

public class serviceCount_jsp extends... implements... throws... {

private int accessCount = 0;

private synchronized int incAccess()

{ return ++accessCount;}

public void _jspService(HttpServletRequest request,

HttpServletResponse response)

throws ServletException, IOException {

...

...

out.write("<H1>Accesses to page since server reboot: ");

out.print(incAccess());

... } ... }

Page 26: JSP – Java Server Pages

jspInit jspInit andand jspDestroy jspDestroy

• In JSP pages, like regular Servlets, sometimes want to use init and destroy

• It is illegal to use JSP declarations to override init or destroy, since they (usually) are already implemented by the generated Servlet

• Instead, override the methods jspInit and jspDestroy- The generated servlet is guaranteed to call these methods from

init and destroy respectively

- The standard versions of jspInit and jspDestroy are empty (placeholders for you to override)

Page 27: JSP – Java Server Pages

JSP DirectivesJSP Directives

• A JSP directive affects the structure of the Servlet class

that is generated from the JSP page

• It usually has the following form:

<%@ directive attribute="value" %>

• Multiple attribute settings for a single directive can be

combined:

<%@ directive attribute1="value1" ...

attributeN="valueN" %>

• Two types discussed in this section: page and include

Page 28: JSP – Java Server Pages

pagepage-Directive Attributes-Directive Attributes

• import attribute: A comma separated list of

classes/packages to import

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

• contentType attribute: Sets the MIME-Type of the

resulting document (default is text/html)

<%@ page contentType="text/plain" %>

• Note that instead of using the contentType attribute, you

can write

<% response.setContentType("text/plain"); %>

Page 29: JSP – Java Server Pages

More More pagepage-Directive Attributes-Directive Attributes

• session="true|false" - use a session?

• buffer="sizekb|none"

- Specifies the content-buffer size (out)

• errorPage="url "

- Defines a JSP page that handles uncaught exceptions

- The page in url must have true in the page-directive:

• isErrorPage="true|false"

- The variable exception holds the exception thrown

by the calling JSP

Page 30: JSP – Java Server Pages

<HTML>

<HEAD> <TITLE>Reading From Database</TITLE></HEAD>

<BODY>

<%@ page import="java.sql.*" %>

<%@ page errorPage="errorPage.jsp" %>

<% Class.forName("oracle.jdbc.driver.OracleDriver");

Connection con =

DriverManager.getConnection("jdbc:oracle:thin:" +

"snoopy/snoopy@sol4:1521:stud");

Statement stmt = con.createStatement();

ResultSet rs = stmt.executeQuery("Select * from a");

ResultSetMetaData md = rs.getMetaData();

int col = md.getColumnCount();

%>

showTableA.jsp

Page 31: JSP – Java Server Pages

<TABLE border="2">

<% while (rs.next()) { %>

<TR>

<% for (int i = 1 ; i <= col ; i++) { %>

<TD><%= rs.getString(i) %></TD>

<% } %>

</TR>

<% } %>

</TABLE>

</BODY>

</HTML>

Page 32: JSP – Java Server Pages

<HTML>

<HEAD> <TITLE>Reading From Database</TITLE></HEAD>

<BODY>

<%@ page isErrorPage="true" %>

<h1>Oops. There was an error when you accessed the database. </h1>

<h2>Here is the stack trace: </h2>

<font color="red">

<pre>

<% exception.printStackTrace(new PrintWriter(out)); %>

</pre></font>

</BODY>

</HTML>

errorPage.jsp

Page 33: JSP – Java Server Pages

Table A exists!

Page 34: JSP – Java Server Pages

Table A does not exist!

Page 35: JSP – Java Server Pages

The The includeinclude Directive Directive

• This directive lets you include files at the time the

JSP page is translated into a Servlet

• The directive looks like this:

<%@ include file="url" %>

• JSP content can affect main page

• In Tomcat 5.x, generated Servlet is updated when

included files change (unlike old versions...)

Page 36: JSP – Java Server Pages

<HTML>

<HEAD> <TITLE>Including JSP</TITLE></HEAD>

<BODY>

Here is an interesting page.<br><br>

Bla, Bla, Bla, Bla. <br><br><br>

<%@ include file="AccessCount.jsp" %>

</BODY>

</HTML>

BlaBla.jsp

<hr>

Page Created for Dbi Course. Email us

<a href="mailto:[email protected]">here</a>.

<br>

<%! private int accessCount = 0; %>

Accesses to page since server reboot:

<%= ++accessCount %>

AccessCount.jsp

Page 37: JSP – Java Server Pages
Page 38: JSP – Java Server Pages

out.write("<HTML> \r\n");

out.write("<HEAD> <TITLE>Including JSP</TITLE></HEAD>\r\n");

out.write("<BODY>\r\n");

out.write("Here is an interesting page.<br><br>\r\n");

out.write("Bla, Bla, Bla, Bla. <br><br><br>\r\n");

out.write("\r\n");

out.write("<hr>\r\n");

out.write("Page Created for Dbi Course. Email us \r\n");

out.write("<a href=\"mailto:[email protected]\">here</a>.\r\n");

out.write("<br>\r\n");

out.write(" \r\n");

out.write("Accesses to page since server reboot: \r\n");

out.print( ++accessCount );

out.write("\r\n");

out.write("</BODY>\r\n");

out.write("</HTML> ");

Page 39: JSP – Java Server Pages

Writing JSP in XMLWriting JSP in XML(and vice versa)(and vice versa)

• We can replace the JSP tags with XML tags that

represent

- Expressions

- Scriptlets

- Declarations

- Directives

Page 40: JSP – Java Server Pages

<%= Expression %><jsp:expression>

Expression </jsp:expression>

<% Code %><jsp:scriptlet>

Code

</jsp:scriptlet>

<%! Declaration %><jsp:declaration>

Declaration </jsp:declaration>

<%@ Directive %> <jsp:directive.typeAttribute="value"/>

Page 41: JSP – Java Server Pages

<?xml version="1.0" encoding="UTF-8"?>

<jsp:root xmlns:jsp="http://java.sun.com/JSP/Page"

version="2.0">

<numbers>

<jsp:scriptlet> for(int i=1; i&lt;=10; ++i) { </jsp:scriptlet>

<number>

<jsp:expression> i </jsp:expression>

</number>

<jsp:scriptlet> } </jsp:scriptlet>

</numbers>

</jsp:root>

Page 42: JSP – Java Server Pages
Page 43: JSP – Java Server Pages

Variables in JSP Variables in JSP

Page 44: JSP – Java Server Pages

Implicit ObjectsImplicit Objects

• As seen before, some useful variables, like

request and session are predefined

• These variables are called implicit objects

• Implicit objects are defined in the scope of the

service method

- Can these be used in JSP declarations?

• Implicit objects are part of the JSP specifications

Page 45: JSP – Java Server Pages

The objectsThe objects request request and and responseresponse

• request and response are the HttpServletRequest

and HttpServletResponse arguments of the service

method

• Using these objects, you can:

• Read request parameters

• Set response headers

• etc. (everything we learned in Servlet lectures)

Page 46: JSP – Java Server Pages

The objectThe object out out

• This is the Writer used to add write output into

the response body

• This object implements the interface JspWriter,

which supports auto-flush

• Recall that you can adjust the buffer size, or turn

buffering off, through use of the buffer attribute

of the page directive

Page 47: JSP – Java Server Pages

The objectThe object page page

• Simply a synonym for (Object)this

• page is not very useful in JSP pages

• It was created as a placeholder for the time when

the scripting language could be something other

than Java

Page 48: JSP – Java Server Pages

The objectThe object pageContext pageContext

• pageContext is a new object introduced by JSP

• This object encapsulates use of server-specific

features (e.g. higher performance JspWriters)

• Access server-specific features through this class

rather than directly, so your code will conform to

JSP spec. and not be server dependent

• This object is also used to store page-scoped

Java Beans (discussed later)

Page 49: JSP – Java Server Pages

The objectThe object session session

• This is the HttpSession object associated with

the request

• If the session attribute in the page directive is

turned off (<%@ page session="false" %>)

then this object is not available

• Recall that a session is created by default

Page 50: JSP – Java Server Pages

The objectThe object config config

• This is the ServletConfig of the page, as received

in the init() method

• Remember: Contains Servlet specific

initialization parameters

• Later, we will study how initialization

parameters are passed to JSP pages in Tomcat

• You can get the ServletContext from config

Page 51: JSP – Java Server Pages

The objectThe object application application

• This is the ServletContext as obtained via

getServletConfig().getContext()

• Remember:

- The ServletContext is shared by all Web-application

Servlets (including ones generated from JSP)

- Getting and setting attributes is with getAttribute and

setAttribute of ServletContext

- You can use this object to get application-wide

initialization parameters

Page 52: JSP – Java Server Pages

Review: Variable ScopeReview: Variable Scope

a.jsp

b.jsp

client1

client2

service() local variables

Page 53: JSP – Java Server Pages

Review: Variable ScopeReview: Variable Scope

a.jsp

b.jsp

client1

client2

Servlet members

Page 54: JSP – Java Server Pages

Review: Variable ScopeReview: Variable Scope

a.jsp

b.jsp

client1

client2

session

Page 55: JSP – Java Server Pages

Review: Variable ScopeReview: Variable Scope

a.jsp

b.jsp

client1

client2

application

Page 56: JSP – Java Server Pages

Servlet Package and Helper ClassesServlet Package and Helper Classes

• The generated Servlet has a named package

• In Tomcat, this package is: org.apache.jsp

• In Java, you cannot use classes from the default

package (i.e. with no package declaration) from a

named package!

• Therefore, helper classes used by JSP pages must

have a named package

Page 57: JSP – Java Server Pages

JSP ActionsJSP Actions

Page 58: JSP – Java Server Pages

ActionsActions

• JSP actions use constructs in XML syntax to

control the behavior of the Servlet engine

• Using actions, you can

- dynamically insert a resource content

- forward the user to another page

- reuse Java Beans and custom tags - briefly

discussed later

Page 59: JSP – Java Server Pages

The The forwardforward Action Action

• jsp:forward - Forwards the requester to a new

resource <jsp:forward page="{relativeURL|<%= expression %>}">

<jsp:param name="parameterName"  

value="{parameterValue | <%= expression %>}" /> *

</jsp:forward>

• This action is translated to an invocation of the

RequestDispatcher

Page 60: JSP – Java Server Pages

The The includeinclude Action Action

• jsp:include - Include a resource content at run

time <jsp:include page="{relativeURL|<%= expression %>}"

flush="true| false" >    

<jsp:param name="parameterName"

value="{parameterValue | <%= expression %>}" />*

</jsp:include>

• This action is also translated to an invocation of

the RequestDispatcher

Page 61: JSP – Java Server Pages

includeinclude Directive vs. Action Directive vs. Action

• When a file is included using the include directive, the

file itself is included verbatim into the JSP code, prior to

the Servlet generation

• When a resource is included using the include action,

the generated Servlet uses the dispatcher to include its

content at runtime

• Question: using which of the latter options can the

included element change the HTTP headers or status?

Page 62: JSP – Java Server Pages

JSP Life CycleJSP Life Cycle

Page 63: JSP – Java Server Pages

JSP Life CycleJSP Life Cycle

The following table describes the life cycle of JSP

generated Servlet in details:

Page 64: JSP – Java Server Pages

JSP Life CycleJSP Life Cycle

Page first w

ritten

Request

#1

Request

#2

Server restarted

Request

#3

Request

#4

Page m

odified

Request

#5

Request

#6

JSP page

translated into

servlet

Yes No No No Yes No

Servlet compiled Yes No No No Yes No

Servlet

instantiated and

loaded into

server's memory

Yes No Yes No Yes No

init (or

equivalent) called

Yes No Yes No Yes No

doGet (or

equivalent) called

Yes Yes Yes Yes Yes Yes

Written by Marty Hall. Core Servlets & JSP book: www.coreservlets.com

Page 65: JSP – Java Server Pages

JSP TranslationJSP Translation

• When the JSP file is modified, JSP is translated into a

Servlet

- Application need not be reloaded when JSP file is modified

- In Tomcat 5.0, translation is done even if included files

(through the include directive) are modified

• Server does not generate the Servlet class after startup,

if the latter already exists

- Generated Servlet acts just like any other Servlet

Page 66: JSP – Java Server Pages

init()init() and and destroy()destroy()

• init() of the generated Servlet is called every time

the Servlet class is loaded into memory and

instantiated

• destroy() of the generated Servlet is called every

time the generated Servlet is removed

• The latter two happen even if the reason is

modification of the JSP file

Page 67: JSP – Java Server Pages

Thread SynchronizationThread Synchronization

• After the Servlet is generated, one instance of it

services requests in different threads, just like

any other Servlet

• In particular, the service method (_jspService)

may be executed by several concurrent threads

• Thus, like Servlets, JSP code should deal with

concurrency

Page 68: JSP – Java Server Pages

JSP in Tomcat 5.0JSP in Tomcat 5.0

Page 69: JSP – Java Server Pages

Tomcat 5.0 Generated ServletTomcat 5.0 Generated Servlet

<HTML>

<HEAD>

<TITLE>Hello World</TITLE>

</HEAD>

<BODY>

<H2><I><%= new java.util.Date() %></I></H2>

<H1>Hello World</H1>

</BODY>

</HTML>Tomcat 5.0 Generated Servlet

Page 70: JSP – Java Server Pages

Generated Servlet HierarchyGenerated Servlet Hierarchy(Tomcat 5.0 Implementation)(Tomcat 5.0 Implementation)

Apache

Implementation

Generated

Servlet

Sun

Specifications

GenericServlet

Servlet

JspPage

HttpJspPageHttpServlet

HttpJspBase

mypage_jsp

different packages

Page 71: JSP – Java Server Pages

Implementation vs. SpecificationImplementation vs. Specification

• JSP files should conform to JSP specifications

and be container independent

• For example, JSP files that assume extension of

HttpServlet will compile and run correctly under

Tomcat, but may fail to compile under other

containers

• The implicit object pageContext exists for this

reason

Page 72: JSP – Java Server Pages

JSP Initial ParametersJSP Initial Parameters

• Like Servlets, initial parameters can be passed to

JSP files using the <servlet> element of the

application configuration file web.xml

• Use the sub-element <jsp-file> instead of the

sub-element <servlet-class>

• A <servlet-mapping> is also needed

- Use the JSP file as the <url-pattern>

Page 73: JSP – Java Server Pages

<web-app>

<context-param>

<param-name>dbLogin</param-name>

<param-value>snoopy</param-value>

</context-param>

<context-param>

<param-name>dbPassword</param-name>

<param-value>snoopass</param-value>

</context-param>

Page 74: JSP – Java Server Pages

<servlet>

<servlet-name>ParamPage</servlet-name>

<jsp-file>/paramPage.jsp</jsp-file>

<init-param>

<param-name>tableName</param-name>

<param-value>users</param-value>

</init-param>

</servlet>

<servlet-mapping>

<servlet-name>ParamPage</servlet-name>

<url-pattern>/initParam.jsp</url-pattern>

</servlet-mapping>

</web-app>

Page 75: JSP – Java Server Pages

<HTML>

<HEAD><TITLE>JSP initial parameters</TITLE></HEAD>

<BODY>

<H1>Hello</H1>

<H2>I should use the table <I><%=

config.getInitParameter("tableName") %></I>.</H2>

<H2>To access the Database, I should use the login

<I><%=

application.getInitParameter("dbLogin") %></I> and

the password <I><%=

application.getInitParameter("dbPassword")

%></I>.</H2>

</BODY>

</HTML>

Page 76: JSP – Java Server Pages
Page 77: JSP – Java Server Pages

Appendix 1:Appendix 1:Java Beans in JSPJava Beans in JSP

Page 78: JSP – Java Server Pages

MotivationMotivation

• Software components (e.g. objects, data structures,

primitives) are extensively used in Web applications

• For example:

- Service local variables

- Attributes forwarded in requests

- Session attributes, like users information

- Application attributes, like access counters

Page 79: JSP – Java Server Pages

MotivationMotivation

• Standard actions are used to manipulate

components: declaration, reading from the

suitable context, setting of new values (according

to input parameters), storing inside the suitable

context, etc.

• Java Beans provide a specification for automatic

handling and manipulation of software

components in JSP (and other technologies...)

Page 80: JSP – Java Server Pages

Java Beans: The IdeaJava Beans: The Idea

• Wrap your data, in a standard fashion, inside a

Java class (Java Bean)

• Use special JSP actions to access and manipulate

the bean

• Use special action attributes to specify the

properties of the bean, like its scope

Page 81: JSP – Java Server Pages

Example 1: Access CounterExample 1: Access Counter

In the following example, we use a Bean to

maintain an access counter for requests to the

pages

Page 82: JSP – Java Server Pages
Page 83: JSP – Java Server Pages
Page 84: JSP – Java Server Pages

package dbi;

public class CounterBean {

private int counter;

public CounterBean() { counter = 0; }

public int getCounter() { return counter; }

public void setCounter(int i) { counter = i; }

public void increment() { ++counter; }

}

Bean must reside in a package

Bean is created by an empty constructor

getCounter and setCounter define the property counter

other methods can be used

Page 85: JSP – Java Server Pages

<jsp:useBean id="accessCounter"

class="dbi.CounterBean" scope="application"/>

<% accessCounter.increment(); %>

<H1>Welcome to Page A</H1>

<H2>Accesses to this application:

<jsp:getProperty name="accessCounter"

property="counter"/></H2>

<A HREF="pageB.jsp">Page B</a>

pageA.jsp

invokes getCounter()

Page 86: JSP – Java Server Pages

<jsp:useBean id="accessCounter"

class="dbi.CounterBean" scope="application"/>

<% accessCounter.increment(); %>

<H1>Welcome to Page B</H1>

<H2>Accesses to this application:

<jsp:getProperty name="accessCounter"

property="counter"/></H2>

<A HREF="pageA.jsp">Page A</a>

pageB.jsp

Page 87: JSP – Java Server Pages

Example 2: Session DataExample 2: Session Data

In the following example, we use a Bean in order

to keep a user's details throughout the session

Page 88: JSP – Java Server Pages
Page 89: JSP – Java Server Pages
Page 90: JSP – Java Server Pages
Page 91: JSP – Java Server Pages

package dbi;

public class UserInfoBean {

private String firstName;

private String lastName;

public UserInfoBean() { firstName = lastName = null;}

public String getFirstName() {return firstName;}

public String getLastName() { return lastName;}

public void setFirstName(String string) {firstName = string;}

public void setLastName(String string) {lastName = string;}

}

Page 92: JSP – Java Server Pages

<HTML>

<HEAD><TITLE>User Info</TITLE></HEAD>

<BODY><H1>Fill in your details:</H1>

<FORM ACTION="infoA.jsp">

Your First Name: <INPUT TYPE="text"

NAME="firstName"> <BR>

Your Last Name: <INPUT TYPE="text"

NAME="lastName"> <BR>

<INPUT TYPE="submit">

</FORM> </BODY>

</HTML>

infoForm.html

Page 93: JSP – Java Server Pages

<jsp:useBean id="userInfo"

class="dbi.UserInfoBean" scope="session"/>

<jsp:setProperty name="userInfo" property="*"/>

<H1> <jsp:getProperty name="userInfo"

property="firstName"/>

<jsp:getProperty name="userInfo"

property="lastName"/>, </H1>

<H1>Have a nice session!</H1>

<A HREF="infoB.jsp">User Info B</a>

infoA.jsp

Match parameters to corresponding properties

Page 94: JSP – Java Server Pages

<jsp:useBean id="userInfo"

class="dbi.UserInfoBean" scope="session"/>

<jsp:setProperty name="userInfo" property="*"/>

<H1> <jsp:getProperty name="userInfo"

property="firstName"/>

<jsp:getProperty name="userInfo"

property="lastName"/>, </H1>

<H1>Have a nice session!</H1>

<A HREF="infoA.jsp">User Info A</a>

infoB.jsp

Page 95: JSP – Java Server Pages

Advantages of Java BeansAdvantages of Java Beans

• Easy and standard management of data

- Automatic management of bean sharing and lots more

• Good programming style

- Allow standard but not direct access to members

- You can add code to the setters and getters (e.g. constraint

checks) without changing the client code

- You can change the internal representation of the data without

changing the client code

• Increase of separation between business logic (written

by programmers) and HTML (written by GUI artists)

Page 96: JSP – Java Server Pages

Appendix 2:Appendix 2:Custom JSP TagsCustom JSP Tags

Page 97: JSP – Java Server Pages

Custom JSP TagsCustom JSP Tags

• JSP code may use custom tags - tags that are

defined and implemented by the programmer

• The programmer defines how each of the custom

tags is translated into Java code

• There are two methods to define custom tags:

- Tag libraries - used in old versions of JSP

- Tag files - much simpler, introduced in JSP 2.0

Page 98: JSP – Java Server Pages

Tag LibrariesTag Libraries

• A tag library consists of:

- Tag handlers - Java classes that define how each of

the new tags is translated into Java code

- A TLD (Tag Library Descriptor) file, which is an

XML file that defines the structure and the

implementing class of each tag

Page 99: JSP – Java Server Pages

A Simple TagLib ExampleA Simple TagLib Example

package dbi;

import javax.servlet.jsp.JspException;

import javax.servlet.jsp.tagext.SimpleTagSupport;

import java.io.IOException;

public class DateTag extends SimpleTagSupport {

public void doTag() throws JspException, IOException {

getJspContext().getOut().print(new java.util.Date());

}

}

DateTag.java

Page 100: JSP – Java Server Pages

<?xml version="1.0" encoding="ISO-8859-1" ?>

<taglib version="2.0">

<tlib-version>1.0</tlib-version>

<tag> <name>date</name>

<tagclass>dbi.DateTag</tagclass>

<body-content>empty</body-content> </tag>

</taglib>

dbi-taglib.tld

<%@ taglib prefix="dbitag"

uri="/WEB-INF/tags/dbi-taglib.tld" %>

<html><body>

<h1>Hello. The time is: <dbitag:date/></h1>

</body></html>

taglibuse.jsp

Page 101: JSP – Java Server Pages
Page 102: JSP – Java Server Pages

Tag FilesTag Files

• The new version of JSP (2.0) provides an

extremely simplified way of defining tags

• The idea: for each custom tag, write a tag file

tagName.tag that implements the tag translation

using JSP code

• This way, the programmer can avoid creating tag

handlers and TLD files

Page 103: JSP – Java Server Pages

<%= new java.util.Date() %> date.tag

<%@ taglib prefix="dbitag" tagdir="/WEB-INF/tags/" %>

<html><body>

<h1>Hello. The time is: <dbitag:date/></h1>

</body></html>taguse.jsp

The Simplified ExampleThe Simplified Example

Page 104: JSP – Java Server Pages
Page 105: JSP – Java Server Pages

Other Capabilities of Custom TagsOther Capabilities of Custom Tags

• Attributes

- You can define the possible attributes of the Tags

- These can be accessed during the Tag translation

• Tag Body

- Tag translation may choose to ignore, include or

change the tag body