Java Server Pages V3.1 Workshop Solutions

  • Uploaded by: Muhammad Imran Saeed
  • 0
  • 0
  • June 2020
  • PDF

This document was uploaded by user and they confirmed that they have the permission to share it. If you are author or own the copyright of this book, please report to us by using this DMCA report form. Report DMCA


Overview

Download & View Java Server Pages V3.1 Workshop Solutions as PDF for free.

More details

  • Words: 2,672
  • Pages:
JavaServer Pages

Solutions to Workshops

Java Servlets: Solutions to Workshops

Page i

Contents

Lesson 2: Using Basic JSP Constructs Workshop...........................................................................1 Lesson 4: Include Files in JSP Workshop ........................................................................................4 Lesson 5: Using JavaBeans with JSPs Workshop...........................................................................6 Lesson 7: Custom Tag Library Basics Workshops ..........................................................................9 Lesson 8: Adding Attributes to Custom Tag Workshop .................................................................11 Lesson 8: Using PageContext Workshop ......................................................................................14 Lesson 9: Using Body Content Workshop .....................................................................................17

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 1

Lesson 2: Using Basic JSP Constructs Workshop

Welcome.jsp <TITLE>Welcome

Welcome <%= request.getParameter("userName") %>!

We are pleased to have you as a customer!

Here is the information you entered:



© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 2

<% String strCardNum = request.getParameter("cardNumber"); if(strCardNum.length() > 0) { %> <% } else { %> <% } %>
Name: <%= request.getParameter("userName") %>
Street address: <%= request.getParameter("street") %>
City: <%= request.getParameter("city") %>
State: <%= request.getParameter("state") %>
Zip: <%= request.getParameter("zip") %>
Credit Card #: <%= strCardNum %>
Note: Since you did not enter a credit card number, all orders must be paid in cash on the barrelhead!



register.html <TITLE>Customer Registration

Customer Registration

Please enter the following information so we can process your order:



© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 3

Name:
Street address:
City:
State:
Zip:
Credit Card #:


web.xml <web-app>

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 4

Lesson 4: Include Files in JSP Workshop

CompanyHeader.jsp

StudentSoft Online SuperStore

If we don't have it, you don't need it!




Welcome.jsp <TITLE>Welcome <%@ include file="CompanyHeader.jsp" %>

Welcome <%= request.getParameter("userName") %>!

We are pleased to have you as a customer!

Here is the information you entered:



© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 5

<% String strCardNum = request.getParameter("cardNumber"); if(strCardNum.length() > 0) { %> <% } else { %> <% } %>
Name: <%= request.getParameter("userName") %>
Street address: <%= request.getParameter("street") %>
City: <%= request.getParameter("city") %>
State: <%= request.getParameter("state") %>
Zip: <%= request.getParameter("zip") %>
Credit Card #: <%= strCardNum %>
<jsp:include page="NoCreditCard.html" flush="true" />



NoCreditCard.html Note: Since you did not enter a credit card number, all orders must be paid in cash on the barrelhead!

And we mean it!!!

Register.html same as before

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 6

Lesson 5: Using JavaBeans with JSPs Workshop

