basic jsp celsina bignoli [email protected]. problems with servlets servlets contain –request...

25
Basic JSP Celsina Bignoli [email protected]

Upload: cornelius-garrett

Post on 19-Jan-2016

215 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

Basic JSP

Celsina [email protected]

Page 2: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

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

Page 3: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

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

Page 4: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

A Simple JSP Page

• Static: HTML/XML elements• Dynamic: scriptlets

• Special JSP elements

Example.jsp

<%@ page import=“java.text.*, java.util.*”%>

<head><title><Date Example</title></head><html>

<%Date d=new Date();String today = DateFormat.getInstance().format(d);

%>Today is:<em><%=today%></em></body></html>

HTML

Scriptlet

<body>

JSP tag

Page 5: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

JSP Processing- Translation Phase

Servlet Container

JSPPage(.jsp)

JSPPage(.jsp)

ServletSource(.java)

ServletSource(.java)

JSPTranslator

ServletClass(.class

)

ServletClass(.class

)

JavaCompiler

JVM

Translation Phase

Request Processing Phase

TextBuffer

TextBuffer

request response

Page 6: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

JSP Processing- Request Processing Phase

Servlet Container

JSPPage(.jsp)

JSPPage(.jsp)

ServletSource(.java)

ServletSource(.java)

JSPTranslator

ServletClass(.class

)

ServletClass(.class

)

JavaCompiler

JVM

Translation Phase

Request Processing Phase

TextBuffer

TextBuffer

request response

Page 7: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

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

JSP Translation - Example

Page 8: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

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

service() method - Example

Page 9: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

JSP Elements

• Directives• Comments• Actions

– standard actions– custom actions and JSTL

• Expression Language (EL)• Scripting• JavaBean components

Page 10: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

JSP Directive 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

Page 11: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

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

Page 12: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

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

Page 13: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

JSP Comments

<%-- comment goes here --%>

• delimited by <%-- and --%>

• ignored when the page is processed

• never sent to the browser

Page 14: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

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

Page 15: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

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

Page 16: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

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”

Page 17: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

JSP Standard Tag Library (JSTL)

• group of libraries each containing related actions• Core• XML processing• Internationalization• Relational Database Access• Functions

Page 18: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

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>

Page 19: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

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)

Page 20: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

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

}

%>

Page 21: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

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>

Page 22: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

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)

%>

Page 23: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

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

Page 24: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

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

Page 25: Basic JSP Celsina Bignoli bignolic@smccd.net. Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped

Model-View-Controller Design

Controller(Servlet)

View(JSP)

Model(JavaBean)

Data

Data

Data TierJSP/Servlet Container

Request

Response

Browser