110 likes | 244 Views
JSP Example and Best Practices. Minpo , Jung. Web Application Architecture. Patterns for Web Application Development. Servlet model. Moving to JSP Model 1. Moving to JSP Model 2. JSP Processing. Structure of a JSP Page. JSP page = directives + declarations + expressions + scriptlets.
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 intgetAccountBalance() { return balance; } %> <jsp:declaration> int balance = 0; </jsp:declaration> <jsp:declaration> public intgetAccountBalance() { return balance; } </jsp:declaration>
Expressions • Hello, my name is <%= getName() %>. How are you? • out.println("Hello, my name is "+ getName() + ". How are you?"); • <% StringBuffersb = 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>