1 / 40

Java 2 Enterprises Edition (J2EE)

Java 2 Enterprises Edition (J2EE) Java Server Pages (JSP) Technology Jun Ni, Ph.D.M.E. Academic Technology, ITS Department of Computer Science The University of Iowa June 5, 2001 What is JSP? Stands for Java Server Pages

Télécharger la présentation

Java 2 Enterprises Edition (J2EE)

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. Java 2 Enterprises Edition (J2EE) Java Server Pages (JSP) Technology Jun Ni, Ph.D.M.E. Academic Technology, ITS Department of Computer Science The University of Iowa June 5, 2001

  2. What is JSP? • Stands for Java Server Pages • New technology for creating dynamic web content to be with Java codes • Server-side computing • Portable by supporting the JavaBean and Enterprises JavaBean component models

  3. What is JSP? • The base of JSP is Java Servlet technology • Simplified Java Servlet programming • Convert JSP to Servlet automatically • De-couple business logic and presentation • Does not do page layout design

  4. Review Static vs. Dynamic Web pages • With a static Web page, the client requests a Web page from the Web server, and the server responds by sending back the requested file to the client. Then client displays the file. request Server (maintain static Web page) Client (Web browser) response

  5. Review Static vs. Dynamic Web pages (cont.) • In dynamic Web design, the content of the page is generated at the time when it is requested, or changed the content based on client requested criteria. It is generated by application on the server, receiving input from the client, computing on the server, and responding back to the client.

  6. Review Static vs. Dynamic Web pages (cont.) Server (Web Application) request Client (Web browser) Server (generate Web page) response

  7. Other methods to create dynamic Web pages (CGI) • Most prolific from of Web application in use today. • Requests to be sent to an external program, in Perl, C/C++, or other languages • Shortcomings using CGI • Large amount of system resources • Independent from Web server • Not portable to other platforms • FastCGI (http://www.fastengines.com/) • Apache Web server (http://perl.apache.org/) • PerlEx (http://www.activestate.com/plex/)

  8. Other methods to create dynamic Web pages (Server APIs) • Web server specific API • Netscape’s Web Application Interface (WAI) • Microsoft’s Internet Information Server (ISAPI) • Tremendous performance speed and resources • Limited to particular platform and Web server • Server API extension can create several security issues, since it is as part of the Web server, which could cause the Web server to crash.

  9. Other methods to create dynamic Web pages (Client-side scripting) • Merits: • Very useful to create client-side scripting • Computing is performed on the client’s machine • More secure and efficient

  10. Other methods to create dynamic Web pages (Client-side scripting) • drawbacks: • Client needs to support the scripting language in exactly the ways expected • Differences between web browsers due to the interpretation of client-side scripts. • Various scripts (JavaScript (Nescape, Jscript(MS), VBScript (MS), ECMAScript (ECMA-262). • Can not access server-side database • JavaScript and JSP plays a different rule • The best technology is to combine them together.

  11. Other methods to create dynamic Web pages (Server-side scripting) • Scripts run on the server before the page is sent to user • Netscape’s Server Side JavaScript (SSJS) • Script is executed on the server to modify HTML pages and scripts are pre-compiled to improve server performance

  12. Other methods to create dynamic Web pages (Server-side scripting) • Microsoft’s Active Server Pages (ASP) • Very similar to JSP • Allows developer to embed VBScript or Jscript code directly into a Web page. • The ASP pages have to be compiled every time they run. • Only available to MS’s Internet Information Server 3.0 or later • Platform dependent.

  13. Other methods to create dynamic Web pages (Java applets) • Java applets are pre-designed by Web developers • They can be executed of client-side of machine. • They can be used for certain dynamic features such as multimedia and interactive Web application • Can not applied to database or file system • Performance depends on client’s machines • Loading Byte files through internet.

  14. Other methods to create dynamic Web pages (Java servlets) • Opposite to Java applets, which are small Web applications run on the client side, Java servlets are powerful alternative to CGI programs and scripting languages • Easy to be ported to any environment that supports the Servlet API. • Bypassing the security problems that effect the server APIs, since they run in Java VM. • Scalable and supported by all major Web servers • Drawback: Java programming • See J2EE’s Java Servlet technology

  15. Mechanism of JSP • JSP is extremely powerful fro Web Applications • JSP is a technology using server-side script that is actually translated into Servlets and compiled before they are run. • It provides developers a scripting interface to create powerful Java Servlets • JSP provides tags that allows developers to perform most dynamic content operations without writing complex java code. • Advanced Java Programmers can perform advanced operations in JSP pages

  16. Mechanism of JSP • On a server page, the client requests a Web page, server replaces some sections of a template with new data, and send this newly modified page to client. • JSP has special tags "<%" and "%>" to note the start and end of a JSP section, respectively.

  17. Mechanism of JSP Server Page Engine request Client (Web browser) Template Page response Generate Web Page

  18. Power of JSP • Write once and run anywhere • No AWT and Swing • Full power of Java API including networking, multithreading, database connectivity, internationalization, image manipulation, object serialization, RMI, CORBA, JNDI, Mail API, Java classes, JavaBeans and Enterprise JavaBeans • Communicate with Java Applet directly for advanced client server application development

  19. Power of JSP • Security and safety (avoid memory issues with automatic garbage collections and absence of pointers; established exception; Java security manager) • Scalability • Extensibility (Java servlet extensions; support other scripting languages; close relationship to XML • Components: JavaBean component framework

  20. Power of JSP • How to run your JSP on your web server? • You need to download JRUN from Allaire (Macromedia). Web site: http://www.jrun.com/ • You can also download from Apache software ( foundation's (http://www.apache.org/ ) Javarta project web site at http://jakarta.apache.org/

  21. HelloWorld.jsp <html> <head> <title>HelloWorld JSP Version</title> </head> <body> Hellow World from JSP! </body> </html> That is nothing but html syntax! You can write .jsp file instead of .html file URL: http://localhost:8100/HelloWorld.jsp

  22. simpleDate.html (static) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Final//EN"> <HTML> <HEAD> <TITLE>A simple date example</TITLE> </HEAD> <BODY COLOR=#ffffff> The time on the server is Friday June 8 15:12:09 PST 2001 </BODY> </HTML> URL: http://localhost:8100/simpleDate.html

  23. simpleDate.jsp (dynamic) <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Final//EN"> <HTML> <HEAD> <TITLE>A simple date example</TITLE> </HEAD> <BODY COLOR=#ffffff> The time on the server is <%= new java.util.Date() %> </BODY> </HTML> URL: http://localhost:8100/simpleDate.jsp

  24. HelloWorldLoop.jsp <html> <head> <title>Greetings</title></head> <body> <% for(int i=0;i<5;i++) { %> <h1>Hello World!</h1> <% } %> </body> </html> Interesting to embedded Java into html syntax URL: http://localhost:8100/HelloWorld.jsp

  25. JSP HelloWorld Lopp in Servlet version Alternatively you can explicitly write your code in term of servlet Java code, for example

  26. Example: Servlet based Java codeHelloWorldServletLoop.java import javax.servlet.*; import javax.servlet.http.*; import java.io.*; public class HelloWorldServletLoop extends HttpServlet { public void service (HttpServletRequest req, HttpServletResponse res) throws IOException { PrintWriter out = res.getWriter(); res.setContentType("text/html"); out.println("<HTML> <BODY> "); for(int i=0;i<10;i++) out.println("Hello World!<BR>"); out.println("<BODY> </HTML> "); out.close(); } }

  27. Compile HelloWorldServletLoop.java URL: http://localhost:8100/servlet/HelloWorldServletLoop

  28. Scripting Elements of JSP • Scriptlets (embedded into HTML page) • Expressions (access value of Java code) • Declarations (define methods and variables for initialization) • Directives (apply global information to the page) • Actions (information for translation phase) JSPs combine HTML with special tags that define dynamic behaviors. The tags have the following features:

  29. Scripting Elements of JSP • Different formats for JSP elements (Tag formats) • JSP syntax: <% script code %> (recommended) • XML standard syntax: <jsp:element />

  30. JSP's Scriptlet • JSP syntax: • <% script code %> • <jsp:scriptlet> code </jsp:scriptlet> • Small bits of Java code inserted into JPS pages • Scriplets may contain single or multiple statements • Scriplets may be combined with original HTML to provide dynamic control • Scriplets use "<%" and "%>" tags • For example:

  31. Example: date.jsp <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Final//EN"> <HTML> <HEAD> <TITLE>Current date is displayed using JSP's Scriptlet</TITLE></HEAD> <BODY> <h1> Current date is displayed using JSP's Scriptlet </h1> The current date is: <% out.println(new java.util.Date()); %> </BODY> </HTML> Small bits of Java code URL: http://localhost:8100/date.jsp

  32. Example: ScriptletHelloWorld.jsp <html> <head> <title>Dynamic HelloWorld JSP Version</title> </head> <body> <center><font size=5>HelloWorld using JSP Scriptle version</font></center> <br> The content of the Web materials is based on if-else with 50% randomization. <br> Either Albert Gore or Patrick Buchanan. <p> In order to test, please keep re-loading or re-flashing.

  33. Example: ScriptletHelloWorld.jsp <p> <%double chad=Math.random(); if (chad<0.5) {%> Albert Gore <%} else {%> Patrick Buchanan <%}%> <br> </body></html> Dynamic control http://localhost:8100/ScriptletHelloWorld.jsp

  34. JSP's Expressions • Expression: JSP allows developers to insert the value of a Java expression directly into a web page. • Expression do not end with a semi-colon and must evaluate to a basic type or String • It uses "=" sign. Scriptlet: <% out.println(Math.random() ) ; %> Expression: <% =math.radom() %>

  35. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Final//EN"> <%! public java.util.Date PrintDate() { return(new java.util.Date()); } %> <HTML> <HEAD> <TITLE>Current Date</TITLE> </HEAD> <BODY> The current date is: <%= PrintDate() %> </BODY> </HTML>

  36. JSP's Declaration • Declaration: is used to declare methods and variables that are initialized with the page.

  37. JSP's Directives • Directive contains global information that is applicable to the whole page.

  38. JSP's Action • Provide information for the translation phase of the JSP page and consist of a set of standard, built-in methods

  39. JSP Reference • W3C (http://www.w3.org/) • Apache software foundation (http://www.apache.org) • Sun Java resources (http://www.javasoft.com) • Tomcat, small-scale web server (http://jakarta.apache.org) • Jrun server (http://www.jrun.com)

  40. JSP Reference • Javasoft’s JSP web site (http://java.sun.com/products/jsp/index.html) • Tomcat’s F&Q (http://java.sun.com/products/jsp/tomcat/faq.html) • JSP’s Tagkib • (http://jakarta.apache.org/taglibs)

More Related