250 likes | 406 Views
Basic JSP. Celsina Bignoli bignolic@smccd.net. Problems with Servlets. Servlets contain request processing , business logic response generation all lumped together! Problems: good Java knowledge is needed to develop and maintain the application
E N D
Basic JSP Celsina Bignoli bignolic@smccd.net
Problems with Servlets • Servlets contain • request processing, • business logic • response generation all lumped together! • Problems: • good Java knowledge is needed to develop and maintain the application • changing look and feel or a new client type require changes to the Servlet code • cannot use web page development tools
Advantages of JSP • separates request processing and business logic from the presentation • places all static HTML in a JSP page and adds a few JSP elements to generate the dynamic content • allows the separation of tasks between Java programmers and WEB page authors • makes it easier to change presentation without affecting business layer and viceversa
<% Date d=new Date(); String today = DateFormat.getInstance().format(d); %> A Simple JSP Page JSP tag <%@ page import=“java.text.*, java.util.*”%> <html> HTML <head><title><Date Example</title></head> <body> Scriptlet Today is: <em><%=today%></em> </body> </html> Example.jsp • Static: HTML/XML elements • Dynamic: scriptlets • Special JSP elements
JSP Processing- Translation Phase request response JSP Page (.jsp) JSP Translator Text Buffer Java Compiler Servlet Source (.java) Servlet Class (.class) JVM Servlet Container Translation Phase Request Processing Phase
JSP Processing- Request Processing Phase request response JSP Page (.jsp) JSP Translator Text Buffer Java Compiler Servlet Source (.java) Servlet Class (.class) JVM Servlet Container Translation Phase Request Processing Phase
JSP Translation - Example package jsp; import javax.servlet.*; import javax.servlet.http.*; … import java.text.*; import java.util.*; public class example_jsp extends org.apache.jasper.runtime.HttpJspBase { … } example_jsp.java extends: javax.servlet.http.HttpServlet
service() method - Example public void _jspService(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { … try { out = pageContext.getOut(); out.write("<html>\r\n"); out.write("<head><title><Date Example</title></head>\r\n"); out.write("<body>\r\n"); Date d=new Date(); String today = DateFormat.getInstance().format(d); out.write("\r\n"); out.write("Today is:\r\n"); out.write("<em>"); out.print(today); out.write("</em>\r\n"); out.write("</body>\r\n"); out.write("</html>"); } … }
Directives Comments Actions standard actions custom actions and JSTL Expression Language (EL) Scripting JavaBean components JSP Elements
Specify attribute of the page itself type of content buffering requirements resources used how runtime errors should be handled Do not affect content of a response but how the container should handle it. Enclosed between <%@ and and %> Have a name and one or more attribute/value pairs attribute and values are case-sensitive values must be enclosed in single or double quotes JSP Directive Elements
Page Directive <%@page contentType=“text/html” %> • other possible values for contentType • text/plain • text/xml • text/vnd.wap.wml • other possible attributes • errorPage • isErrorPage • session • pageEncoding • buffer • autoFlush
Taglib Directive <%@taglib prefix=“c” uri=“http://java.sun.com/jsp/jstl/core” %> • declares a JSTL custom tag library used in the page • the prefix is the name used for the library in the JSP page • the uri uniquely identifies the library
JSP Comments <%-- comment goes here --%> • delimited by <%-- and --%> • ignored when the page is processed • never sent to the browser
JSP Actions • represent dynamic actions to be performed at runtime <prefix: action-name attr1=“value1” attr2=“value2”> action_body </brefix:action-name> • or <prefix: action-name attr1=“value1” attr2=“value2”> • allows dor actions in different libraries to have the same name • allow contaienr to determine which library an action belongs
Standard Actions <isp:useBean> makes a java bean component available to a page <jsp:getProperty> gets a property value from a JavaBean components and adds it to the response <jsp:setProperty> Set a JavaBean property value <jsp:include> Include the response from a servlet or JSP page during the request processing phase <jsp:forward> Forwards the processing of a request to a servlet or JSP page
Standard Actions (2) <isp:param> adds a parameter to a request handed over to another servlet or JSP page <jsp:plugin> Used to run applets on the browser <jsp:attribute> Set the value of an action attribute <jsp:body> Sets the action element body <jsp:element> Dynamically generate an XML element <jsp:text> to encapsulate text “verbatim”
JSP Standard Tag Library (JSTL) • group of libraries each containing related actions • Core • XML processing • Internationalization • Relational Database Access • Functions
JSP Expression Language • based on both ECMAScript and XPath • built-in support for JavaBean access and manipulation, collection of objects, automatic type conversion etc… • EL expressions enclosed within ${ and } • ${anObject.aProperty} • <c:if test=“${user.salary > 10000}”> … </C:if>
Scripting Elements • fragments of java code embedded in a JSP page • Declarations • Expressions • Scriptlets • heavily used in early JSP pages but rarely in newer developments (and discouraged)
Declarations • used to insert methods, constants and variable declarations in JSP pages <%! private static String EXAMPLE=“/example2”; private static String SHOP_PAGE=“/estore.jsp”; private static String CART_PAGE=“/shopcart.jsp”; private String dispPrice(String price) { // method body } %>
Expressions • an embedded Java expression that is evaluated and converted to a text String • The text string is placed in the JSP output at the location in which the element appears <table> <tr> <td> <%= curItem.getName() %></td> <td> <%= String.valueOf(dispPrice(curItem.getPrice())) %> </td> </tr> </table>
Scriptlets • used to include complete fragments of java code in a JSP page • cau use the out implicit object (of type javax.servlet.jsp.JspWriter) to write output <% if (state.isLocal()) out.print(totalCost * localTax) else out.print(totalCost) %>
Problems with Scriptlets • discouraged since they do not promote separation of presentation from data/logic • look as a Servlet inside-out • make JSP page as hard to write/maintain as corresponding Servlet
Model-View-Controller Design • Separation of • data and business logic (Model) • data presentation (View) • data interaction (Controller) • The user interacts with the Controller to ask for things to be done. • Controller forward the request to the Model • the result of the request is displayed by the View
Model-View-Controller Design Controller (Servlet) Request Data Model (JavaBean) Response View (JSP) Data Data Tier JSP/Servlet Container Browser