+ All Categories
Home > Documents > JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the...

JSP Pages. What and Why of JSP? JSP = Java code imbedded in HTML or XML –Static portion of the...

Date post: 17-Jan-2016
Category:
Upload: randall-walker
View: 237 times
Download: 0 times
Share this document with a friend
22
JSP Pages JSP Pages
Transcript
Page 1: JSP Pages. 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.

JSP PagesJSP Pages

Page 2: JSP Pages. 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.

What and Why of JSP?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

Page 3: JSP Pages. 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.

Servlet vs. JSPServlet vs. JSP

Import java.io.*;import javax.servlet.*;import javax.servlet.http.*;publc class HtmlPage extends HttpServlet{ public void doGet(HttpServletRequest request, HttpServletResponse response) throws IOException { response.setContentType(“text/html”); PrintWriter out = response.getWriter(): String name = req.getParameter(“name”); out.Println(“<HTML>”); out.Println(“<HEAD><TITLE>First Servlet</TITLE></HEAD>”); out.Println(“<BODY>”); out.Println(“<H1>Hello “ + name + “</H1>”); out.Println(“</BODY>”); out.Println(“</HTML>”); }}

Servlet Example

Page 4: JSP Pages. 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.

Servlet vs. JSP (cont)Servlet vs. JSP (cont)

<HTML><HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY> <% String name = request.getParameter(“name”); %> <H1>Hello <%=name%> </H1> </BODY></HTML>

JSP Example

• Presentation centric• Presentation is separated from content• Easier to code• Better organization of Web application

Page 5: JSP Pages. 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.

RecommendationRecommendation

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

• Use Servlets– Validation, simple business logic– Simple/small presentation

Page 6: JSP Pages. 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.

Anatomy of a JSP PageAnatomy of a JSP Page

• Template (static HTML or XML)

• JSP Elements

• Tag libraries

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

Page 7: JSP Pages. 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.

HTML (XML) TemplateHTML (XML) Template

<HTML><HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY> <H1>Hello </H1>

</BODY></HTML>

Page 8: JSP Pages. 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.

JSP ElementsJSP ElementsDirective Elements

<%@ page info=“HomeDirectBank” %><%@ page import=“java.sql.*, java.math.*” %><%@ page isThreadSafe=“true” %><%@ page errorPage=“/homedirectbank/error.jsp” %>

<HTML><HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY> <%@ include file=“Header.jsp” %>

<H1>Hello World</H1> </BODY></HTML>

Page 9: JSP Pages. 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.

JSP ElementsJSP ElementsScripting Elements

<%! private double totalAmount; %>

<HTML><HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY> <H1>Hello World</H1> <% double amount = request.getParameter(“amt”); totalAmount += amount; %>

<P>The total amount is <% =totalAmount %> </P> </BODY></HTML>

Declarations

Scriplet

Expression

Page 10: JSP Pages. 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.

JSP ElementsJSP ElementsAction Elements

<HTML><HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY>

<jsp:usebean id=account class=model.Student scope=Session/> <jsp:include page=“/pageHeader” flush=“true” />

<% if (request.getParameter(“amount”) < 0) %> <jsp:forward page=“/errorPage2.jsp” flush=“true” /> <% else %> <jsp:setProperty name=“account” property=“balance” value=“25.32” /> <jsp:include page=“/pageFooter/” flush=“true” /> </BODY></HTML>

Include resource

Forward page

Set value of class variable in Java Bean

Page 11: JSP Pages. 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.

Accessing Servlet VariablesAccessing Servlet Variables

• config• request• response• session• out• pageContext• application• page

Page 12: JSP Pages. 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.

JSP ElementsJSP ElementsServlet Variables

<HTML><HEAD><TITLE>First Servlet</TITLE></HEAD> <BODY> <H1>Hello World</H1> <br>Date: <% out.print(new java.util.Date()); %>

<% double amount = request.getParameter(“amt”); totalAmount += amount; Double taxRate = (Double) session.getAttribute(“taxRate”); %>

<P>The total amount is <% =totalAmount %> </P> </BODY></HTML>

Page 13: JSP Pages. 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.

Simplify JSP DevelopmentSimplify JSP Development

Use Java Beans

Use Tag Libraries

Page 14: JSP Pages. 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.

Model/View/Controller

JavaBean

Request object

JSP/Servlets in the EnterpriseJSP/Servlets in the Enterprise

Servlet

JSP page

Database

WebServer

Model One

Architecture

Output

doGet/doPostInput

<<forward>> <<creates>>

<<uses>>

Page 15: JSP Pages. 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.

Using Java Beans in JSPUsing Java Beans in JSPModel 1Model 1

