1 / 63

Object-Oriented Enterprise Application Development

Object-Oriented Enterprise Application Development. JavaServer Pages. Topics. During this class we will examine: Anatomy of a JavaServer Page Merging markup with Java Configuring our JSPs with directives Integrating servlets and JSPs. JavaServer Pages. Justification.

kaspar
Download Presentation

Object-Oriented Enterprise Application Development

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Object-Oriented Enterprise Application Development JavaServer Pages

  2. Topics • During this class we will examine: • Anatomy of a JavaServer Page • Merging markup with Java • Configuring our JSPs with directives • Integrating servlets and JSPs

  3. JavaServer Pages

  4. Justification • JavaServer Pages (JSPs) are a technology that allow developers to mix static with dynamic content. • JavaServer Pages have been successful for two (2) reasons: • JSPs are supported across multiple platforms. • JSPs give developers access to all Java technologies instead of using a scripting language.

  5. Roles and Responsibilities • Although servlets can generate dynamic content, it's often advantageous to separate the business logic in the servlet from the presentation logic. • By using JSPs we can have application programmers building servlets while content designers and graphic artists construct the JSPs.

  6. Versions • We'll use version 1.1 of the JavaServer Pages specification. • The 1.0 and 1.1 version JSPs are almost totally incompatible with the 0.92 version of the specification.

  7. JSP Structure

  8. Basic Concepts • At their heart, basic JSPs look just like regular HTML markup. • A static HTML document can be turned into a JSP simply by changing its extension to .jsp. • The key difference is that JSPs are dynamic. • When a JSP is executed, the JSP engine converts that JSP into an equivalent servlet which then follows the normal lifecycle.

  9. Sample Code - HelloWorld • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML4.0 Transitional//EN"> • <%-- comments look like this --%> • <HTML> • <HEAD> • <TITLE>HelloWorld JSP</TITLE> • </HEAD> • <BODY> • <H2>Hello, World!</H2> • </BODY> • </HTML>

  10. Dynamic Content • If all we needed was static content, then we'd just use HTML. The strength of JSPs lie in their ability to generate and manipulate dynamic content. • There are three common ways of performing this generation using JSPs: • Expressions, • Scriptlets • Declarations

  11. Lifecycle • Even though JSPs are ultimately compiled into servlets, there are a few differences in their lifecycle. • Most of the tags for generating dynamic content are placed within the JSP's jservice() method. • This method is called by the JSP's service() method for both GET and POST requests.

  12. JSP Tags

  13. Basic Tags • In the 0.92 version of the JSP specification, we identified dynamic content using special tags. • In version 1.1 of the JSP specification we can still use some of these tags. We can also use equivalent XML tags.

  14. JSP Expressions • The simplest form of dynamic content is the JSP expression. This is used to send a value back to the client. • The tag format of an expression is given by: <%= java expression %>

  15. Sample Code - DynamicHelloWorld • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> • <HTML> • <HEAD><TITLE> • DynamicHelloWorld JSP • </TITLE></HEAD> • <BODY> • <H2>Hello, World!</H2><BR> • <H3>It’s now • <%= new java.util.Date() %> • </H3> • </BODY> • </HTML>

  16. Implicit Variables(1 of 3) • There are eight (8) implicit variables for each JSP. The four (4) most common are: • request: The HttpServletRequest object passed to the JSP. • response: The HttpServletResponse object passed to the JSP. • session: The client's HttpSession object. • out: The PrintWriter object used to send output to the client that initiated the request.

  17. Implicit Variables(2 of 3) • Other useful variables include: • application: The ServletContext object. • config: The ServletConfig object. • pageContect: The PageContext object. • page: The this object (not currently used).

  18. Implicit Variables(3 of 3) • These implicit variable can be used within JSP expressions and scriptlets, but not within JSP declarations.

  19. Sample Code – Status(1 of 2) • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> • <HTML> • <HEAD><TITLE> • StatusHelloWorld JSP • </TITLE></HEAD> • <BODY> • <H2>Hello, World!</H2><BR> • <H3>Current Time: • <%= new java.util.Date() %> • </H3><BR> • <H3>Session ID: • <%= session.getId() %> • </H3><BR>

  20. Sample Code – Status(2 of 2) • <H3>Hostname: • <%= request.getRemoteHost() %> • </H3><BR> • <H3>Parameter: • <%= request.getParameter("test") %> • </H3> • </BODY> • </HTML>

  21. JSP Scriptlets • Often a single expression isn't adequate for the complexity of the output we want the JSP to generate. • JSPs allow us to embed complete segments of Java code within a scriptlet. • The tag format of a scriptlet is given by: <% java code %>

  22. Sample Code – Scriptlet(1 of 2) • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> • <HTML> • <HEAD> • <TITLE>Scriptlet JSP</TITLE> • </HEAD> • <BODY> • <H2>Hello, World!</H2><BR> • <H3>Current Time: • <%= new java.util.Date() %> • </H3><BR> • <H3>Session ID: • <%= session.getId() %> • </H3><BR>

  23. Sample Code – Scriptlet(2 of 2) • <H3>Hostname: • <%= request.getRemoteHost() %> • </H3><BR> • <% • String testString = request.getParameter("test"); • if (null == testString) { • testString = "no.such.parameter"; • } • %> • <H3>Parameter: <%= testString %></H3> • </BODY> • </HTML>

  24. Conditional Tags • JSP scriptlets are useful for including conditional content. • This cuts down on the amount of data returned to the client. • This technique can be used to implement basic security features.

  25. Sample Code – Scriptlet (rev.)(1 of 3) • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> • <HTML> • <HEAD> • <TITLE>Scriptlet JSP</TITLE> • </HEAD> • <BODY> • <H2>Hello, World!</H2><BR> • <H3>Current Time: • <%= new java.util.Date() %> • </H3><BR>

  26. Sample Code – Scriptlet (rev.)(2 of 3) • <% • if (session.getId() != null) { • %> • <H3>Session ID: • <%= session.getId() %> • </H3><BR> • <% • } • %> • <H3>Hostname: • <%= request.getRemoteHost() %> • </H3><BR>

  27. Sample Code – Scriptlet (rev.)(3 of 3) • <% • String testString = request.getParameter("test"); • if (null == testString) { • testString = "no.such.parameter"; • } • %> • <H3>Parameter: • <%= testString %> • </H3> • </BODY> • </HTML>

  28. Translated Code – Scriptlet(1 of 8) • import javax.servlet.*; • import javax.servlet.http.*; • import javax.servlet.jsp.*; • import javax.servlet.jsp.tagext.*; • import java.io.PrintWriter; • import java.io.IOException; • import java.io.FileInputStream; • import java.io.ObjectInputStream; • import java.util.Vector; • import org.apache.jasper.runtime.*; • import java.beans.*; • import org.apache.jasper.JasperException;

  29. Translated Code – Scriptlet(2 of 8) • public class _0002fHtml_0002fscriptlet_0002ejspscriptlet_jsp_0 extends HttpJspBase { • static { } • public _0002fHtml_0002fscriptlet_0002ejspscriptlet_jsp_0( ) { } • private static boolean _jspx_inited = false; • public final void _jspx_init() throws JasperException { }

  30. Translated Code – Scriptlet(3 of 8) • public void _jspService( HttpServletRequest request, HttpServletResponse response) • throws IOException, ServletException { • JspFactory _jspxFactory = null; • PageContext pageContext = null; • HttpSession session = null; • ServletContext application = null; • ServletConfig config = null; • JspWriter out = null; • Object page = this; • String _value = null;

  31. Translated Code – Scriptlet(4 of 8) • try { • if (_jspx_inited == false) { • _jspx_init(); • _jspx_inited = true; • } • _jspxFactory = JspFactory.getDefaultFactory(); • response.setContentType( • "text/html;charset=8859_1"); • pageContext = _jspxFactory.getPageContext( this, request, response, "", true, 8192, true);

  32. Translated Code – Scriptlet(5 of 8) • application = pageContext.getServletContext(); • config = pageContext.getServletConfig(); • session = pageContext.getSession(); • out = pageContext.getOut(); • out.write("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\r\n<HTML>\r\n <HEAD>\r\n <TITLE>Scriptlet JSP</TITLE>\r\n </HEAD>\r\n <BODY>\r\n\t\t\t<H2>Hello, World!</H2>\t<BR>\r\n\t\t\t<H3>Current Time: ");

  33. Translated Code – Scriptlet(6 of 8) • out.print( new java.util.Date() ); • out.write("</H3>\r\n\t\t\t"-<BR>\r\n\t\t\t"); • if (session.getId() != null) { • out.write("\r\n\t\t\t\t<H3>"-Session ID: "); • out.print( session.getId() ); • out.write("</H3><BR>\r\n\t\t\t"); • } • out.write("\r\n\t\t\t<H3>"-Hostname: "); • out.print( request.getRemoteHost() ); • out.write("</H3><BR>\r\n\t\t\t");

  34. Translated Code – Scriptlet(7 of 8) • String testString = request.getParameter("test"); • if (null == testString) { • testString = "no.such.parameter"; • } • out.write("\r\n\t\t\t<H3>"-Parameter: "); • out.print( testString ); • out.write("</H3>\r\n\t</BODY>"-\r\n</HTML>");

  35. Translated Code – Scriptlet(8 of 8) • } catch (Exception ex) { • if (out.getBufferSize() != 0) • out.clearBuffer(); • pageContext. handlePageException(ex); • } finally { • out.flush(); • _jspxFactory. releasePageContext(pageContext); • } • } • }

  36. JSP Declarations • A JSP declaration exists outside of the jservice() method. • This means that code in a declaration cannot make use of the implicit variables. • This allows us to declare JSP-level variables and methods. • The tag format of an expression is given by: <%! java declaration %>

  37. Sample Code – Declaration(1 of 2) • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> • <HTML> • <HEAD> • <TITLE>Declaration JSP</TITLE> • </HEAD> • <BODY> • <H2>Declarations</H2><BR> • <H3>Current Time: • <%= new java.util.Date() %> • </H3><BR> • <H3>Session ID: • <%= session.getId() %> • </H3><BR>

  38. Sample Code – Declaration(2 of 2) • <H3>Hostname: • <%= request.getRemoteHost() %> • </H3><BR> • <%! private int accesses = 0; %> • <H3> • <%= ++accesses %> • Accesses to the page since JSP was loaded. • </H3> • </BODY> • </HTML>

  39. JSP Directives

  40. Overview • We use directives to customize the JSP's behavior. Each directive affects the structure of the servlet resulting from the JSP's compilation. • There are three (3) types of directives: • page: Controls the servlet's structure. • include: Inserts a file into the servlet class. • taglib: Defines custom markup tags.

  41. Page Directives • There are many page directives. We'll look at a few of the most common attributes: • import • isThreadSafe • session • errorPage • We use the directive tag to indicate that we want a directive to take effect: <%@ directive %>

  42. Import Attribute • The import attribute allows a JSP to import class libraries: <%@ page import="java.util.*" %> • By default a JSP automatically imports: • java.lang.* • java.servlet.* • java.servlet.http.* • java.servlet.jsp.*

  43. Sample Code - Import • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> • <%@ page import=“java.util.Date” %> • <HTML> • <HEAD> • <TITLE>Import JSP</TITLE> • </HEAD> • <BODY> • <H2>Imports</H2><BR> • <H3>Current Time: • <%= new Date() %> • </H3> • </BODY> • </HTML>

  44. isThreadSafe Attribute • The isThreadSafe attribute forces the JSP's servlet to implement the SingleThreadedModel interface: <%@ page isThreadSafe="true" %> • By default a JSP is assumed to be thread safe in the same way that servlets are. • If this isn't the case you can: • Make it thread-safe using synchronized blocks • Set isThreadSafe to false.

  45. Sample Code – IsThreadSafe(1 of 2) • <%! private int id = 0; %> • <% • String userId = "id" + id; • out.println("Your id is " + id); • id++; • %>

  46. Sample Code – IsThreadSafe(2 of 2) • <%! private int id = 0; %> • <% • synchronized (this) { • String userId = "id" + id; • out.println("Your id is " + id); • id++; • } • %>

  47. Sample Code - IsThreadSafe • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> • <%@ page import=“java.util.Date” %> • <%@ page isThreadSafe=“false” %> • <HTML> • <HEAD> • <TITLE>Single Thread JSP</TITLE> • </HEAD> • <BODY> • <%! private int accesses = 0; %> • <%= ++accesses %> • Accesses since JSP was loaded. • </BODY> • </HTML>

  48. Session Attribute • The session attribute controls whether or not the JSP can access the client's session: <%@ page session="true" %> • By default a JSP participates in the client's session. <%@ page session="true" %> • If this isn't the behavior we want, we can turn it off: <%@ page session="false" %>

  49. errorPage Attribute • The errorPage attribute specifies the URL to which the client will be sent if an unhandled exception is encountered: <%@ page errorPage="some URL" %> • The exception that caused the problem will be provided to the page via the exception variable.

  50. Sample Code - ErrorPage • <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> • <%@ page errorPage=“error.jsp” %> • <HTML> • <HEAD> • <TITLE>ErrorPage JSP</TITLE> • </HEAD> • <BODY> • <H3>Session Variable: • <%= • session. • getAttribute("var").toString() • %> • </BODY> • </HTML>

More Related