JSP Pages What and Why of JSP JSP

  • Slides: 22
Download presentation
JSP Pages

JSP Pages

What and Why of JSP? • JSP = Java code imbedded in HTML or

What and Why of JSP? • JSP = Java code imbedded in HTML or XML – Static portion of the page is HTML – Dynamic portion is Java • Easy way to develop and maintain dynamic web pages and dynamic XML documents

Servlet vs. JSP Servlet Example Import java. io. *; import javax. servlet. http. *;

Servlet vs. JSP Servlet Example Import java. io. *; import javax. servlet. http. *; publc class Html. Page extends Http. Servlet { public void do. Get(Http. Servlet. Request request, Http. Servlet. Response response) throws IOException { response. set. Content. Type(“text/html”); Print. Writer out = response. get. Writer(): String name = req. get. Parameter(“name”); out. Println(“<HTML>”); out. Println(“<HEAD><TITLE>First Servlet</TITLE></HEAD>”); out. Println(“<BODY>”); out. Println(“<H 1>Hello “ + name + “</H 1>”); out. Println(“</BODY>”); out. Println(“</HTML>”); } }

Servlet vs. JSP (cont) JSP Example <HTML> <HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY> <% String name =

Servlet vs. JSP (cont) JSP Example <HTML> <HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY> <% String name = request. get. Parameter(“name”); %> <H 1>Hello <%=name%> </H 1> </BODY> </HTML> • • Presentation centric Presentation is separated from content Easier to code Better organization of Web application

Recommendation • Use JSP – If presentation changes frequently – Presentation is complex •

Recommendation • Use JSP – If presentation changes frequently – Presentation is complex • Use Servlets – Validation, simple business logic – Simple/small presentation

Anatomy of a JSP Page • Template (static HTML or XML) • JSP Elements

Anatomy of a JSP Page • Template (static HTML or XML) • JSP Elements Element Type JSP Syntax Description Directives <%@ directive_name%> Controls to define translation into Java code Scripting <% …………… %> Embed Java code in HTML Actions <jsp: …………. > JSP-specific tag for Java Beans • Tag libraries

HTML (XML) Template <HTML> <HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY> <H 1>Hello </BODY> </HTML> </H 1>

HTML (XML) Template <HTML> <HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY> <H 1>Hello </BODY> </HTML> </H 1>

JSP Elements Directive Elements <%@ page info=“Home. Direct. Bank” %> <%@ page import=“java. sql.

JSP Elements Directive Elements <%@ page info=“Home. Direct. Bank” %> <%@ page import=“java. sql. *, java. math. *” %> <%@ page is. Thread. Safe=“true” %> <%@ page error. Page=“/homedirectbank/error. jsp” %> <HTML> <HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY> <%@ include file=“Header. jsp” %> <H 1>Hello World</H 1> </BODY> </HTML>

JSP Elements Scripting Elements <%! private double total. Amount; %> <HTML> <HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY>

JSP Elements Scripting Elements <%! private double total. Amount; %> <HTML> <HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY> <H 1>Hello World</H 1> <% double amount = request. get. Parameter(“amt”); total. Amount += amount; %> <P>The total amount is <% =total. Amount %> </P> </BODY> </HTML> Declarations Scriplet Expression

JSP Elements Action Elements <HTML> <HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY> <jsp: usebean id=account class=model. Student scope=Session/>

JSP Elements Action Elements <HTML> <HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY> <jsp: usebean id=account class=model. Student scope=Session/> <jsp: include page=“/page. Header” flush=“true” /> Include resource <% if (request. get. Parameter(“amount”) < 0) %> <jsp: forward page=“/error. Page 2. jsp” flush=“true” /> Forward page <% else %> <jsp: set. Property name=“account” property=“balance” value=“ 25. 32” /> <jsp: include page=“/page. Footer/” flush=“true” /> </BODY> Set value of class </HTML> variable in Java Bean

Accessing Servlet Variables • • config request response session out page. Context application page

Accessing Servlet Variables • • config request response session out page. Context application page

JSP Elements Servlet Variables <HTML> <HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY> <H 1>Hello World</H 1> Date: <%

JSP Elements Servlet Variables <HTML> <HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY> <H 1>Hello World</H 1> Date: <% out. print(new java. util. Date()); %> <% double amount = request. get. Parameter(“amt”); total. Amount += amount; Double tax. Rate = (Double) session. get. Attribute(“tax. Rate”); %> <P>The total amount is <% =total. Amount %> </P> </BODY> </HTML>

Simplify JSP Development Use Java Beans Use Tag Libraries

Simplify JSP Development Use Java Beans Use Tag Libraries

JSP/Servlets in the Enterprise Model One Architecture Model/View/Controller JSP page Output Web Server <<uses>>

JSP/Servlets in the Enterprise Model One Architecture Model/View/Controller JSP page Output Web Server <<uses>> Request object Java. Bean <<forward>> Input do. Get/ do. Post <<creates>> Servlet Data base

Using Java Beans in JSP Model 1 Getting values from a java bean <HTML>

