110 likes | 244 Views
This document outlines the essential structures and best practices for developing web applications using JavaServer Pages (JSP). It covers crucial components, including directives, declarations, expressions, and scriptlets, providing examples to illustrate each concept. The document emphasizes the utilization of directives to organize JSP pages effectively and includes practical examples of how to implement them in a web application context. Learn how to structure your JSP pages for optimal performance and maintainability in web application development.
E N D
JSP Example and Best Practices Minpo, Jung
Patterns for Web Application Development Servlet model
Structure of a JSP Page • JSP page = directives + declarations + expressions + scriptlets
Directives • An example of the page directive <%@ page import="java.util.Date, java.io.*“ extends="myJSPpage“ buffer="32k" autoflush="false" %> <jsp:directive.page import="java.util.Date, java.io.* " extends="myJSPpage" buffer="32k" autoflush="false" /> <%@ include file="/legal/disclaimer.html"> <jsp:directive.include file="/templates/footer.html" />
Declarations <%! int balance = 0; %> <%! public int getAccountBalance() { return balance; } %> <jsp:declaration> int balance = 0; </jsp:declaration> <jsp:declaration> public int getAccountBalance() { return balance; } </jsp:declaration>
Expressions • Hello, my name is <%= getName() %>. How are you? • out.println("Hello, my name is "+ getName() + ". How are you?"); • <% StringBuffer sb = new StringBuffer(); • sb.append("Hello, my name is ""); • sb.append(getName()); • sb.append(". How are you?); • out.println(sb.toString()); • %> <jsp:expression></jsp:expression>
Scriptlets • <% for (int n=0; n<10; n++) { • out.println(n); • out.println("<br>"); // Line break • } • %> • <jsp:scriptlet> • for (int n=0; n<10; n++) { • out.println(n); • out.println("<br>"); // Line break • } • </jsp:scriptlet>