CustomerBean.java /* Customer String String String String String String

Bean with these read/write properties: userName street city state zip cardNumber

*/ package labs.jspbeans; public class CustomerBean { private String userName private String street private String city private String state private String zip private String cardNumber

= = = = = =

""; ""; ""; ""; ""; "";

//================================================ // String userName property //================================================ public String getUserName() { return userName; } public void setUserName(String value) { userName = value; } //================================================ // String street property //================================================ public String getStreet() { return street; } public void setStreet(String value) { street = value; }

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 7

//================================================ // String city property //================================================ public String getCity() { return city; } public void setCity(String value) { city = value; } //================================================ // String state property //================================================ public String getState() { return state; } public void setState(String value) { state = value; } //================================================ // String zip property //================================================ public String getZip() { return zip; } public void setZip(String value) { zip = value; } //================================================ // String cardNumber property //================================================ public String getCardNumber() { return cardNumber; } public void setCardNumber(String value) { cardNumber = value; } }

Welcome.jsp <TITLE>Welcome <jsp:useBean id="customer" class="labs.jspbeans.CustomerBean" scope="session"> <jsp:setProperty name="customer" property="*" /> <%@ include file="CompanyHeader.jsp" %>

Welcome <jsp:getProperty name="customer" property="userName" />!

We are pleased to have you as a customer!

Here is the information you entered:



© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 8

property="street" />

property="city" />

property="state" />

property="zip" />

<% String strCardNum = customer.getCardNumber(); if(strCardNum.length() > 0) { %> <% } else { %> <% } %>
Name: <jsp:getProperty name="customer" property="userName" />
Street address: <jsp:getProperty name="customer"
City: <jsp:getProperty name="customer"
State: <jsp:getProperty name="customer"
Zip: <jsp:getProperty name="customer"
Credit Card #: <jsp:getProperty name="customer" property="cardNumber" />
<jsp:include page="NoCreditCard.html" flush="true" />



Unchanged members CompanyHeader.jsp Register.html NoCreditCard.html

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 9

Lesson 7: Custom Tag Library Basics Workshops

CounterTest.jsp A Hit Count Tag --> <%@ taglib uri='ctlbasic' prefix='basic' %> This page has been accessed times!



ctlbasic.tld 1.0 <jspversion>1.1 <shortname>JSP course labs Various tags for CTL labs hitcount ctlbasic.HitCountTag empty

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 10

HitCountTag.java package ctlbasic; import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspWriter; import javax.servlet.jsp.tagext.TagSupport; public class HitCountTag extends TagSupport { private static int _iInstCount; private static int _iCount; public HitCountTag() { System.out.println("Instantiating CounterTag #" + ++_iInstCount); } public int doStartTag() throws JspException { _iCount++; System.out.println("Incrementing counter to " + _iCount); try { JspWriter out = pageContext.getOut(); out.print(_iCount); } catch(java.io.IOException ex) { throw new JspException(ex.getMessage()); } return SKIP_BODY; } }

web.xml <web-app> ctlbasic /WEB-INF/tlds/ctlbasic.tld

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 11

Lesson 8: Adding Attributes to Custom Tag Workshop

CounterTest.jsp A Hit Count Tag <%@ taglib uri='ctladvanced1' prefix='adv1' %> Count for Huey (reset=true):

Count for Dewey (explicit reset=false):

Count for Louie (default reset):

Count for (parameter):



ctladvanced1.tld 1.0 <jspversion>1.1 <shortname>JSP course labs Various tags for CTL labs hitcount ctladvanced1.HitCountTag

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 12

empty name <required>true true reset <required>false false


HitCountTag.java package ctladvanced1; import java.util.Hashtable; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.TagSupport; public class HitCountTag extends TagSupport { private static int _iInstCount; // private static int _iCount; private static Hashtable _htCounters = new Hashtable(); private String _strName; private boolean _bReset = false; public HitCountTag() { System.out.println("Instantiating CounterTag #" + ++_iInstCount); } public void setPageContext(PageContext pc) { super.setPageContext(pc); _strName = null; _bReset = false; } // Name attribute public void setName(String value) { _strName = value; } public void setReset(String value) { _bReset = value.toUpperCase().equals("TRUE"); } public int doStartTag() throws JspException { // Get initial count

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 13

int iCount = 0; if(! _bReset && _htCounters.containsKey(_strName)) { Integer intCount = (Integer) _htCounters.get(_strName); iCount = intCount.intValue(); } iCount++; try { JspWriter out = pageContext.getOut(); out.print(iCount); // Put new count back into hashtable Integer intCount = new Integer(iCount); _htCounters.put(_strName, intCount); } catch(java.io.IOException ex) { throw new JspException(ex.getMessage()); } return SKIP_BODY; } }

web.xml <web-app> ctlbasic /WEB-INF/tlds/ctlbasic.tld ctladvanced1 /WEB-INF/tlds/ctladvanced1.tld

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 14

Lesson 8: Using PageContext Workshop

CounterTest.jsp A Hit Count Tag <%@ taglib uri='ctladvanced2' prefix='adv2' %> <% String strName = request.getParameter("name"); if(strName == null) strName = "No name"; %> Count for Huey (reset=true):

Count for Dewey (explicit reset=false):

Count for Louie (default reset):

Count for <%= strName %> (parameter):



ctladvanced2.tld 1.0 <jspversion>1.1 <shortname>JSP course labs

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 15

Various tags for CTL labs hitcount ctladvanced2.HitCountTag empty name <required>true true reset <required>false false


HitCountTag.java package ctladvanced2; //import java.util.Hashtable; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.TagSupport; public class HitCountTag extends TagSupport { private static int _iInstCount; // private static int _iCount; // private static Hashtable _htCounters = new Hashtable(); private String _strName; private boolean _bReset = false; public HitCountTag() { System.out.println("Instantiating CounterTag #" + ++_iInstCount); } public void setPageContext(PageContext pc) { super.setPageContext(pc); _strName = null; _bReset = false; } // Name attribute public void setName(String value) { _strName = value; } public void setReset(String value) { _bReset = value.toUpperCase().equals("TRUE"); }

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 16

public int doStartTag() throws JspException { // Get initial count int iCount = 0; Integer intCount = (Integer) pageContext.getAttribute(_strName, PageContext.APPLICATION_SCOPE); if(! _bReset && intCount != null) { Integer intCount = (Integer) _htCounters.get(_strName); iCount = intCount.intValue(); }

//

iCount++; try { JspWriter out = pageContext.getOut(); out.print(iCount); // Put new count back into hashtable intCount = new Integer(iCount); pageContext.setAttribute(_strName, intCount, PageContext.APPLICATION_SCOPE); } catch(java.io.IOException ex) { throw new JspException(ex.getMessage()); } return SKIP_BODY; } }

web.xml < <web-app> ctlbasic /WEB-INF/tlds/ctlbasic.tld ctladvanced1 /WEB-INF/tlds/ctladvanced1.tld ctladvanced2 /WEB-INF/tlds/ctladvanced2.tld

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 17

Lesson 9: Using Body Content Workshop

CompanyBean.java /* Company Bean with these read/write properties: String name String motto */ package ctlbody; public class CompanyBean { private String name = ""; private String motto = ""; //================================================ // String name property //================================================ public String getName() { return name; } public void setName(String value) { name = value; } //================================================ // String motto property //================================================ public String getMotto() { return motto; } public void setMotto(String value) { motto = value; } public String toString() { return "Name: " + name + '\n' + "Motto: " + motto + '\n'; } }

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 18

CompanyHeader.jsp <%@ taglib uri='ctlbody' prefix='bod' %> Dorothy Com's Online Store The lowest prices in Cyberspace! <jsp:useBean id="companyBean" class="ctlbody.CompanyBean" scope="application" />

<jsp:getProperty name="companyBean" property="name" />

<jsp:getProperty name="companyBean" property="motto" />




CompanyTag.java package ctlbody; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.TagSupport; public class CompanyTag extends TagSupport { private static boolean _bCreatedBean = false; public static final String BEAN_NAME = "companyBean"; public int doStartTag() throws JspException { if(_bCreatedBean) return SKIP_BODY; else { CompanyBean company = new CompanyBean(); pageContext.setAttribute(BEAN_NAME, company, PageContext.APPLICATION_SCOPE); return EVAL_BODY_INCLUDE; } } public int doEndTag() throws JspException { _bCreatedBean = true; return EVAL_PAGE; } }

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 19

ctlbody.tld 1.0 <jspversion>1.1 <shortname>JSP course labs Various tags for CTL labs company ctlbody.CompanyTag JSP name ctlbody.NameTag JSP motto ctlbody.MottoTag JSP

CustomerBean.java //* Customer String String String String String String

Bean with these read/write properties: userName street city state zip cardNumber

*/ package ctlbody; public class CustomerBean { private String userName private String street private String city private String state private String zip private String cardNumber

© 2004 SkillBuilders, Inc.

= = = = = =

""; ""; ""; ""; ""; "";

V 3.1

Jav Server Pages: Solutions to Workshops

Page 20

//================================================ // String userName property //================================================ public String getUserName() { return userName; } public void setUserName(String value) { userName = value; } //================================================ // String street property //================================================ public String getStreet() { return street; } public void setStreet(String value) { street = value; } //================================================ // String city property //================================================ public String getCity() { return city; } public void setCity(String value) { city = value; } //================================================ // String state property //================================================ public String getState() { return state; } public void setState(String value) { state = value; } //================================================ // String zip property //================================================ public String getZip() { return zip; } public void setZip(String value) { zip = value; } //================================================ // String cardNumber property //================================================ public String getCardNumber() { return cardNumber; } public void setCardNumber(String value) { cardNumber = value; } }

MottoTag.java package ctlbody; import javax.servlet.jsp.*;

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 21

import javax.servlet.jsp.tagext.BodyTagSupport; public class MottoTag extends BodyTagSupport { public int doAfterBody() throws JspException { System.out.println("Processing motto"); // Get company bean from application attribute Object obj = pageContext.getAttribute(CompanyTag.BEAN_NAME, PageContext.APPLICATION_SCOPE); CompanyBean company = (CompanyBean) obj; System.out.println("Company bean before assigning motto:"); System.out.println(company); // Get body content as a string String content = bodyContent.getString(); company.setMotto(content); System.out.println("Company bean after assigning motto:"); System.out.println(company); return SKIP_BODY; } }

NameTag.java package ctlbody; import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.BodyTagSupport; public class NameTag extends BodyTagSupport { public int doAfterBody() throws JspException { System.out.println("Processing motto"); // Get company bean from application attribute Object obj = pageContext.getAttribute(CompanyTag.BEAN_NAME, PageContext.APPLICATION_SCOPE); CompanyBean company = (CompanyBean) obj; System.out.println("Company bean before assigning name:"); System.out.println(company); // Get body content as a string String content = bodyContent.getString(); company.setName(content); System.out.println("Company bean after assigning name:"); System.out.println(company); return SKIP_BODY; } }

© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 22

web.xml <web-app> ctlbasic /WEB-INF/tlds/ctlbasic.tld ctladvanced1 /WEB-INF/tlds/ctladvanced1.tld ctladvanced2 /WEB-INF/tlds/ctladvanced2.tld ctlbody /WEB-INF/tlds/ctlbody.tld

welcome.jsp <TITLE>Welcome <jsp:useBean id="customer" class="ctlbody.CustomerBean" scope="session"> <jsp:setProperty name="customer" property="*" /> <%@ include file="CompanyHeader.jsp" %>

Welcome <jsp:getProperty name="customer" property="userName" />!

We are pleased to have you as a customer!

Here is the information you entered:



© 2004 SkillBuilders, Inc.

V 3.1

Jav Server Pages: Solutions to Workshops

Page 23

property="userName" />

property="street" />

property="city" />

property="state" />

property="zip" />

<% String strCardNum = customer.getCardNumber(); if(strCardNum.length() > 0) { %> <% } else { %> <% } %>
Name: <jsp:getProperty name="customer"
Street address: <jsp:getProperty name="customer"
City: <jsp:getProperty name="customer"
State: <jsp:getProperty name="customer"
Zip: <jsp:getProperty name="customer"
Credit Card #: <jsp:getProperty name="customer" property="cardNumber" />
<jsp:include page="NoCreditCard.html" flush="true" />



Unchanged Register.html NoCreditCard.html

© 2004 SkillBuilders, Inc.

V 3.1

Related Documents


More Documents from ""