Using Java Beans in JSP Model 1 Getting values from a java bean <HTML> <HEAD><TITLE>JSP Page 2</TITLE></HEAD> <BODY> …. <jsp: use. Bean id=“employee” class=“java. Beans. Employee” scope=“request” />. . lastname = <jsp: get. Property name=“employee” property=“last. Name” /> firstname = <jsp: get. Property name=“employee” property=“first. Name” /> lastname = <%= employee. get. Last. Name() %> firstname = <%= employee. get. First. Name() %> …. </BODY> </HTML> Get Java Bean Reference Java Bean class variables

Using Java Beans in JSP Model 1 Creating a java bean and setting values

Using Java Beans in JSP Model 1 Creating a java bean and setting values in the java bean <HTML> <HEAD><TITLE>JSP Page 1</TITLE></HEAD> <BODY> …. <jsp: use. Bean id=“customer 1” class=“control. Customer” scope=“request”> <jsp: set. Property name=“customer 1” property=“last. Name” value=“Flintstone” /> <jsp: set. Property name=“customer 1” property=“first. Name” value=“Wilma” /> <% customer 1. set. Userid(“flintstonew”); %> <% customer 1. set. Password(“dino”); %> </jsp: use. Bean> …. <jsp: forward page=”/jsp. Page 2” />”/> …. </BODY> </HTML> Create Java Bean Forward request to next JSP page

Using Java Beans in JSP Model 1 Getting values from a java bean <HTML>

Using Java Beans in JSP Model 1 Getting values from a java bean <HTML> <HEAD><TITLE>JSP Page 2</TITLE></HEAD> <BODY> …. <jsp: use. Bean id=“customer 1” class=“control. Customer” scope=“request” />. . Get Java Bean Last name = <jsp: get. Property name=“customer 1” property=“last. Name” /> first name = <jsp: get. Property name=“customer 1” property=“first. Name” /> Username = <% customer 1. get. Userid(); %> Password = <% customer 1. get. Password(); %> Reference Java Bean class variables …. </BODY> </HTML>

Tag Libraries • Create custom XML tags that you can imbed in JSP pages

Tag Libraries • Create custom XML tags that you can imbed in JSP pages – Custom commands (i. e. , macros) – Java tag handler class defined for each custom tag – XML tag in JSP Java method called for tag

Tag Types • XML format – Tag without a body <rkj. Tag. Lib: dept.

Tag Types • XML format – Tag without a body <rkj. Tag. Lib: dept. Header/> – Tag without a body but with an attribute <rkj. Tag. Lib: table rowcount=5 colcount=3 /> – Tag with body and an attribute <rkj. Tag. Lib: table rowcount=5 colcount=3 > Title of Table </rkj. Tag. LIb: table>

Tag Handler Class import java. io. *; import java. servlet. jsp. tagext. *; public

Tag Handler Class import java. io. *; import java. servlet. jsp. tagext. *; public class Dept. Header extends Tag. Support { public int do. Start. Tag() { try { Jsp. Writer out = page. Context. get. Out(); out. println(“<H 2>Information Systems Dept. </H 1>”); out. println(“<H 3>Brigham Young University-Idaho </H 3>”); } catch (IOException ioex) { …. } Inherit Tag. Support Invoked at starting tag return (SKIP_BODY); } public int do. End. Tag() { return(EVAL_PAGE); } } Invoked at ending tag

Tag Library Descriptor <? xml version=“ 1. 0” encoding=“ISO-8859 -1” ? > <!DOCTYPE taglib

Tag Library Descriptor <? xml version=“ 1. 0” encoding=“ISO-8859 -1” ? > <!DOCTYPE taglib PUBLIC “-//Sun Microsystems, Inc. //DTD JSP Tag Library 1. 2//EN” http: //java. sun. com/dtd/web-jsptaglibrary_1_2. dtd> <taglib> <tlib-version>1. 0</tlib-version>> <jspversion>1. 2</jspversion> <shortname>home. Direct. Bank</shortname> <tag> <name>dept. Header</name> <tagclass>homedirectbank. Dept. Header</tagclass> <bodycontent>EMPTY</bodycontent> <info>Inserts IS department header</info> </taglib>

Using Tag in JSP Page Tag Library Descriptor (home. Drect. Bank) <taglib> <tlibversion>1. 1</tlibversion>

Using Tag in JSP Page Tag Library Descriptor (home. Drect. Bank) <taglib> <tlibversion>1. 1</tlibversion> <jspversion>1. 2</jspversion> <shortname>home. Direct. Bank</shortname> <tag> <name>dept. Header</name> <tagclass>com. taglib. homedirectbank. Dept. Header</tagclass> <bodycontent>EMPTY</bodycontent> <info>Inserts IS department header</info> </taglib> JSP Page uses <%@ taglib uri=“/home. Direct. Bank” prefix=“utils”> <HTML> <HEAD><TITLE>Test Servlet</TITLE></HEAD> <BODY> <utils: dept. Header /> …. . </BODY> </HTML> } Tag Handler Class import java. io. *; import java. servlet. jsp. tagext. *; maps public class Dep. Header extends Tag. Support { public int do. Start. Tag() { try { Jsp. Writer out = page. Context. get. Out(); out. println(“<H 2>Information Systems Dept. </H 1>”); out. println(“<H 3>Brigham Young University-Idaho </H 3>”); } catch (IOException ioex) { …. } return (SKIP_BODY); } public int do. End. Tag() { return(EVAL_PAGE); } }