Presentation is loading. Please wait.

Presentation is loading. Please wait.

COMP 321 Week 11. Overview Lab 8-1 Solution Tag Files Custom Tags Web Application Deployment.

Similar presentations


Presentation on theme: "COMP 321 Week 11. Overview Lab 8-1 Solution Tag Files Custom Tags Web Application Deployment."— Presentation transcript:

1 COMP 321 Week 11

2 Overview Lab 8-1 Solution Tag Files Custom Tags Web Application Deployment

3 Lab 8-1 (“Servlets”) Solution

4 Tag Files Allow creation of reusable content Better than jsp:include and c:import –Don’t require adding request parameters to pass information around Easier to read than jsp:include or c:import

5 Tag Files --> Welcome to our site.

6 Passing Parameters ${param.subTitle} Contact us at: ${initParam.mainEmail}

7 Passing Parameters ${subTitle} Contact us at: ${initParam.mainEmail}

8 Passing Parameters We take the sting out of SOAP. OK, so it's not Jini, but we'll help you get through it with the least frustration and hair loss. Contact us at: ${initParam.mainEmail}

9 Finding Tag Files The container looks for tag files in:  WEB-INF/tags  Subdirectories of WEB-INF/tags  META-INF/tags in library JAR files  Subdirectories of META-INF/tags in library JAR files If the tag file is in a JAR, there must be a TLD file for it!

10 Sharpen Your Pencil What do you put in the Tag File to declare that the tag has one required attribute called title, that can use an EL expression as its value? What do you put in the Tag File to declare that the tag must NOT have a body? Draw a Tag File in each of the locations where the Container will look for Tag Files. WEB-INFclassesfoolibJAR META- INF tagsmyTagsTLDstags moreTags

11 Sharpen Your Pencil What do you put in the Tag File to declare that the tag has one required attribute called title, that can use an EL expression as its value? What do you put in the Tag File to declare that the tag must NOT have a body?

12 Sharpen Your Pencil Draw a Tag File in each of the locations where the Container will look for Tag Files. WEB-INFclassesfoolibJAR META- INF tagsfoo.tagmyTagsfoo.tagTLDsbar.tldtagsmoreTagsfoo.tag

13 Custom Tag Handlers Allow much more flexibility than Custom Tag Files Written in Java Come in two flavors  Classic  Simple (JSP 2.0 and above)

14 Creating a Simple Tag Handler 1. Write a class that extends SimpleTagSupport 2. Override the doTag() method 3. Create a TLD for the tag 4. Deploy the tag handler and TLD 5. Write a JSP that uses the tag

