Professional Documents
Culture Documents
ER/CORP/CRS/ED113/003
Confidential
General Guideline
(2008) Infosys Technologies Ltd.
This document contains valuable confidential and proprietary information of Infosys. Such
confidential and proprietary information includes, amongst others, proprietary intellectual
property which can be legally protected and commercialized. Such information is furnished
herein for training purposes only. Except with the express prior written permission of Infosys,
this document and the information contained herein may not be published, disclosed, or used
for any other purpose.
Confidential
Confidential Information
Confidential
Learning approach
The following are strongly suggested for a better learning and
understanding of this course:
Noting down the key concepts in the class, explained by the educator
Analyze all the examples / code snippets provided
Study and understand the self study topics
Completion and submission of all the assignments, on time
Completion of the self review questions in the lab guide
Study and understand all the artifacts including the reference materials / elearning / supplementary materials specified
Completion of the project (if applicable for this course) on time inclusive
of individual and group activities
Taking part in the self assessment activities
Participation in the doubt clearing sessions
Confidential
Course Objectives
To illustrate the development of presentation tier of
an enterprise application
Through Servlet, JSP and JSF
Confidential
Course Pre-requisites
Knowledge of
HTML Forms development
HTTP Methods
HTTP Request and Response model
Core Java concepts such as packaged class, interfaces
and API
Advanced Java concepts such as multithreading,
connecting to database
Confidential
Confidential
Expectations
At the end of this course, the participant should be
able to Design and develop a web application using Servlet and
JSP
Design and develop a web application using JSF
framework
Develop the web application using Eclipse IDE
Develop web application that supports i18n using JSF
Deploy the developed web application in an application
server such as JBoss 5.0
Test and Debug the web application
Copyright 2008, Infosys Technologies Ltd.
Confidential
Course Agenda
Day 1
Day 2
Introduction to JSP
Basic tags in JSP
Lifecycle and lifecycle methods of JSP
Implicit objects in JSP
Tags in JSP
Directive Elements in JSP
Copyright 2008, Infosys Technologies Ltd.
Confidential
Course Agenda
Day 3
Day 4
Introduction to JSF
Developing and deploying web application using JSF
Page Navigation
Managed Bean Facility
UI Components and Core tags in JSF
Copyright 2008, Infosys Technologies Ltd.
10
Confidential
Course Agenda
Day 5
Event Handling
Validators
Converters
Dynamic Data Table Creation
Day 6
Internationalization and localization of JSF web application
JSF Request Processing Lifecycle
11
Confidential
12
Confidential
13
Confidential
ER/CORP/CRS/ED113/003
Confidential
Confidential
15
Confidential
16
Confidential
17
Confidential
ER/CORP/CRS/ED113/003
Confidential
Confidential
23
Confidential
29
Confidential
Dynamic Pages
The program / software component that executes the serverside program to generate the dynamic content is known as the
Web Container
Copyright 2008, Infosys Technologies Ltd.
30
Confidential
31
Confidential
Application Server
ER/CORP/CRS/ED113/003
Confidential
Confidential
Application Server
Application Servers are specialized Web Server +
Web Container + EJB Container
Application Servers provide all the system services to
address non-functional requirements :
33
Confidential
Vendor independent
Vendor dependent
Ex: MS IIS
34
Confidential
ER/CORP/CRS/ED113/003
Confidential
Confidential
36
Confidential
37
Confidential
Servlets
Any container should be able to execute any Servlet
This requirement calls for a standard interface for all
the Servlets
38
Confidential
Servlet API
Servlet Interface
Present in package javax.servlet
Has the following methods
init() called once for initialization of instance
variables of instantiated servlet object
service()- called each time, servlet is requested
Referred as Request Handling method
destroy() - called once after servlet object is removed
2 implementing Servlet classes
GenericServlet
HttpServlet
Copyright 2008, Infosys Technologies Ltd.
39
Confidential
40
Confidential
<<interface>>
Servlet
(present in javax.servlet)
GenericServlet
(present in javax.servlet)
HttpServlet
(present in
javax.servlet.http)
Deployment Descriptor
web.xml the Deployment Descriptor
There should be on deployment descriptor per web
application
Should be placed within the WEB-INF subfolder of
Application root folder
Mapping between the URL and the corresponding
servlet class is defined within
41
Confidential
42
Confidential
43
Confidential
Contd
package edu.infy.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
public class HelloServlet implements
javax.servlet.Servlet {
public void destroy() {
}
public ServletConfig getServletConfig() {
return null;
}
public String getServletInfo() {
return null;
}
Copyright 2008, Infosys Technologies Ltd.
Confidential
web.xml
welcome.html
<html>
<head>
<title>Example html</title>
</head>
<body>
<h1>
<url-pattern>/hello</url-pattern>
</servlet-mapping>
<!-- end of root tag -->
45
Confidential
WEB-INF
welcome.html
src
classes
web.xml
lib
edu
infy
servlets
HelloServlet.java
46
Confidential
47
Confidential
48
Confidential
request,
If request is
for dynamic Client
content,
invokes Web
Container
Web Server
web.xml
Loads the
Spawn a
HelloServlet
Invokes the
Instantiates
thread for
HelloServlet
class
web.xml
into
the request
memory
Invokes the
HTTP &
Generates
Web Container sends
service
the
method
HTTP
Response
<servlet>
<servlet-name>myFirstServlet</servlet-name>
<servlet-class>com.infy.HelloServlet</servlet-class>
/html");
</servlet>
Writer();
<servlet-mapping>
ITLE>
<servlet-name>myFirstServlet</servlet-name>
</HEAD>"); <url-pattern>/hello</url-pattern>
World !
</servlet-mapping>
Y> </HTML>");
Copyright 2008, Infosys Technologies Ltd.
49
Confidential
If container
Invokes
stoppedthe
or
web.xml
Spawn
servleta&
determines
thread for
undeployed,
the
request
servlet
invokes
name
destroy()
Web Server
Invokes the&
Generates
Web Container sends
HTTP
the
service
HTTP
public void service(ServletRequest request,
method
Response
ServletResponse response) throws
ServletException, IOException {
response.setContentType("text/html");
PrintWriter out = response.getWriter();
out.println("<HTML><HEAD><TITLE>
Welcome to Servlet</TITLE></HEAD>");
out.println("<BODY><H2>Hello World !
Welcome to Servlets!</H2></BODY> </HTML>");
out.close();
} 2008, Infosys Technologies Ltd.
Copyright
50
Confidential
52
Confidential
53
Confidential
54
Confidential
Servlet : Example
Problem Definition:
loginForm.html accepts username and password, and
passes it to Servlet, LoginServlet. LoginServlet has to
verify if it is valid username in a Database.
If valid, display Welcome! Login successful!
If invalid, display Sorry guest ! Login failed!
Note: For Simplicity, we are not connecting to Database,
whereas, we will check the username against a value. If
it is admin, then it is valid.
55
Confidential
Servlet : Solution
Solution
Create a dynamic web project
Create LoginServlet.java inheriting from HttpServlet
Create loginForm.html with a text field for username, a
password field for password and a submit button to
invoke the LoginServlet URL
Define the deployment descriptor (to map the
HttpServlet to the URL)
Build and deploy the application
Execute the application
56
Confidential
LoginServlet.java (1/2)
LoginServlet.java
LoginServlet.java
package edu.infy.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
public class LoginServlet extends
HttpServlet{
protected void doPost(HttpServletRequest
request, HttpServletResponse response)
throws
ServletException, IOException {
String message = new String();
String username =
request.getParameter("username");
57
Confidential
web.xml
58
Confidential
loginForm.html
loginForm.html
loginForm.html
<html><head>
<title>Login Page</title>
</head>
<body bgcolor="#CCFFCC" >
<form action="Login" method="post">
<h2><center>Enter Login Details </center>
<br>
<table border="4" align="center"
bordercolor="brown" bgcolor="pink">
<tr><td>UserName</td><td>
<input type="text" name="username">
</td></tr>
<tr><td><input type="submit"
value="Login"/></td>
<td><input type="reset"
value="Reset"/></td></tr>
</table>
</h2>
</form>
</body>
</html>
59
Confidential
Result
60
Confidential
Server-side validations
What if data for username and password are left blank
and form is submitted?
How will you make sure that user has entered all
mandatory fields on the form before submitting it?
Answer JavaScript
But this is client-side validations, and user can disable
JavaScript from the web browser settings
So, we need to check it at server-side too
Checking whether the data given for the parameter is
zero-length or null are must Server-side validations
Copyright 2008, Infosys Technologies Ltd.
62
Confidential
63
Confidential
64
Confidential
65
Confidential
JSP
We need a technology which helps us to
write/maintain server-side presentation logic
easily. JSP!!!
66
Confidential
Reference Material
For more information about the topics covered today,
refer the supplementary material given in the
following link:
http://myshec123505d/EBook
67
Confidential
Summary
Java Platform, Enterprise Edition (Java EE) is the
industry, open standard for developing portable,
robust, scalable and secure server-side Java
applications
A Java EE App Server takes care of the nonfunctional requirements of a Java EE application
Java EE provides two technologies Servlet and JSP
to develop the server side presentation logic
As a good programming practice, presentation logic
and business logic need to be separated
Copyright 2008, Infosys Technologies Ltd.
68
Confidential
Summary
Servlets are difficult to maintain by the page authors/
web designers
Web designers would be maintaining the
presentation logic and hence Sun Microsystems
offered JSP technology which is as simple as HTML
and as powerful as Servlets
JSP help us to code server side presentation logic
69
Confidential
Thank You
The contents of this document are proprietary and confidential to Infosys Technologies Ltd. and may not be
disclosed in whole or in part at any time, to any third party without the prior written consent of
Infosys Technologies Ltd.
2008 Infosys Technologies Ltd. All rights reserved. Copyright in the whole and any part of this document
belongs to Infosys Technologies Ltd. This work may not be used, sold, transferred, adapted, abridged, copied or
reproduced in whole or in part, in any manner or form, or in any media, without the prior written consent of Infosys
Technologies Ltd.
70
Confidential
71
Confidential
Application
Root
WEB-INF
Html pages
classes
lib
web.xml
External
libraries
Copyright 2008, Infosys Technologies Ltd.
src
Packaged servlets
72
Confidential
73
Confidential