<HTML><HEAD><TITLE>JSP Page 2</TITLE></HEAD> <BODY> ….

<jsp:useBean id=“employee” class=“javaBeans.Employee” scope=“request” /> ....

<br>lastname = <jsp:getProperty name=“employee” property=“lastName” /> <br>firstname = <jsp:getProperty name=“employee” property=“firstName” />

<br>lastname = <%= employee.getLastName() %> <br>firstname = <%= employee.getFirstName() %>

…. </BODY></HTML>

Getting values from a java bean

Get Java Bean

Reference Java Bean class variables

Page 16: JSP Pages. 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.

Using Java Beans in JSPUsing Java Beans in JSPModel 1Model 1

<HTML><HEAD><TITLE>JSP Page 1</TITLE></HEAD> <BODY> ….

<jsp:useBean id=“customer1” class=“control.Customer” scope=“request”> <jsp:setProperty name=“customer1” property=“lastName” value=“Flintstone” /> <jsp:setProperty name=“customer1” property=“firstName” value=“Wilma” /> <% customer1.setUserid(“flintstonew”); %> <% customer1.setPassword(“dino”); %> </jsp:useBean>

….

<jsp:forward page=”/jspPage2” />”/>

…. </BODY></HTML>

Creating a java bean and setting values in the java bean

CreateJava Bean

Forward request tonext JSP page

Page 17: JSP Pages. 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.

Using Java Beans in JSPUsing Java Beans in JSPModel 1Model 1

<HTML><HEAD><TITLE>JSP Page 2</TITLE></HEAD> <BODY> ….

<jsp:useBean id=“customer1” class=“control.Customer” scope=“request” /> ....

<br>Last name = <jsp:getProperty name=“customer1” property=“lastName” /> <br>first name = <jsp:getProperty name=“customer1” property=“firstName” /> <br>Username = <% customer1.getUserid(); %> <br>Password = <% customer1.getPassword(); %>

…. </BODY></HTML>

Getting values from a java bean

Get Java Bean

Reference Java Bean class variables

Page 18: JSP Pages. 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.

Tag LibrariesTag 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

Page 19: JSP Pages. 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.

Tag TypesTag Types

• XML format– Tag without a body

<rkjTagLib:deptHeader/>

– Tag without a body but with an attribute

<rkjTagLib:table rowcount=5 colcount=3 />

– Tag with body and an attribute

<rkjTagLib:table rowcount=5 colcount=3 > Title of Table </rkjTagLIb:table>

Page 20: JSP Pages. 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.

Tag Handler ClassTag Handler Classimport java.io.*;import java.servlet.jsp.*;import java.servlet.jsp.tagext.*;

public class DeptHeader extends TagSupport{ public int doStartTag() { try { JspWriter out = pageContext.getOut(); out.println(“<H2>Information Systems Dept.</H1>”); out.println(“<H3>Brigham Young University-Idaho </H3>”); } catch (IOException ioex) {

…. }

return (SKIP_BODY); }

public int doEndTag() { return(EVAL_PAGE); }}

Inherit TagSupport

Invoked at starting tag

Invoked at ending tag

Page 21: JSP Pages. 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.

Tag Library DescriptorTag 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>homeDirectBank</shortname>

<tag> <name>deptHeader</name> <tagclass>homedirectbank.DeptHeader</tagclass> <bodycontent>EMPTY</bodycontent> <info>Inserts IS department header</info> </tag>

</taglib>

Page 22: JSP Pages. 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.

Using Tag in JSP PageUsing Tag in JSP Page<taglib> <tlibversion>1.1</tlibversion> <jspversion>1.2</jspversion> <shortname>homeDirectBank</shortname>

<tag> <name>deptHeader</name> <tagclass>com.taglib.homedirectbank.DeptHeader</tagclass> <bodycontent>EMPTY</bodycontent> <info>Inserts IS department header</info> </tag>

</taglib>

Tag Library Descriptor (homeDrectBank)

<%@ taglib uri=“/homeDirectBank” prefix=“utils”><HTML><HEAD><TITLE>Test Servlet</TITLE></HEAD> <BODY> <utils:deptHeader /> ….. …..</BODY></HTML>

}

JSP Page

import java.io.*;import java.servlet.jsp.*;import java.servlet.jsp.tagext.*;

public class DepHeader extends TagSupport{ public int doStartTag() { try { JspWriter out = pageContext.getOut(); out.println(“<H2>Information Systems Dept.</H1>”); out.println(“<H3>Brigham Young University-Idaho </H3>”); } catch (IOException ioex) {

…. }

return (SKIP_BODY); }

public int doEndTag() { return(EVAL_PAGE); }}

Tag Handler Class

maps

uses


Recommended