1 / 19

Using JavaServer Pages

Using JavaServer Pages. Harry R. Erwin, PhD CIT304/CSE301. Review. Last lecture, we examined the use of Java servlets as a way to provide dynamic content. Problems: Requires fully-qualified Java programmers (£) Tedious and error-prone (£) Time-consuming and requires frequent update (££)

flavio
Download Presentation

Using JavaServer Pages

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. Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301

  2. Review • Last lecture, we examined the use of Java servlets as a way to provide dynamic content. • Problems: • Requires fully-qualified Java programmers (£) • Tedious and error-prone (£) • Time-consuming and requires frequent update (££) • Adding a new servlet involves editing XML and restarting the application (£) • Mixing static HTML with servlets does not scale (£). • This lecture, we will examine the use of JavaServer Pages to overcome these problems.

  3. Resources • Farley, Crawford, and Flanagan, 2002, Java Enterprise in a Nutshell, second edition, O’Reilly, ISBN: 0-596-00152-5. For programmers. • Bergsten, 2002, JavaServer Pages, second edition, O’Reilly, ISBN: 0-596-00317-X. Covers JSP 1.2, for both programmers and web page authors. Has lots of worked examples. Recommended. If you have to manage web application development, it won’t hurt you to know something about the process.

  4. JavaServer Pages (JSP) • Allow you to embed the dynamic elements into the static content. • Can be edited using a text editor or HTML authoring tool. • Can be previewed live in the various browsers that you plan to support.

  5. Template Engines Resources Template Processor Web Server Web Browser Instruction Template

  6. Java and JSP • JSP pages are compiled into Java servlets before being executed. • Benefits • More efficient lifecycle • Integration with Java • Easy use of JavaBeans without writing code • Java code can be completely avoided. • Avoids programming.

  7. JSP Basics • Individual JSP pages are text files stored on the web server. • When a page is first requested, the JSP engine uses the page to generate a servlet. • The compiled servlet is saved and used to handle later requests. • When a page is modified, the servlet is recreated. • Precompilation of pages is also feasible.

  8. A Simple JSP (from Java Enterprise in a Nutshell) <HTML> <BODY> Hello, sailor, it is now <%= new java.util.Date().toString() %> </BODY> </HTML> The only non-self-evident element is the: “new java.util.Date().toString()” This creates a date object and converts it to a String that can be displayed. The <%= %> tag is are used to display a Java variable. XML syntax can also be used, if you want to be open to the future.

  9. Points to Note • More generally, the <% %> elements (HTML/XML comments) can be used to insert regular Java code to control the flow of a page. • We will discuss this in more detail next lecture. • The next slide gives an example of an ‘if, then, else’ flow control construct used in a JSP page. This is also from Java Enterprise in a Nutshell.

  10. Java Flow Control <HTML> <BODY> <% java.util.Date theDate = new java.util.Date(); %> <% int theHour = theDate.getHours(); %> <% if(theHour<12) { %> Good morning, <% } else { %> Good afternoon, <% } %> sailor. It is now <%= theDate.toString() %>. </BODY> </HTML>

  11. Java APIs Available • JDBC (=ODBC) • RMI and CORBA • Java Naming and Directory Interface • Enterprise JavaBeans • Java Message Service • Java Transaction API • JAXP • JavaMail

  12. JSP Directives • Affect the entire page. • Begin with <%@ or <% @ • <% @include file=“data.html”%> • Used to include static content • <% @page … %> • Used to set page parameters • <% @taglib … %> • Declares a tag library (later…)

  13. Important Page Directives • contentType • MIME type of the page, default: text/html • extends • JspPage class to be used if not default. • import • Java classes or packages to import. • info • A description of JSP page. • session • Indicates whether page participates in a user session.

  14. Declaration Elements • This is another type of tag used to create variables. • <%! global variable declaration=value; %> • These are defined at the level of the JSP rather than at the session level. They persist across invocations. • They can be used to count hits and detect hacking. Not all that useful. Use other approaches, if the data need to persist whenever the JSP is rebuilt and reloaded.

  15. Built-ins • Your web server provides classes that the JSP can use. These include: • Configuration data • The standard output stream • The request that created the invocation • The response being generated • The current user’s session record.

  16. What’s in it for the Web Designer? • JSP supports action tags. • These are regular HTML tags of two types: • Built-in functions • Custom tags • Built-in functions use XML namespace-enabled tag syntax. They work with JavaBeans, too.

  17. Standard Action Tags • <jsp:useBean> imports a JavaBeans component. • <jsp:getProperty> gets a property value and adds it to the response. • <jsp:setProperty> sets a property value. • <jsp:include> includes the response from a servlet or JSP page that is called for the request. • <jsp:forward> forwards the call to a servlet or JSP page for service. • <jsp:param> adds a parameter to a request. • <jsp:plugin> generates the OBJECT or EMBED tag for an applet.

  18. taglib Directives • If the web site architect wants to extend the JSP tag syntax, she can also define custom action tags. These are organized into Tag Libraries and can be loaded using the taglib directive. • This allows her to remove almost all Java code from the JSPs. Her web page developers use these custom tags to replace the calls to Java functions, beans and programs. • The programmers can then do their jobs separately from everyone else.

  19. Conclusions • JSP is a solution to providing dynamic content. • Equivalent to ASP.NET but currently more popular. • Can be used to avoid Java scripting. • Next lecture: • Java programming basics—just enough to let you organize your JSP pages—there is no requirement to learn class or method syntax unless you want to provide programming support to a web design organization.

More Related