JavaServer Pages Fundamentals

tieplantlimabeansSoftware and s/w Development

Oct 28, 2013 (4 years and 15 days ago)

62 views

Prof. Dr.-Ing. Thomas Korte
Laboratory of Information Technology
JavaServer Pages
Fundamentals
1
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
2
Introduction to JSP

JavaServer
Pages
(JSP)
is
a
Java
technology
which

enables
the
development
of
dynamic
web
sites.

JSP was developed by Sun Microsystems to allow server side
development.

JSP files are HTML files with special Tags containing Java source
code that provide the dynamic content.
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Why Should I Use JSP?

JSP
is
easy
to
learn
and
allows
developers
to
quickly

produce
web
sites
and
applica@ons
in
an
open
and

standard
way.

main advantages of JSP:

multi-platform, i.e. JSPs can be moved to different platforms

uses Java and is therefor not locked to qspecific vendor

provides a separation between presentation (HTML) and
implementation (Java) code
3
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
JSP Compared to Other Technologies

JSP
compared
to
ASP
or
ASP.NET

Microsofts Active Server Pages

similar functionality

both allow embedded code in HTML pages, session variables and
database access

ASP is bound to Microsoft

ASP.NET supports application development using different languages
such as Visual Basic, C# and JavaScript

JSP
compared
to
Servlets

A Servlet is a Java class that provides special server side service.
It is hard work to write HTML code in Servlets.

In Servlets you need to have lots of println statements to generate
HTML.

JSP pages are converted to Servlets so actually can do the same
thing as old Java Servlets.
4
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
JSP Architecture

JSPs
are
built
on
top
of
the
servlet
technology.


JSPs are essential an HTML page with special JSP tags
embedded.

These JSP tags can contain Java code.

The JSP file extension is .jsp rather than .htm or .html.

The JSP engine parses the .jsp and creates a Java servlet source file. It
then compiles the source file into a class file, this is done
the first time

and this why the JSP is probably slower the first time it is accessed. Any
time after this
the special compiled servlet is executed
and is
therefore returns faster.

The figure on the next page depicts a JSP request from a web
browser.
5
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Requesting a JSP Page
3.
The Web server recognises the file
from its .jsp extension and passes it
to the JSP Servlet Engine.

If the JSP file has not been called
the first time, step 7 is performed.
4.
If the JSP file has been called the
first time, servlet source code is
generated from the JSP file.
5.
All the HTML text is converted to
println statements.
6.
The servlet source code is compiled
into a class.
7.
The Servlet is instantiated,calling the
init and service methods.
8.
HTML from the Servlet output is sent
via the Internet.
9.
HTML results are displayed on the
user's web browser.
6
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Setting up a JSP Environment

get
a
Java
SE
JDK

download
the
JSP
environement

http://java.sun.com/products/jsp/index.jsp

download
a
web
server
and
a
JSP
and
Servlet
engine

A much used option is to download the Tomcat server.

In Tomcat, a free open source Web server is combined with a
JSP and Servlet engine, developed by the Apache foundation.

http://tomcat.apache.org
»
alternatively download the NetBeans IDE, which
contains Web/applications servers and the JSP and
servlet libraries.
7
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Creating Your first JSP Page

Type
the
code
below
into
a
text
file
index.jsp
and
place
it

in
the
correct
directory
into
your
JSP
web
server

instala@on.

Then call it via your browser.
<html>
<head>
<title>My first JSP page
</title>
</head>
<body>
<%@ page language="java" %>
<% out.println("Hello World"); %>
</body>
</html>
8
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Using JSP Tags

There
are
five
main
tags:

Declaration tag

Expression tag

Directive tag

Scriptlet tag

Action tag

Declara@on
Tag

(
<%!

%>
)

This tag allows the developer to declare variables or methods.

Before the declaration you must have <%!

At the end of the declaration,the developer must have %>

Code placed in this tag must end in a semicolon ( ; ).

Declarations do not generate output so are used with JSP expressions or
scriptlets. For Example:
<%!
private int counter = 0 ;
private String get Account ( int accountNo) ;
%>
9
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Expression Tag

Expression
tag
(
<%=


%>)

This tag allows the developer to embed any Java expression and
is short for out.println().

A semicolon ( ; ) does not appear at the end of the code inside
the tag.

For example,to show the current date and time:

Date : <%= new java.util.Date() %>
10
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Directive Tag

direc@ve
tag
(
<%@
direc@ve
...
%>
)

A JSP directive gives special information about the page to the
JSP Engine.   There are three main types of directives:  
1)      page - processing information for this page.
2)      Include - files to be included.
3)      Tag library - tag library to be used in this page.  

Directives do not produce any visible output when the page is
requested but change the way the JSP Engine processes the
page.

For example,you can make session data unavailable to a page
by setting a page directive (session) to false.
11
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Sciptlet Tag

scriptlet
tag
(
<%
...
%>
)

Between <% and %> tags, any valid Java code is called a
Scriptlet.

This code can access any variable or bean declared.

For example, to print a variable: 
  

<% 
String username = "visualbuilder" ;
out.println ( username ) ;
%>
12
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Action Tag

There
are
three
main
roles
of
ac@on
tags:
1.

enable the use of server side
Javabeans
2.
transfer control between pages
3.
browser independent support for applets.

A Javabean is a special type of class that has a number of specially
named methods.

The JSP page can call these methods so almost all Java code in JSPs
can be hidden in Javabeans.
 

To
use
a
Javabean
in
a
JSP
page
use
the
following
syntax:


<jsp : usebean id = " ...." scope = "application" class = "com..." />

The following is a list of Javabean scopes:

page - valid until page completes.

request - bean instance lasts for the client request

session - bean lasts for the client session.

application - bean instance created and lasts until application ends. 
13
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Creating Your Second JSP Page

crea@ng
a
page
visit
counter
<
HTML
>
    
<HEAD>
        
<TITLE>Using the Application Object</TITLE>
    
</HEAD>
    
<
BODY
>
        
<H1>Using the Application Object</H1>
        
<%
        
Integer counter = (Integer)session.getAttribute(
"counter"
);
        
String heading = 
null
;
        
