Professional Documents
Culture Documents
Controlling
g Web
Application Behavior
The Deployment Descriptor: web.xml
web xml
Originals of Slides and Source Code for Examples:
http://courses coreservlets com/Course-Materials/msajsp html
http://courses.coreservlets.com/Course-Materials/msajsp.html
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 1.x & JSF 2.0, Struts Classic & Struts 2, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Basics
9
Older
web xml Version: 2.3
web.xml 23
<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app
web app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web app>
<web-app>
...
</web-app>
10
Custom URLs
((Servlet Mappings)
pp g )
17
Disabling Invoker Servlet: Example
(Continued)
18
20
Init Params
22
Types of Initialization
• Servlets
– Call ServletConfig.getInitParameter from the init method
• JSP pages
–C
Call
ll ServletConfig.getInitParameter
S l C fi I iP from
f the
h jspInit
j I i
method. Use jsp-file instead of servlet-class.
• Servlet context
– Call ServletContext.getInitParameter
– What method to call this from? See lecture on listeners!
• Filters
– See later lecture
• Listeners
– See later lecture
23
Assigning Init Params:
Problems with Invoker Servlet
<servlet>
<servlet-name>InitTest</servlet-name>
<servlet-class>moreservlets.InitServlet</servlet-class>
<init-param>
<param-name>firstName</param-name>
p p
<param-value>Larry</param-value>
</init-param>
<init-param>
p
<param-name>emailAddress</param-name>
<param-value>ellison@microsoft.com</param-value>
</init-param>
/ p
</servlet>
<servlet-mapping>
<servlet name>InitTest</servlet name>
<servlet-name>InitTest</servlet-name>
<url-pattern>/showInitValues</url-pattern>
</servlet-mapping>
24
26
28
• If y
you leave invoker turned on and have
declaration on previous page:
– Initialized JSP page could be accessed with
htt //h t/ bA P fi /
http://host/webAppPrefix/servlet/InitPage.
l t/I itP Y k!
Yuck!
– Assign URL back to original URL of JSP page instead.
29
Reading JSP Initialization
Parameters
…
<UL>
<LI>First name: <%= firstName %>
<LI>Email address: <%= emailAddress %>
</UL>
…
<%!
This results in ugly and hard-to-
private String firstName, emailAddress; maintain JSP pages. Consider
using MVC and never using
direct init params in JSP.
public void jspInit() {
ServletConfig config = getServletConfig();
fi tN
firstName = config.getInitParameter("firstName");
fi tI itP t ("fi tN ")
if (firstName == null) { firstName = "No first name"; }
emailAddress = config.getInitParameter("emailAddress");
if (emailAddress == null) { emailAddress = "No
No email";
email ; }
}
%>
30
31
Assigning Application-Wide
Initialization Parameters
• web.xml element: context-param
<context-param>
<param-name>support-email</param-name>
<param-value>blackhole@mycompany.com</param-value>
</context-param>
32
36
<web app...>
<web-app >
<error-page>
<error-code>404</error-code>
<l
<location>/WEB-INF/NotFound.jsp</location>
ti >/WEB INF/N tF d j </l ti >
</error-page>
...
</web-app>
37
Error Pages and Status Codes:
Result
38
A Dangerous Computation
package moreservlets;
40
41
WEB-INF/DDE.jsp
<!DOCTYPE HTML PUBLIC
//W3C//DTD HTML 4
"-//W3C//DTD 4.0
0 Transitional//EN">
Transitional//EN >
<HTML>
<HEAD><TITLE>Dumb</TITLE></HEAD>
<BODY BGCOLOR="#FDF5E6">
BGCOLOR= #FDF5E6 >
<H2>Dumb Developer</H2>
We're brain dead. Consider using our competitors.
</BODY></HTML>
42
43
© 2009 Marty Hall
Other Capabilities
Session Timeouts
• You can explicitly deactivate sessions
– session.invalidate()
i i lid t ()
• You can also set session timeout
– session.setMaxInactiveInterval(...)
( )
• You can set Web-app-wide default timeout
<session-config>
<session-timeout>
time-in-minutes
</session-timeout>
</session-config>
– A value
l off 0 or negative
ti number
b indicates
i di t that
th t default
d f lt
sessions should never automatically time out
• If no session-config
– Default session timeout is server-specific
45
Deactivating Scripting or
Expression Language (2.4+
(2 4+ only)
• Disabling scripting
– Used when you want to enforce pure-MVC approach
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
p j p p
<scripting-invalid>true</scripting-invalid>
</jsp-property-group>
• Disabling Expression Language
– Used when you have JSP 1.2 pages that might
accidentally contain ${blah}. Note that EL is disabled
automatically if you use version 2.3 of web.xml.
<jsp-property-group>
<url-pattern>*.jsp</url-pattern>
<el-ignored>true</el-ignored>
</jsp-property-group>
46
Questions?