1 Web Programming with Java Java Server Pages Huynh Huu Viet University of Information Technology Department of Information Systems Email: viethh@uit.edu.vn 2008 © Department of Information Systems - University of Information Technology 2 Outline Introduction Scripting Elements The JSP page Directive Including Files Using JavaBeans Components in JSP Servlets and JSP: The Model View Controller (MVC) Architecture 2008 © Department of Information Systems - University of Information Technology 3 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 2008 © Department of Information Systems - University of Information Technology 4 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 2008 © Department of Information Systems - University of Information Technology 5 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 Macromedia DreamWeaver or Adobe GoLive. Have different members of your team do the HTML layout than do the Java programming JSP encourages you to Separate the (Java) code that creates the content from the (HTML) code that presents it 2008 © Department of Information Systems - University of Information Technology 6 Advantages of JSP Over Competing Technologies (1) 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 pure servlets More convenient to create HTML Can use standard tools (e.g., DreamWeaver) Divide and conquer JSP programmers still need to know servlet programming 2008 © Department of Information Systems - University of Information Technology 7 Advantages of JSP Over Competing Technologies (2) Versus client-side JavaScript (in browser) Capabilities mostly do not overlap with JSP, but • You control server, not client • Richer language Versus server-side JavaScript (e.g., LiveWire, BroadVision) Richer language Versus static HTML Dynamic features Adding dynamic features no longer"all or nothing" decision 2008 © Department of Information Systems - University of Information Technology 8 Example <HTML> <HEAD> <TITLE>JSP Expressions</TITLE> <META NAME="keywords" CONTENT="JSP,expressions,JavaServer Pages"> <META NAME="description“ CONTENT="A quick example of JSP expressions."> <LINK REL=STYLESHEET HREF="JSP-Styles.css“ TYPE="text/css"> </HEAD> <BODY> <H2>JSP Expressions</H2> <UL> <LI>Current time: <%= new java.util.Date() %> <LI>Server: <%= application.getServerInfo() %> <LI>Session ID: <%= session.getId() %> <LI>The <CODE>testParam</CODE> form parameter: <%= request.getParameter("testParam") %> </UL> </BODY> </HTML> 2008 © Department of Information Systems - University of Information Technology 9 JSP Lifecycle 2008 © Department of Information Systems - University of Information Technology 10 JSP/Servlets in the Real World Ten most popular Web sites [1] 1) Google •Custom technology,some Java 2) Yahoo • PHP and Java 3) MySpace • ColdFusion (Java “under the hood”) 4) YouTube • Flash, Python, Java 9) Ebay •Java 10) AOL •Java Web pages using JSP [2] 568 million Most popular languages worldwide [3] Java C/C++ Visual Basic PHP Python C# [1]: reported by alexis.com, Fall 2008 [2]:reported by Google [3]: reported by tiobe.com [...]... out.println(bar()); baz(); } 2008 © Department of Information Systems - University of Information Technology 22 JSP Scriptlets Example Suppose you want to let end users customize the background color of a page What is wrong with the following code? 2008 © Department of Information Systems - University of Information Technology 23 JSP Scriptlets Example... Department of Information Systems - University of Information Technology 26 JSP/Servlet Correspondence (1) Original JSP Some Heading Better alternative: Make randomHeading a static method in a separate class 2008 © Department of Information Systems - University of Information Technology. ..Some webpages using JSP Airlines American Airlines British Airways United Airlines Financial Services Bank of America NY Stock Exchange Royal Bank of Scotland Entertainment Billboard.com WarnerBrothers.com Military and Federal Government CIA NSA Army Search/Portals Parts of Google All of Ebay Paypal 2008 © Department of Information Systems - University of Information Technology 11 Outline... (MVC) Architecture 2008 © Department of Information Systems - University of Information Technology 12 Uses of JSP Constructs Simple Application Scripting elements calling servlet code directly Scripting elements calling servlet code indirectly (by means of utility classes) Beans Servlet/JSP combo (MVC) MVC with JSP expression language Complex Application Custom tags MVC with beans, custom tags, and a framework... Department of Information Systems - University of Information Technology 18 Predefined Variables request The HttpServletRequest (1st argument to service/doGet) response The HttpServletResponse (2nd arg to service/doGet) out The Writer (a buffered version of type JspWriter) used to send output to the client session The HttpSession associated with the request (unless disabled with the session attribute of the... Department of Information Systems - University of Information Technology 28 JSP Declarations Example JSP Declarations JSP Declarations Accesses to page since server reboot: 2008 © Department of Information Systems - University. .. doSomethingWith(s); } %> Same issue with separate static methods And they are usually preferred over JSP declarations println of JSPWwriter throws IOException Use “throws IOException” for methods that use println 2008 © Department of Information Systems - University of Information Technology 32 When to use Expressions, Scriptlets or Declarations Task 1 Output a bulleted list of five... methods from init and destroy, but the standard versions of jspInit and jspDestroy are empty (placeholders for you to override) 2008 © Department of Information Systems - University of Information Technology 30 jspInit and jspDestroy Methods (2) Example The lifecycle of jsp page Showing the life cycle of jsp using jspInit and jspDestroy XML-compatible syntax Java Expression You cannot mix versions within a single page You must page use XML for entire page if you use jsp:expression • See slides at end of this lecture 2008 © Department of Information Systems - University of Information Technology 17 JSP/Servlet Correspondence Original JSP A Random Number Representative . 1 Web Programming with Java Java Server Pages Huynh Huu Viet University of Information Technology Department of Information Systems Email: viethh@uit.edu.vn 2008 © Department of Information. %> </UL> </BODY> </HTML> 2008 © Department of Information Systems - University of Information Technology 9 JSP Lifecycle 2008 © Department of Information Systems - University of Information Technology 10 JSP/Servlets. JSP programmers still need to know servlet programming 2008 © Department of Information Systems - University of Information Technology 7 Advantages of JSP Over Competing Technologies (2) Versus