1 / 25

Basic JSP

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

saima
Download Presentation

Basic JSP

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. Basic JSP Celsina Bignoli bignolic@smccd.net

  2. 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

  3. 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

  4. <% 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

  5. 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

  6. 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

  7. 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

  8. 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>"); } … }

  9. Directives Comments Actions standard actions custom actions and JSTL Expression Language (EL) Scripting JavaBean components JSP Elements

  10. 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

  11. 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

  12. 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

  13. JSP Comments <%-- comment goes here --%> • delimited by <%-- and --%> • ignored when the page is processed • never sent to the browser

  14. 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

  15. 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

  16. 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”

  17. JSP Standard Tag Library (JSTL) • group of libraries each containing related actions • Core • XML processing • Internationalization • Relational Database Access • Functions

  18. 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>

  19. 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)

  20. 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 } %>

  21. 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>

  22. 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) %>

  23. 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

  24. 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

  25. Model-View-Controller Design Controller (Servlet) Request Data Model (JavaBean) Response View (JSP) Data Data Tier JSP/Servlet Container Browser

More Related