How to structure a web application with the MVC pattern Chapter 7

Chapter 7
How to structure
a web application
with the MVC pattern
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Objectives
Applied
• 8VHWKH09&SDWWHUQWRGHYHORS\RXUZHEDSSOLFDWLRQVVRVHUYOHWV
FRQWUROWKHSURFHVVLQJDQG-63VGRWKHSUHVHQWDWLRQ
• 3URYLGHIRUVHUYHUVLGHGDWDYDOLGDWLRQLQ\RXUDSSOLFDWLRQV
• 8VHLQFOXGHILOHVLQ\RXU-63VDWFRPSLOHWLPHRUUXQWLPH
• 8VHWKHZHE[POILOHWRVHWLQLWLDOL]DWLRQSDUDPHWHUVDQGXVH\RXU
VHUYOHWVWRJHWWKHSDUDPHWHUV
• 8VHWKHZHE[POILOHWRLPSOHPHQWFXVWRPHUURUKDQGOLQJ
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Objectives (cont.)
Knowledge
• 'HVFULEHWKH0RGHODUFKLWHFWXUH
• 'HVFULEHWKH0RGHO9LHZ&RQWUROOHUSDWWHUQ
• ([SODLQKRZWKH09&SDWWHUQFDQLPSURYHDSSOLFDWLRQ
GHYHORSPHQW
• 'LVWLQJXLVKEHWZHHQWKHXVHRI-DYD6FULSWDQGVHUYOHWVIRUGDWD
YDOLGDWLRQ
• 'HVFULEHWKHXVHRILQFOXGHILOHV
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The code for the servlet
package email;
import
import
import
import
import
java.io.*;
javax.servlet.*;
javax.servlet.http.*;
business.User;
data.UserIO;
public class AddToEmailListServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException,
IOException
{
// get parameters from the request
String firstName =
request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String emailAddress =
request.getParameter("emailAddress");
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The code for the servlet (cont.)
// get a relative file name
ServletContext context = getServletContext();
String path =
context.getRealPath("/WEB-INF/EmailList.txt");
// use regular Java classes
User user = new User(firstName, lastName,
emailAddress);
UserIO.addRecord(user, path);
// store the User object in the request object
request.setAttribute("user", user);
// forward request and response objects to JSP page
String url = "/display_email_entry.jsp";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The code for the JSP
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Murach's Java Servlets and JSP</title>
</head>
<body>
<h1>Thanks for joining our email list</h1>
<p>Here is the information that you entered:</p>
<%@ page import="business.User" %>
<% User user = (User) request.getAttribute("user"); %>
<table cellspacing="5" cellpadding="5" border="1">
<tr>
<td align="right">First name:</td>
<td><%= user.getFirstName() %></td>
</tr>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The code for the JSP (cont.)
<tr>
<td align="right">Last name:</td>
<td><%= user.getLastName() %></td>
</tr>
<tr>
<td align="right">Email address:</td>
<td><%= user.getEmailAddress() %></td>
</tr>
</table>
<p>To enter another email address, click on the Back <br>
button in your browser or the Return button shown <br>
below.</p>
<form action="join_email_list.html" method="post">
<input type="submit" value="Return">
</form>
</body>
</html>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The Model 1 architecture
Browser
HTTP request
HTTP response
JSP
display_email_entry.jsp
Java classes
User.class
UserIO.class
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
Data store
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The Model 1 architecture
• 7KH0RGHODUFKLWHFWXUHLVVRPHWLPHVDGHTXDWHIRUZHE
DSSOLFDWLRQVZLWKOLPLWHGSURFHVVLQJUHTXLUHPHQWV:LWKWKLV
DUFKLWHFWXUH-63VKDQGOHDOORIWKHSURFHVVLQJDQGSUHVHQWDWLRQIRU
WKHDSSOLFDWLRQ
• ,QWKH0RGHODUFKLWHFWXUHWKH-63VFDQXVHUHJXODU-DYDFODVVHV
WRVWRUHWKHGDWDRIWKHDSSOLFDWLRQDQGWRGRWKHEXVLQHVV
SURFHVVLQJRIWKHDSSOLFDWLRQ
• 7KHGDWDVWRUHFDQEHDGDWDEDVHRURQHRUPRUHGLVNILOHV7KLVLV
RIWHQUHIHUUHGWRDVSHUVLVWHQWGDWDVWRUDJHEHFDXVHLWH[LVWVDIWHU
WKHDSSOLFDWLRQHQGV
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The Model-View-Controller pattern
Browser
HTTP response
HTTP request
Controller
AddToEmailListServlet.class
View
show_email_entry.jsp
Model
User.class
UserIO.class
Data store
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The Model-View-Controller (MVC) pattern
• 7KH0RGHO9LHZ&RQWUROOHU09&SDWWHUQLVFRPPRQO\XVHGWR
VWUXFWXUHZHEDSSOLFDWLRQVWKDWKDYHVLJQLILFDQWSURFHVVLQJ
UHTXLUHPHQWV7KDWPDNHVWKHPHDVLHUWRFRGHDQGPDLQWDLQ7KLV
SDWWHUQLVDOVRNQRZQDVWKH0RGHODUFKLWHFWXUH
• ,QWKH09&SDWWHUQWKHPRGHOFRQVLVWVRIEXVLQHVVREMHFWVOLNHWKH
8VHUREMHFWWKHYLHZFRQVLVWVRI+70/SDJHVDQG-63VDQGWKH
FRQWUROOHUFRQVLVWVRIVHUYOHWV
• 8VXDOO\WKHPHWKRGVRIGDWDFODVVHVOLNHWKH8VHU,2FODVVDUHXVHG
WRUHDGDQGZULWHEXVLQHVVREMHFWVOLNHWKH8VHUREMHFWWRDQGIURP
WKHGDWDVWRUH
• :KHQ\RXXVHWKH09&SDWWHUQ\RXWU\WRFRQVWUXFWHDFKOD\HUVR
LW¶VDVLQGHSHQGHQWDVSRVVLEOH7KHQLI\RXQHHGWRPDNHFKDQJHV
WRRQHOD\HUDQ\FKDQJHVWRWKHRWKHUOD\HUVDUHPLQLPL]HG
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Two methods available from the request object
Method
setAttribute(String name, Object o)
getAttribute(String name)
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
Description
6WRUHVDQ\REMHFWLQWKHUHTXHVW
DVDQDWWULEXWHDQGVSHFLILHVD
QDPHIRUWKHDWWULEXWH
$WWULEXWHVDUHUHVHWEHWZHHQ
UHTXHVWV
5HWXUQVWKHYDOXHRIWKH
VSHFLILHGDWWULEXWHDVDQ2EMHFW
W\SH,IQRDWWULEXWHH[LVWVIRU
WKHVSHFLILHGQDPHWKLV
PHWKRGUHWXUQVDQXOOYDOXH
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to set a request attribute
User user = new User(firstName, lastName, emailAddress);
request.setAttribute("user", user);
How to get a request attribute
User user = (User) request.getAttribute("user");
How to set a request attribute for a primitive type
int id = 1;
request.setAttribute("id", id);
How to get a request attribute for a primitive type
int id = (Integer) request.getAttribute("id");
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
A method of the ServletContext object for working
with paths
Method
getRequestDispatcher(String
Description
path) 5HWXUQVD5HTXHVW'LVSDWFKHU
REMHFWIRUWKHVSHFLILHGSDWK
A method of the RequestDispatcher object
Method
forward(ServletRequest request,
ServletResponse response)
Description
)RUZDUGVWKHUHTXHVWDQG
UHVSRQVHREMHFWVWRDQRWKHU
UHVRXUFHRQWKHVHUYHU
XVXDOO\D-63RUVHUYOHW
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to forward the request to an HTML page
String url = "/display_email_entry.html";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
How to forward the request to a JSP
String url = "/display_email_entry.jsp";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
How to forward the request to a servlet
String url = "/cart/displayInvoice";
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
A method of the HttpServletResponse class
Method
sendRedirect(String path)
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
Description
6HQGVDWHPSRUDU\UHGLUHFWUHVSRQVH
WRWKHFOLHQWXVLQJWKHVSHFLILHG
UHGLUHFWORFDWLRQ85/
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to redirect a response relative to the current
directory
response.sendRedirect("join_email_list.html");
How to redirect a response relative to the servlet
engine
response.sendRedirect(
"/musicStore/email/join_email_list.jsp");
How to redirect a response to a different web
server
response.sendRedirect("http://www.murach.com/email/");
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The JavaScript that validates a form
<script language="JavaScript">
function validate(form)
{
if (form.firstName.value=="")
{
alert("Please fill in your first name");
form.firstName.focus();
}
else if (form.lastName.value=="")
{
alert("Please fill in your last name");
form.lastName.focus();
}
else if (form.emailAddress.value=="")
{
alert("Please fill in your email address");
form.emailAddress.focus();
}
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The JavaScript that validates a form (cont.)
else
{
form.submit();
}
}
</script>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The HTML for the form
<form action="addToEmailList" method="post">
<table cellspacing="5" border="0">
<tr>
<td align="right">First name:</td>
<td><input type="text" name="firstName"
value="<%= user.getFirstName() %>"></td>
</tr>
<tr>
<td align="right">Last name:</td>
<td><input type="text" name="lastName"
value="<%= user.getLastName() %>"></td>
</tr>
<tr>
<td align="right">Email address:</td>
<td><input type="text" name="emailAddress"
value="<%= user.getEmailAddress() %>"></td>
</tr>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The HTML for the form (cont.)
<tr>
<td></td>
<td><br><input type="button" value="Submit"
onClick="validate(this.form)"></td>
</tr>
</table>
</form>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The dialog box that’s displayed when an entry isn’t
made
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to use JavaScript to validate the data on the
client
• :KHQ\RXXVH-DYD6FULSWWRGLVSOD\YDOLGDWLRQPHVVDJHVWKH\
DSSHDULQVWDQGDUGGLDORJER[HV
• ,I\RXZDQWWRGLVSOD\HUURUPHVVDJHVLQDQ+70/SDJHRU-63
\RXFDQXVHVHUYOHWVWRYDOLGDWHGDWDRQWKHVHUYHUVLGH
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
A servlet that validates data
package email;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import business.User;
import data.UserIO;
public class AddToEmailListServlet extends HttpServlet
{
protected void doPost(HttpServletRequest request,
HttpServletResponse response)
throws ServletException, IOException
{
// get parameters from the form
String firstName =
request.getParameter("firstName");
String lastName = request.getParameter("lastName");
String emailAddress =
request.getParameter("emailAddress");
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
A servlet that validates data (cont.)
// create the User object from the parameters
User user =
new User(firstName, lastName, emailAddress);
// validate the parameters
String message = "";
String url = "";
if (firstName.length() == 0 ||
lastName.length() == 0 ||
emailAddress.length() == 0)
{
message =
"Please fill out all three text boxes.";
url = "/join_email_list.jsp";
}
else
{
message = "";
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
A servlet that validates data (cont.)
ServletContext context = getServletContext();
String path = context.getRealPath(
"/WEB-INF/EmailList.txt");
UserIO.addRecord(user, path);
url = "/display_email_entry.jsp";
}
request.setAttribute("user", user);
request.setAttribute("message", message);
// forward request and response to the view
RequestDispatcher dispatcher =
getServletContext().getRequestDispatcher(url);
dispatcher.forward(request, response);
}
}
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
A JSP that displays a validation message
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Murach's Java Servlets and JSP</title>
</head>
<body>
<%@ page import="business.User" %>
<%
// get attributes from the request
User user = (User) request.getAttribute("user");
String message =
(String) request.getAttribute("message");
// handle null values
if (user == null) user = new User();
if (message == null) message = "";
%>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
A JSP that displays a validation message (cont.)
<h1>Join our email list</h1>
<p>To join our email list, enter your name and
email address below. <br>
Then, click on the Submit button.</p>
<p><i><%= message %></i></p>
<form action="addToEmailList" method="post">
<table cellspacing="5" border="0">
<tr>
<td align="right">First name:</td>
<td><input type="text" name="firstName"
value="<%= user.getFirstName() %>"></td>
</tr>
<tr>
<td align="right">Last name:</td>
<td><input type="text" name="lastName"
value="<%= user.getLastName() %>"></td>
</tr>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
A JSP that displays a validation message (cont.)
<tr>
<td align="right">Email address:</td>
<td><input type="text" name="emailAddress"
value="<%= user.getEmailAddress() %>"></td>
</tr>
<tr>
<td></td>
<td><br><input type="submit" value="Submit"></td>
</tr>
</table>
</form>
</body>
</html>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The JSP that’s displayed when the application is
first started
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The JSP that’s displayed when an entry isn’t made
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
A header file named header.html
<!DOCTYPE HTML PUBLIC
"-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Murach's Java Servlets and JSP</title>
</head>
<body>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
A footer file named footer.jsp
<%@ page import="java.util.*" %>
<%
// initialize the current year that's used in the
// copyright notice
GregorianCalendar currentDate = new GregorianCalendar();
int currentYear = currentDate.get(Calendar.YEAR);
%>
<p><small>
&copy; Copyright <%= currentYear %>
Mike Murach &amp; Associates, Inc.
All rights reserved
</small></p>
</body>
</html>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
A JSP file that uses both include files
<%@ include file="/includes/header.html" %>
<h1>Thanks for joining our email list</h1>
<p>Here is the information that you entered:</p>
<%@ page import="business.User" %>
<% User user = (User) request.getAttribute("user"); %>
<table cellspacing="5" cellpadding="5" border="1">
<tr><td align="right">First name:</td>
<td><%= user.getFirstName() %></td>
</tr>
<tr><td align="right">Last name:</td>
<td><%= user.getLastName() %></td>
</tr>
<tr><td align="right">Email address:</td>
<td><%= user.getEmailAddress() %></td>
</tr>
</table>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
A JSP file that uses both include files (cont.)
<p>To enter another email address, click on the Back <br>
button in your browser or the Return button shown <br>
below.</p>
<form action="join_email_list.jsp" method="post">
<input type="submit" value="Return">
</form>
<%@ include file="/includes/footer.jsp" %>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The JSP page
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
Another JSP page that uses the same header and
footer
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to include a file in a JSP at compile-time with
an include directive
Syntax
<%@ include file="fileLocationAndName" %>
Examples
<%@ include file="/includes/header.html" %>
<%@ include file="/includes/footer.jsp" %>
How to include a file in a JSP at runtime with an
include action
Syntax
<jsp:include page="fileLocationAndName" />
Examples
<jsp:include page="/includes/header.html" />
<jsp:include page="/includes/footer.jsp" />
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Two techniques for including files in a JSP
• 7RLQFOXGHDILOHLQD-63DWFRPSLOHWLPH\RXXVHWKHLQFOXGH
GLUHFWLYH
• :KHQ\RXXVHWKHLQFOXGHGLUHFWLYHWKHFRGHLQWKHLQFOXGHGILOH
EHFRPHVSDUWRIWKHJHQHUDWHGVHUYOHW$VDUHVXOWDQ\FKDQJHVWR
WKHLQFOXGHGILOHZRQ¶WDSSHDULQWKH-63XQWLOWKH-63LV
UHJHQHUDWHGDQGUHFRPSLOHG+RZHYHUVRPHRIWKHQHZHUZHE
VHUYHUVDXWRPDWLFDOO\GHWHFWFKDQJHVWRLQFOXGHGILOHVDQG
DXWRPDWLFDOO\UHJHQHUDWHDQGUHFRPSLOHWKHVHUYOHWVIRUWKH-63V
WKDWQHHGWREHXSGDWHG
• 7RLQFOXGHDILOHLQD-63DWUXQWLPH\RXXVHWKHLQFOXGHDFWLRQ
• :KHQ\RXXVHWKHLQFOXGHDFWLRQWKHJHQHUDWHGVHUYOHWXVHVD
UXQWLPHFDOOWRJHWWKHLQFOXGHGILOHDQGDQ\FKDQJHVWRWKH
LQFOXGHGILOHDSSHDULQWKH-63WKHQH[WWLPHLWLVUHTXHVWHG
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The web.xml file for the Email List application
<?xml version="1.0" encoding="UTF-8"?><web-app version="2.5"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<context-param>
<param-name>custServEmail</param-name>
<param-value>[email protected]</param-value>
</context-param>
<servlet>
<servlet-name>AddToEmailListServlet</servlet-name>
<servlet-class>email.AddToEmailListServlet
</servlet-class>
<init-param>
<param-name>relativePathToFile</param-name>
<param-value>/WEB-INF/EmailList.txt
</param-value>
</init-param>
</servlet>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The web.xml file for the Email List application
(cont.)
<servlet-mapping>
<servlet-name>AddToEmailListServlet</servlet-name>
<url-pattern>/addToEmailList</url-pattern>
</servlet-mapping>
<!-- you can comment out these tags when the app is in
development -->
<error-page>
<error-code>404</error-code>
<location>/error_404.jsp</location>
</error-page>
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error_java.jsp</location>
</error-page>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The web.xml file for the Email List application
(cont.)
<session-config>
<session-timeout>30</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>join_email_list.jsp</welcome-file>
</welcome-file-list>
</web-app>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to work with the web.xml file
• 7KHZHE[POILOHLVVWRUHGLQWKH:(%,1)GLUHFWRU\IRUDQ
DSSOLFDWLRQ:KHQ7RPFDWVWDUWVLWUHDGVWKHZHE[POILOH
• ,IWKHHOHPHQWVLQWKHZHE[PODUHQ¶WLQWKHFRUUHFWRUGHU7RPFDW
ZLOOGLVSOD\DQHUURUPHVVDJHZKHQLWUHDGVWKHZHE[POILOH
• $IWHU\RXPRGLI\WKHZHE[POILOH\RXPXVWUHGHSOR\WKH
DSSOLFDWLRQVRWKHFKDQJHVWDNHHIIHFW2U\RXFDQUHVWDUW7RPFDW
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
XML tags that set initialization parameters in a
web.xml file
<context-param>
<param-name>custServEmail</param-name>
<param-value>[email protected]</param-value>
</context-param>
<servlet>
<servlet-name>AddToEmailListServlet</servlet-name>
<servlet-class>email.AddToEmailListServlet
</servlet-class>
<init-param>
<param-name>relativePathToFile</param-name>
<param-value>/WEB-INF/EmailList.txt</param-value>
</init-param>
</servlet>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
XML elements for working with initialization
parameters
Element
<context-param>
<servlet>
<servlet-name>
<servlet-class>
<init-param>
<param-name>
<param-value>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
Description
'HILQHVDSDUDPHWHUWKDW¶VDYDLODEOHWRDOO
VHUYOHWVZLWKLQDQDSSOLFDWLRQ
,GHQWLILHVDVSHFLILFVHUYOHWZLWKLQWKH
DSSOLFDWLRQ
'HILQHVWKHQDPHIRUWKHVHUYOHWWKDW¶VXVHGLQ
WKHUHVWRIWKHZHE[POILOH
,GHQWLILHVWKHVHUYOHWE\VSHFLI\LQJWKHVHUYOHW¶V
SDFNDJHDQGFODVVQDPH
'HILQHVDQDPHYDOXHSDLUIRUDQLQLWLDOL]DWLRQ
SDUDPHWHUIRUDVHUYOHW
'HILQHVWKHQDPHRIDSDUDPHWHU
'HILQHVWKHYDOXHRIDSDUDPHWHU
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to work with initialization parameters
• 7RFUHDWHDQLQLWLDOL]DWLRQSDUDPHWHUWKDWZLOOEHDYDLODEOHWRDOO
VHUYOHWVFDOOHGDFRQWH[WLQLWLDOL]DWLRQSDUDPHWHU\RXFRGHWKH
SDUDPQDPHDQGSDUDPYDOXHHOHPHQWVZLWKLQWKHFRQWH[WSDUDP
HOHPHQW
• 7RFUHDWHDQLQLWLDOL]DWLRQSDUDPHWHUWKDWZLOOEHDYDLODEOHWRD
VSHFLILFVHUYOHWFDOOHGDVHUYOHWLQLWLDOL]DWLRQSDUDPHWHU\RXFRGH
WKHSDUDPQDPHDQGSDUDPYDOXHHOHPHQWVZLWKLQWKHLQLWSDUDP
HOHPHQW%XWILUVW\RXPXVWLGHQWLI\WKHVHUYOHWE\FRGLQJWKH
VHUYOHWVHUYOHWQDPHDQGVHUYOHWFODVVHOHPHQWV
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Two methods of the GenericServlet class
Method
getServletContext()
getServletConfig()
Description
5HWXUQVD6HUYOHW&RQWH[WREMHFWWKDW
FRQWDLQVLQIRUPDWLRQDERXWWKHHQWLUHZHE
DSSOLFDWLRQ
VFRQWH[W
5HWXUQVD6HUYOHW&RQILJREMHFWWKDWFRQWDLQV
LQIRUPDWLRQDERXWDVLQJOHVHUYOHW¶V
FRQILJXUDWLRQ
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
A method of the ServletContext and ServletConfig
interfaces
Method
getInitParameter(String
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
Description
name) 5HWXUQVD6WULQJREMHFWWKDW
FRQWDLQVWKHYDOXHRIWKHVSHFLILHG
LQLWLDOL]DWLRQSDUDPHWHU,IWKH
SDUDPHWHUGRHVQ¶WH[LVWWKLV
PHWKRGUHWXUQVDQXOOYDOXH
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
Code that gets an initialization parameter that’s
available to all servlets
ServletContext context = this.getServletContext();
String custServEmail =
context.getInitParameter("custServEmail");
Code that gets an initialization parameter that’s
available to the current servlet only
ServletConfig config = this.getServletConfig();
String relativePath =
config.getInitParameter("relativePathToFile");
The getInitParameter methods
• 7RJHWDQLQLWLDOL]DWLRQSDUDPHWHUWKDW¶VDYDLODEOHWRDOOVHUYOHWV
\RXXVHWKHJHW,QLW3DUDPHWHUPHWKRGRIWKH6HUYOHW&RQWH[WREMHFW
• 7RJHWDQLQLWLDOL]DWLRQSDUDPHWHUIRUDVSHFLILFVHUYOHW\RXXVHWKH
JHW,QLW3DUDPHWHUPHWKRGRIWKH6HUYOHW&RQILJREMHFW
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
XML elements for working with error handling
Element
Description
<error-page>
6SHFLILHVDQ+70/SDJHRU-63WKDW¶VGLVSOD\HG
ZKHQWKHDSSOLFDWLRQHQFRXQWHUVDQXQFDXJKW
H[FHSWLRQRUDFHUWDLQW\SHRI+773VWDWXVFRGH
<error-code>
6SHFLILHVWKHQXPEHURIDYDOLG+773VWDWXV
FRGH
<exception-type> 8VHVWKHIXOO\TXDOLILHGFODVVQDPHWRVSHFLI\D
-DYDH[FHSWLRQ
<location>
6SHFLILHVWKHORFDWLRQRIWKH+70/SDJHRU-63
WKDW¶VGLVSOD\HG
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The XML tags that provide error-handling for a
HTTP 404 status code
<error-page>
<error-code>404</error-code>
<location>/error_404.jsp</location>
</error-page>
The code for a file named error_404.jsp
<%@ include file="/includes/header.html" %>
<%@ page isErrorPage="true" %>
<h1>404 Error</h1>
<p>The server was not able to find the file you
requested.</p>
<p>To continue, click the Back button.</p>
<br>
<%@ include file="/includes/footer.jsp" %>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The XML tags that provide error-handling for all
Java exceptions
<error-page>
<exception-type>java.lang.Throwable</exception-type>
<location>/error_java.jsp</location>
</error-page>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The code for a file named error_java.jsp
<%@ include file="/includes/header.html" %>
<%@ page isErrorPage="true" %>
<h1>Java Error</h1>
<p>Sorry, Java has thrown an exception.</p>
<p>To continue, click the Back button.</p>
<br>
<h2>Details</h2>
<p>
Type: <%= exception.getClass() %><br>
Message: <%= exception.getMessage() %><br>
</p>
<%@ include file="/includes/footer.jsp" %>
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The JSP page for the 404 error
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
The JSP page that’s displayed when a Java
exception is thrown
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH
How to implement custom error handling
• ,QWKHZHE[POILOH\RXFDQXVHWKHHUURUSDJHHOHPHQWWRVSHFLI\
WKHHUURUSDJHVWKDWVKRXOGEHGLVSOD\HGZKHQWKHDSSOLFDWLRQ
HQFRXQWHUVVSHFLILF+773VWDWXVFRGHVRUXQFDXJKW
H[FHSWLRQV
• %\GHIDXOWWKH,QWHUQHW([SORUHUXVHVLWVRZQHUURUSDJHVIRU+773
VWDWXVFRGHV$VDUHVXOWLI\RXZDQWWRXVHWKLVEURZVHUWRYLHZD
FXVWRPSDJH\RXPXVWVHOHFWWKH7RROVÆ,QWHUQHW2SWLRQV
FRPPDQGDQGXVHWKH$GYDQFHGWDERIWKHGLDORJER[WRGHVHOHFW
WKH³6KRZIULHQGO\+773HUURUPHVVDJHV´RSWLRQ
0XUDFK¶V-DYD6HUYOHWV-63QG(G&
‹0LNH0XUDFK$VVRFLDWHV,QF
6OLGH