1
© 2001-2002 Marty Hall, Larry Brown http://www.corewebprogramming.com
core
programming
JavaServer Pages
JSP
JSP
2
www.corewebprogramming.com
Agenda
• Introducing JavaServer PagesTM (JSPTM)
• JSP scripting elements
• The JSP page Directive: Structuring
Generated ServletsTM
• Including Files in JSP Documents
• Using JavaBeans™ components with JSP
• Creating custom JSP tag libraries
• Integrating servlets and JSP with the MVC
architecture
JSP
3
www.corewebprogramming.com
The Need for JSP
• With servlets, it is easy to
– Read form data
– Read HTTP request headers
– Set HTTP status codes and response headers
– Use cookies and session tracking
– Share data among servlets
– Remember data between requests
– Get fun, high-paying jobs
• But, it sure is a pain to
– Use those println statements to generate HTML
– Maintain that HTML
JSP
4
www.corewebprogramming.com
The JSP Framework
• Idea:
– Use regular HTML for most of page
– Mark servlet code with special tags
– Entire JSP page gets translated into a servlet (once), and
servlet is what actually gets invoked (for each request)
• Example:
– JSP
• Thanks for ordering
<I><%= request.getParameter("title") %></I>
– URL
• http://host/OrderConfirmation.jsp?title=Core+Web+Programming
– Result
• Thanks for ordering Core Web Programming
JSP
5
www.corewebprogramming.com
Benefits of JSP
• Although JSP technically can't do anything
servlets can't do, JSP makes it easier to:
– Write HTML
– Read and maintain the HTML
• JSP makes it possible to:
– Use standard HTML tools such as HomeSite or
UltraDev
– Have different members of your team do the HTML
layout and the programming
• JSP encourages you to
– Separate the (Java) code that creates the content from the
(HTML) code that presents it
JSP
6
www.corewebprogramming.com
Advantages of JSP Over
Competing Technologies
• Versus ASP or ColdFusion
– Better language for dynamic part
– Portable to multiple servers and operating systems
• Versus PHP
– Better language for dynamic part
– Better tool support
• Versus WebMacro or Velocit