if 
(counter == 
null
) {
            
counter = 
new 
Integer(
1
);
        

else 
{
            
counter = 
new 
Integer(counter.intValue() + 
1
);
        
}
        
session.setAttribute(
"counter"
, counter);
        
Integer applicationCounter = (Integer)application.getAttribute(
"applicationCounter"
);
        
if 
(applicationCounter == 
null
) {
            
applicationCounter = 
new 
Integer(
1
);
        

else 
{
            
applicationCounter = 
new 
Integer(applicationCounter.intValue() + 
1
);
        
}
        
application.setAttribute(
"applicationCounter"
, applicationCounter);
        
%>
        
You have visited 
this 
page <%=counter%> times.
        
<BR>
        
This page has been visited by all users <%=applicationCounter%> times.
    
</
BODY
>
</
HTML
>
14
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Implicit Objects

The Servlet creates several implicit objects a JSP can access:
15
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
The Implicit Object out

out
object
example:
outexample.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"
http://www.w3.org/TR/html4/loose.dtd
">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
<%
for(int i=0;i<10;i++){
for(int j=0;j<=i;j++){
out.print("*");
}
out.println("<br>");
}
%>
</body>
</html>
16
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
The Implicit Objects request, response

request
object
The request object is created for each request and once the response is
submitted then it gets destroyed. The request object is used to pass the
information from one page to the other page if the pages are getting invoked
during the request lifecycle.

response
object
The response object is used to send and set the information at the client side.
The cookies are the small files used to save the information in the client machine
which can be used for some further actions and state maintenance.
17
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
The Implicit Object session

session
object
example
The session object is an instance of
HttpSession
and is used to track information
about a particular client.
Each client has one global session object.
When a user sends the fist request to the JSP engine, it will issue a session id to
track the information for the transaction.
The below example will show the session id as well as the count which shows how
many times the page is accessed during the current session.
sessionexample.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd
">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
</head>
<body>
Sesion id for the Session is :- <%=session.getId() %><br>
<%
if (session.getAttribute("count") != null){
int count = Integer.parseInt((String)session.getAttribute("count"));
count++;
out.println("The number of times the page accessed " + count );
session.setAttribute("count",String.valueOf(count));
}else{
session.setAttribute("count","1");
out.println("The page is accessed for the first time");
}
%>
</body></html>
18
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
The Implicit Object application

applica@on
object

Unlike session object which are created separately for each client browser, one
application object is created for whole application.

This object remains in the memory once the application is initialized.

It is destroyed when the application will be removed from the server or the
server shuts down.

This is a static object which is shared by all the clients.

The application object is normally used to cache data for better performance.

The page counter example from a previous page uses the application object.
19
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Working with HTML forms

The
basic
form
control
aQributes:
20
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
A HTML Form Example

username/password
request
form
(login.html)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd
">
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Login Form</title>
</head>
<body>
<p align="left"><b>Login Form
</b></p>
<form name="loginForm" >
<table border="1" width="50%" cellspacing="1" cellpadding="0" id="table1">
<tr>
<td width="50%">&nbsp;Login Name</td>
<td><input type="text" name="loginName" size="20"></td>
</tr>
<tr>
<td width="50%">Password</td>
<td><input type="password" name="password" size="20"></td>
</tr>
</table>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>
21
no
ac@on
is
associated

with
this
form
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
How to Get Data From a Form

How
do
we
get
the
form
data
into
a
JSP‐Page?

We need to first define which JSP page should be accessed
when this form is submitted.

This can be achieved by the action attribute in the <form>.

For example:
<form action="login.jsp">
will generate a request for
the
login.jsp
page when the submit button will be clicked for the form.

We then need to create the file
login.jsp
to show the data which
the user has sent from the login.html page from the previous
example.

Example login.jsp file: (next page)
22
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
A HTML Form Example With JSP-Processing

username/password
request
form
(Login.html)
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd
">
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Login Form</title>
</head>
<body>
<p align="left"><b>Login Form
</b></p>
<form name="loginForm" method =
"
POST
"
action =
"
login.jsp
"
>
<table border="1" width="50%" cellspacing="1" cellpadding="0" id="table1">
<tr>
<td width="50%">&nbsp;Login Name</td>
<td><input type="text" name="loginName" size="20"></td>
</tr>
<tr>
<td width="50%">Password</td>
<td><input type="password" name="password" size="20"></td>
</tr>
</table>
<p><input type="submit" value="Submit" name="B1"><input type="reset" value="Reset" name="B2"></p>
</form>
</body>
</html>
23
now
the
ac@on
is
to

call
up
login.jsp
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
The login.jsp Page to Process The Form
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/
TR/html4/loose.dtd
">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Login Form Data</title>
</head>
<body>
<b>Submitted Values are </b>
<table border="1" width="50%" cellspacing="1" cellpadding="0" id="table1">
<tr>
<td width="50%">&nbsp;Login Name</td>
<td><%=request.getParameter("loginName") %></td>
</tr>
<tr>
<td width="50%">Password</td>
<td><%=request.getParameter("password") %></td>
</tr>
</table>
</body>
</html>
24
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
HTML Form Validation

form
valida@on

Form validation is the process of checking that a form has been filled in
correctly before it is processed.

There are mainly two types of form validations: server-side (using JSP ASP,
etc), and client-side (usually done using JavaScript). The difference is:

Client side validation runs in the browser to check that the form values
are of the correct type. It should be done using JavaScript (VBScript is
supported only by IE).

Server side validation checks the code that is submitted to make sure it
is correct.

The following example will show how to use JavaScript to validate the login
form:
25
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
HTMl form validation using JavaScript
26
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01
Transitional//EN" "http://www.w3.org/TR/html4/
loose.dtd">
<html><head><meta http-equiv="Content-
Language" content="en-us">
<meta http-equiv="Content-Type" content="text/
html; charset=windows-1252">
<title>Registration Form</title>
</head><script>
function validateForm(form){
if(form.firstName.value==''){
alert ("First Name can not be blank");
return false;
}
if(form.lastName.value==''){
alert ("Last Name can not be blank");
return false;
}
if(form.introduction.value==''){
alert ("Introduce Yourself can not be
blank");
return false;
}
if(form.phone.value==''){
alert ("Phone can not be blank");
return false;
}
if(form.address.value==''){
alert ("Address can not be blank");
return false;
}
if(form.city.value==''){
alert ("City can not be blank");
return false;
}
if(form.state.value==''){
alert ("State can not be blank");
return false;
}
}
</script>
<body>
<p align="left"><b>Registration Form</b></p>
<form name="registerationForm" method="POST"
action="processRegistration.jsp" onsubmit="return
validateForm(document.registerationForm); ">
<table border="1" width="70%" cellspacing="1"
cellpadding="0" id="table1">
<tr><td width="50%">First Name</td>
<td><input type="text" name="firstName"
size="20"></td>
</tr><tr>
<td width="50%">Last Name</td>
<td><input type="text" name="lastName"
size="20"></td>
</tr><tr>
<td width="50%">Introduce Yourself</td>
<td><textarea name="introduction" cols="40"
rows="10"></textarea></td>
</tr><tr>
<td width="50%">Gender</td>
<td><input type="radio" value="V1" checked
name="radio1">male
<input type="radio" name="radio1"
value="V2">female</td>
</tr><tr>
<td width="50%">Phone</td>
<td><input type="text" name="phone" size="20"></
td></tr><tr>
...
<td width="50%">State</td>
<td><input type="text" name="state" size="20"></td>
</tr><tr>
<td width="50%">Country</td>
<td><select size="1" name="country">
<option selected value="United Kingdom">United
Kingdom</option>
<option>USA</option>
<option>Other</option>
</select></td>
</tr></table>
<p><input type="submit" value="Submit"
name="B1"><input type="reset" value="Reset"
name="B2"></p>
</form></body></html>
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Layout of The Form
27
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
The Same Validation using a JSP page…

validateRegistra@onWithJSP.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd
">
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Registration Form</title>
</head>
<body>
<p align="left"><b>Registration Form</b></p>
<form name="registerationForm" method="POST"
action="validateRegistrationWithJSP.jsp"
>
<table border="1" width="70%" cellspacing="1" cellpadding="0" id="table1">
<%

String errors=(String)request.getAttribute("errors");
if(errors != null && errors.trim().length()>0){

out.println("<tr><td colspan='2'>The following are the errors with given data<br>"+errors+"</td></tr>");
}
%>
<tr>
<td width="50%">First Name</td>
<td><input type="text" name="loginName" size="20" value="<%=request.getParameter("loginName")!=null?request.getParameter("loginName"):""%>"></
td></tr>
<tr>
<td width="50%">Last Name</td>
<td><input type="text" name="password" size="20" value="<%=request.getParameter("password")!=null?request.getParameter("password"):""%>"></
td></tr>
<tr>
<td width="50%">Introduce Yourself</td>
<td><textarea name="intro" cols="40" rows="10"><%=request.getParameter("intro")!=null?request.getParameter("intro"):""%></textarea></td>
</tr>
<tr>
<td width="50%">Gender</td>
<td>…
28
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Bildschirmausgabe nach Fehlern
29
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Die Seite validateRegistrationWithJSP.jsp
<%@ page language="java" contentType="text/html;
charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//
EN" "
http://www.w3.org/TR/html4/loose.dtd
">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html;
charset=ISO-8859-1">
</head>
<body>
<%
String loginName=request.getParameter("loginName");
String password=request.getParameter("password");
String intro=request.getParameter("intro");
String gender=request.getParameter("R1");
String phone=request.getParameter("phone");
String address=request.getParameter("address");
String city=request.getParameter("city");
String state=request.getParameter("state");
String country=request.getParameter("country");
String errors="";
if(loginName ==null || loginName.trim().length() ==0){
errors=errors+"<li>Please enter the Login Name<br>";
}
if(password ==null || password.trim().length() ==0){
errors=errors+"<li>Please enter the Password<br>";
}
if(intro ==null || intro.trim().length() ==0){
errors=errors+"<li>Please enter the introduction<br>";
}
if(gender ==null || gender.trim().length() ==0){
errors=errors+"<li>Please select Gender<br>";
}
if(phone ==null || phone.trim().length() ==0){
errors=errors+"<li>Please enter phone<br>";
30
}
if(address ==null || address.trim().length() ==0){
errors=errors+"<li>Please enter address<br>";
}
if(city ==null || city.trim().length() ==0){
errors=errors+"<li>Please enter city<br>";
}
if(state ==null || state.trim().length() ==0){
errors=errors+"<li>Please enter state<br>";
}
if(country ==null || country.trim().length() ==0){
errors=errors+"<li>Please enter country<br>";
}
if( errors.trim().length() >0){
request.setAttribute("errors",errors);
%>

<jsp:forward page="registerwithvalidation.jsp"></
jsp:forward>
<%
}
%>
<h1>The given data is Valid.</h1>
</body>
</html>
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Concentrating JSP Java-Code into Beans

For
beQer
readability
of
JSP‐pages,
the
embedded
Java

code
may
be
moved
into
the
Java
Beans

Java Beans are normal Java classes, which use a naming
convention to allow for easy introspection and reflection.

JSP use special tags to set and get the data from a Java Beans.

The same example using a Bean:

validateRegistra@onWithBean.jsp
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://www.w3.org/TR/html4/loose.dtd
">
<html>
<head>
<meta http-equiv="Content-Language" content="en-us">
<meta http-equiv="Content-Type" content="text/html; charset=windows-1252">
<title>Registration Form</title>
</head>
<body>
<p align="left"><b>Registration Form</b></p>
<form name="registerationForm" method="POST"
action="validateRegistrationWithBean.jsp"
>
<table border="1" width="70%" cellspacing="1" cellpadding="0" id="table1">
<%

String errors=(String)request.getAttribute("errors");
if(errors != null && errors.trim().length()>0){

out.println("<tr><td colspan='2'>The following are the errors with given data<br>"+errors+"</
td></tr>");
}...
31
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Die Seite validateRegistrationWithBean.jsp
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "
http://
www.w3.org/TR/html4/loose.dtd
">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
</head>
<body>
<jsp:useBean id="bean" class="com.visualbuilder.beans.RegistrationBean"/>
<jsp:setProperty name="bean" property="*"/>
<%
com.visualbuilder.beans.RegistrationBean
register
=(com.visualbuilder.beans.RegistrationBean)pageContext.getAttribute("bean");
register.validate(request);
String errors= (String)request.getAttribute("errors");
if( errors.trim().length() >0){
%>
<jsp:forward page="registerwithbean.jsp"></jsp:forward>
<%
}
%>
<h1>The given data is Valid.</h1>
</body>
</html>
32
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Die Java Bean RegistrationBean.java
33

package com.visualbuilder.beans;
import javax.servlet.http.HttpServletRequest;
public class RegistrationBean {
String loginName, password, intro, gender, phone, address, city, state, country;
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassword() {
...
public void validate(HttpServletRequest request){
String errors="";
if(loginName ==null || loginName.trim().length() ==0){
errors=errors+"<li>Please enter the Login Name<br>";
}
if(password ==null || password.trim().length() ==0){
...
}
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Further Reading

jGuru:
Introduc.on
to
JavaServer
Pages
technology

http://java.sun.com/developer/onlineTraining/JSPIntro/index.html

Servlet
and
JavaServer
Pages
technology
free
online
books

http://books.coreservlets.com
/
especially read Chapter 3 of this book

A Fast Introduction to Basic JSP Programming

http://pdf.moreservlets.com/More-Servlets-and-JSP-Chapter-03.pdf
34
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
What is a Web Application?

A
client/server
applica@on
using
a
collec@on
of
web

components
on
the
server,
which
are:

servlets,

JavaServer Pages,

tag libraries,

Web services,

JavaBeans.

examples:

electronic shopping mall

auction site
35
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Request and Response in a Web Application

browser
sends

requests
to
web

server

web
server
passes

request
to
a
web

applica@on

web
applica@on
runs

inside
a
servlet

container

web
applica@on

generates
a
response
36
20
Building Web Components

June 2003
FIGURE
1-1
Requests
and
Responses
in
a
W
eb
Application
W
e
b
applications
consist
of
a
varied
s
et
of
interr
elated
web
components
.
This
set
includes
JSP
pages,
s
ervlets,
and
tag
libraries
t
hat
work
t
ogether
.
Resour
ces
u
tilized
within
the
web
application
must
b
e
coor
dinated
using
a
deployment
descriptor
f
ile.
This
f
ile
contains
the
meta-information
t
hat
describes
the
web
application.
The
servlet
container
translates
J
SP
f
iles
i
nto
s
ervlets
i
n
o
r
der
to
r
u
n
t
hem
in
the
web
server
.
Chapter
2
p
r
ovides
mor
e
detailed
e
xplanations
of
the
components
i
n
t
his
illustration.
Challenges
in
Developing
W
e
b
Applications
T
w
o
key
characteristics
distinguish
web
applications
fr
om
standalone
applications:
!
W
e
b
c
omponents
d
o
not
interact
dir
e
ctly
with
one
another
.
The
servlet
container
and
the
client
br
owser
mediate
t
he
links
between
components.
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Web Application Characteristics

In
contrast
to
standalone
applica@ons,
web
applica@on

components
do
not
interact
directly,
but
via
a
servlet

container
and
a
web
browser.

servlet container: web server extension to excute Java code of
web components

Data
representa@on,
flow,
and
processing
are
distributed

among


the client browser,

the servlet container,

and individual web components

Informa@on
storage
and
database
access
take
place
in

the
server.

The application resides on the server.
37
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Web Application Development is Different…

Web
components
communicate
with
one
another

through
the
servlet
container.

Data
is
always
passed
as
strings

An
applica@on
consists
of
coopera@ng
processes.

Tes@ng
must
take
place
on
mul@ple
browsers
and

servers.

Debugging
is
difficult!

The
deployment
of
a
Web
applica@on
is
typically
a
@me

consuming
task.
38
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
How IDEs can Help

IDES
help
with:

editors for JSP Pages, servlets, tag libraries

syntax coloring, code checking, code completion, compilation,
UML diagrams

wizards for the generation of web service components

generation of the deployment descriptor

deployment descriptor:

xml-file that describes the components of a web application

deployment with a web module structure

execution support

source level debugging of JSPs, servlets and helper classes

http requests and responses can be captured
39
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
The Structure of Web Applications

The
server
side
elements
of
a
web
applica@on
are
called

a
web
module.

A web module contains the web components.

A web module runs inside a servlet container.

A servlet container is contained within a web server.

The contents of a web module:

presentation elements

define views = pages the users see and interact with
»
HTML files and/or JSP pages

controller element

controls page flow, filters in/output
»
a servlet

model

the application logic and resources
40
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Web Server

A
Web
server
provides
mechanisms
for
clients
to
access

resources
on
a
server.

The mechanisms include

support for http and other protocols

execution of server side programms

support for servlet container

Servlet
Container

offer services for the web components of a web module

life-cycle management

network services for requests and responses

decoding of requests and formatting of responses

interpretation and processing of JSP pages into servlets
41
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Web Modules

A
Web
module
is
a
deployable
unit
consis@ng
of
web

components
and
sta@c
content
files
(e.g.
images,
HTML

files).

It resides in a root directory.

It contains a standard layout of subfolders.

It contains a deployment descriptor:

web.xml
file
42
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
JavaServer Faces

A
Server‐Side
User
Interface
Component
Framework

based on the JSP technology
further reading:

http://www.coreservlets.com/JSF-Tutorial/
43
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Web Applications With a Browser Interface

JSF
is
a
liQle
more
elaborate
than
JSP.

The user interface runs on the server.

It is using a set of UI components a browser is able to render.

Special: Java UI components are mapped to special HTML tags.
44
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
JSF Architecture

JavaServer
Faces
technology
components:

API representing UI components:

state management,

event handling,

validation,

data conversion,

page navigation

two JavaServerPages custom tag libraries

map components to server-side java objects
45
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
Benefits

The
separa@on
of
programm
logic
(Java
code)
from

presenta@on
(HTML
code).

Page
designers
and
java
programmers
can
focus
on
their

piece
of
the
development
process.

There
is
a
component
specific
event
handling.

UI
elements
are
stateful
objects
on
the
server.

Model‐View‐Controller
Architecture
46
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
A JavaServer Faces Application…

runs
in
a
servlet
container
and
contains:

JavaBeans components containing application-specific
functionality and data

Event Listeners

HTML pages written as JSP pages

server-side helper classes, such as DB access beans

has:

a custom HTML tag library for rendering UI components on a
page

a custom tag library for representing event handlers, validators,
and other actions

UI components represented as stateful objects on the server

an application configuration resource file
47
Montag, 4. Mai 2009
04/18/09 - Prof. Dr.-Ing. Thomas Korte
MVC Pattern

The
model
contains
applica@on
data.

The
view
is
a
JavaServer
page.

The
controller
is
a

FacesServlet

which receives the HTTP request

which populates a form bean with values from request
parameters

which validates the form bean using standard and user-written
validators, and redisplays the page if errror are found

which dispatches to an Action to process the form
48
Montag, 4. Mai 2009