15 Simple Tag Handlers public class SimpleTagTest1 extends SimpleTagSupport { public void doTag() throws JspException, IOException { getJspContext().getOut().write("Lamest tag EVAR!"); } worst use of a custom tag simple1 foo.SimpleTagTest1 empty

16 Simple Tag Handlers public class SimpleTagTest2 extends SimpleTagSupport { public void doTag() throws JspException, IOException { getJspBody().invoke(null); } slightly better use of a custom tag simple2 foo.SimpleTagTest2 scriptless This is the body

17 Tag Handler Lifecycle 1. Load and instantiate class 2. Call setJspContext(JspContext) 3. If tag is nested, call setParent(JspTag) 4. If tag has attributes, call setters 5. If tag has a body, call setJspBody(JspFragment) 6. Call doTag()

18 Tags and Expressions This is the message: ${message} public class SimpleTagTest3 extends SimpleTagSupport { public void doTag() throws JspException, IOException { getJspContext().setAttribute("message", "Wear sunscreen."); getJspBody().invoke(null); }

19 Tags with Attributes takes an attribute and iterates over body simple5 foo.SimpleTagTest5 scriptless movieList true

20 Tags with Attributes ${movie.name} ${movie.genre} public void setMovieList(List movieList) { this.movieList = movieList; } public void doTag() throws JspException, IOException { for(Movie movie : movieList) { getJspContext().setAttribute("movie", movie); getJspBody().invoke(null); }

21 Sharpen Your Pencil SkipPageException allows tag evaluation to be aborted without killing the rest of the page. What is the output of the tag below? public void doTag() throws JspException, IOException { getJspContext().getOut().print("Message from within doTag(). "); getJspContext().getOut().print("About to throw a SkipPageException"); throw new SkipPageException(); } About to invoke a tag that throws SkipPageException Back in the page after invoking the tag.

22 Sharpen Your Pencil public void doTag() throws JspException, IOException { getJspContext().getOut().print("Message from within doTag(). "); getJspContext().getOut().print("About to throw a SkipPageException"); throw new SkipPageException(); } About to invoke a tag that throws SkipPageException Back in the page after invoking the tag. Result: About to invoke a tag that throws SkipPageException Message from within doTag(). About to throw a SkipPageException

23 Sharpen Your Pencil public void doTag() throws JspException, IOException { getJspContext().getOut().print("Message from within doTag(). "); getJspContext().getOut().print("About to throw a SkipPageException"); throw new SkipPageException(); } This is page (A) that includes another page (B). Doing the include now: Back in page A after the include... This is page B that invokes the tag that throws SkipPageException. Invoking the tag now: Still in page B after the tag invocation…

24 Sharpen Your Pencil Output: This is page (A) that includes another page (B). Doing the include now: This is page B that invokes the tag that throws SkipPageException. Invoking the tag now: Message from within doTag(). About to throw a SkipPageException Back in page A after the include…

25 Classic Tag Handlers <!-- No changes in JSP or TLD  public class Classic1 extends TagSupport { public int doStartTag() throws JspException { JspWriter out = pageContext.getOut(); try { out.println("classic tag output"); } catch(IOException e) { throw new JspException("IOException-" + e.toString()); } return SKIP_BODY; }

26 Classic Tag Handlers // Simple tag with a body public class SimpleTag extends SimpleTagSupport { public void doTag() throws JspException, IOException { getJspContext().getOut().print("Before body."); getJspBody().invoke(null); getJspContext().getOut().print("After body."); }

27 Classic Tag Handlers public int doStartTag() throws JspException { out = pageContext.getOut(); try { out.println("Before body."); } catch (IOException e) { throw new JspException("IOException-" + e.toString()); } return EVAL_BODY_INCLUDE; } public int doEndTag() throws JspException { try { out.println("After body."); } catch (IOException e) { throw new JspException("IOException-" + e.toString()); } return EVAL_PAGE; }

28 Classic Tag Handlers public int doStartTag() throws JspException { out = pageContext.getOut(); try { out.println("Before body."); } catch (IOException e) { throw new JspException("IOException-" + e.toString()); } return EVAL_BODY_INCLUDE; } public int doEndTag() throws JspException { try { out.println("After body."); } catch (IOException e) { throw new JspException("IOException-" + e.toString()); } return EVAL_PAGE; } How can we make the tag body repeat?

29 Classic Tag Handlers

30 Sharpen Your Pencil // Write the classic tag handler that is the equivalent of this simple handler public void doTag() throws JspException, IOException { String [] movies = {"Spiderman", "Saved!", "Amelie"}; for (String movie : movies) { getJspContext().setAttribute("movie", movie); getJspBody().invoke(null); } public class MyIteratorTag extends TagSupport { public int doStartTag() throws JspException {} public int doAfterBody() throws JspException {} public int doEndTag() throws JspException {} }

31 Sharpen Your Pencil public class MyIteratorTag extends TagSupport { String [] movies = {"Spiderman", "Saved!", "Amelie"}; int movieCounter; public int doStartTag() throws JspException { movieCounter = 0; pageContext.setAttribute("movie", movies[movieCounter]); movieCounter++; return EVAL_BODY_INCLUDE; }

32 Sharpen Your Pencil public int doAfterBody() throws JspException { if (movieCounter < movies.length) { pageContext.setAttribute("movie", movies[movieCounter]); movieCounter++; return EVAL_BODY_AGAIN; } return SKIP_BODY; } public int doEndTag() throws JspException { return EVAL_PAGE; }

33 Dynamic Attributes Select beer color: light... Select beer color: <formTags:select name="color" size="1" optionsList="${applicationScope.colorList"/>

34 Dynamic Attributes supports the following attributes supports the following attributes  Core Attributes: id, class, style, title  I18N Attributes: lang, dir  Event Attributes: onclick, ondblclick, onmouseup, onmouseover, onmousemove, onmouseout, onkeypress, onkeyup, onkeydown  Form Attributes: name, disabled, multiple, size, tabindex, onfocus, onblur, onchange

35 Dynamic Attributes public class SelectTagHandler extends SimpleTagSupport implements DynamicAttributes { private String name; private List optionsList; private Map tagAttrs = new HashMap (); public void setName(String name) { this.name = name; } public void setOptionsList(List value) { this.optionsList = value; } @Override public void setDynamicAttribute(String uri, String name, Object value) throws JspException { tagAttrs.put(name, value); }

36 Dynamic Attributes public void doTag() throws JspException, IOException { JspWriter out = ((PageContext)getJspContext()).getOut(); out.print("<select "); out.print(String.format(ATTR, "name", name)); for(String attrName : tagAttrs.keySet()) { out.print(String.format(ATTR, attrName, tagAttrs.get(attrName))); } out.println('>'); // Generate options from optionList here out.println(" "); } private static final String ATTR = "%s='%s' "; }

37 Dynamic Attributes Builds select tag select foo.SelectTagHandler empty name true optionsList java.util.List true true

38 Deployment MyAppmain.jspWEB-INFweb.xml classesfooMyTag.classlibmy.jartagsNavBar.tag

39 WAR Files Pack your web application in one file Allow library dependencies to be declared and checked at deployment time Have the same structure as your webapp, except for adding META-INF/MANIFEST.MF

40 Accessing Static Content Static HTML and JSPs can only be accessed if they are not inside WEB-INF This allows pages to be hidden from direct access, but they can still be forwarded to or included in other pages

41 Servlet Mapping Beer com.example.BeerSelect Beer /Beer/SelectBeer.do

42 Servlet Mapping - Three Types of url-pattern Exact match: /Beer/SelectBeer.do Directory match: /Beer/* Extension match: *.do Container looks for matches in the above order If more than one pattern matches, the longest (most-specific) is used

43 Welcome Files A list of welcome files can be specified in the DD When none of the servlet mappings match, the container looks for the specified files in order index.html default.jsp

44 Welcome Files - Example 1. Client requests www.wickedlysmart.com/MyTestApp/sear ch 2. Container checks for a servlet mapping 3. Container checks for MyTestApp/search/index.html 4. Container checks for MyTestApp/search/default.jsp

45 Progress Check Due this week:  Exam 2


Download ppt "COMP 321 Week 11. Overview Lab 8-1 Solution Tag Files Custom Tags Web Application Deployment."

Similar presentations


Ads by Google