Top Banner
Advanced Java Server Pages An more detailed look at JSPs
33

Advanced Java Server Pages An more detailed look at JSPs.

Dec 19, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Advanced Java Server Pages An more detailed look at JSPs.

Advanced Java Server Pages

An more detailed look at JSPs

Page 2: Advanced Java Server Pages An more detailed look at JSPs.

Custom methods in JSPs

Just as Servlets, JSPs have methods for initialization end destruction

JspInit() corresponds to init() in Servlets JspDestroy corresponds to destroy() in Servlets Both JspInit() and JspDestroy() can implemented in

your JSPs, i.e you can override the base class Think before you use them

If you need the init and destroy, is it really a JSP you should be writing? Probably not!

Page 3: Advanced Java Server Pages An more detailed look at JSPs.

Servlets and JSPs

Servlets should be used as front ends for JSPs The Servlet should handle the control logic The Servlet can initialize Beans that the JSP can

use The Beans can talk to databases, EJBs and so on This is basic MVC

But how is this done?

Page 4: Advanced Java Server Pages An more detailed look at JSPs.

Before the explanation

Assume there is a Bean, DataBean that fetches records from a database The records are fetched to the bean with the

fetch() method The records are returned from the bean with the

getRecords() method

Page 5: Advanced Java Server Pages An more detailed look at JSPs.

The Servlet part

Create the Bean instance Call your methods on the Bean Add the bean to the request or the session Get a RequestDispatcher Use the forward() method in the

RequestDispatcher

Page 6: Advanced Java Server Pages An more detailed look at JSPs.

The Servlet part, using the request public void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {

DataBean db = new DataBean();

db.fetch();

request.setAttribute("dbBean", db);

RequestDispatcher rd = request.getRequestDispatcher("jsppage.jsp");

rd.forward(request, response);

}

Page 7: Advanced Java Server Pages An more detailed look at JSPs.

The Servlet part, using the session public void doGet(HttpServletRequest request,

HttpServletResponse response) throws ServletException, IOException {HttpSession ses = request.getSession();

DataBean db = new DataBean();db.fetch();

ses.setAttribute("dbBean", db); RequestDispatcher rd =

request.getRequestDispatcher("jsppage.jsp"); rd.forward(request, response); }

Page 8: Advanced Java Server Pages An more detailed look at JSPs.

The JSP part

The JSP uses the <jsp:useBean ..> to define the bean, but it should not result in a new instance.

The instance that was added in the Servlet is used The same scope in <jsp:useBean …> as in the Servlet

(request and session in our examples) The same id in <jsp:useBean …> as the name in the

Servlet The JSP uses <jsp:getProperty …> to call methods

in the Bean

Page 9: Advanced Java Server Pages An more detailed look at JSPs.

The JSP part, with request

<jsp:useBean id=“dbBean" class=“beans.DataBean" scope=“request">

Error, the bean should have been created in the servlet!

</jsp:useBean>

<jsp:getProperty name=“dbBean” property=“records” />

<%-- property=“records” will look for getRecords() in the bean --%>

Page 10: Advanced Java Server Pages An more detailed look at JSPs.

The JSP part, with session

<jsp:useBean id=“dbBean" class=“beans.DataBean" scope=“session">

Error, the bean should have been created in the servlet!

</jsp:useBean>

<jsp:getProperty name=“dbBean” property=“records” />

<%-- property=“records” will look for getRecords() in the bean --%>

Page 11: Advanced Java Server Pages An more detailed look at JSPs.

Custom Tags

We don’t want Java code in our JSPs The built in tags <jsp:xxx > is quite limited in

functionality What we need is a way to extend the built in

tags with our own Custom Tags

Page 12: Advanced Java Server Pages An more detailed look at JSPs.

Custom Tags

Custom Tags separates the logic from the presentation even more that <jsp:useBean …>

In our BookShop for example, the JSP with a book list and the shopping cart can consist of two lines of code <bookshop:bookList /> <bookshop:shoppingCart />

Page 13: Advanced Java Server Pages An more detailed look at JSPs.

Two types of Custom Tags

Simple Tag (Tag) A tag with or without arguments Doesn’t use the Tag body Implements javax.servlet.jsp.tagext.Tag

Body Tag With or without arguments Evaluate the body of the tag

<bookshop:aTag>This is the body</bookshop:aTag> Implements javax.servlet.jsp.tagext.BodyTag

Page 14: Advanced Java Server Pages An more detailed look at JSPs.

Simple Tag, an example

import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*;

public class HelloWorldTag implements Tag { private PageContext pageContext; private Tag parent;

/** * Constructor */ public HelloWorldTag() { super(); }

/** * Method called at start of Tag * @return either a EVAL_BODY_INCLUDE or a SKIP_BODY */ public int doStartTag() throws javax.servlet.jsp.JspTagException { return SKIP_BODY; }

Page 15: Advanced Java Server Pages An more detailed look at JSPs.

Simple Tag, an example

/** * Method Called at end of Tag * @return either EVAL_PAGE or SKIP_PAGE */ public int doEndTag() throws javax.servlet.jsp.JspTagException { try { pageContext.getOut().write("Hello World!"); } catch(java.io.IOException e) { throw new JspTagException("IO Error: " + e.getMessage()); } return EVAL_PAGE; }

/** * Method called to releases all resources */ public void release() {}

/** Method used by the JSP container to set the current PageContext * @param pageContext, the current PageContext */ public void setPageContext(final javax.servlet.jsp.PageContext pageContext) { this.pageContext=pageContext; }

Page 16: Advanced Java Server Pages An more detailed look at JSPs.

Simple Tag, an example

/** Method used by the JSP container to set the parent of the Tag * @param parent, the parent Tag */ public void setParent(final javax.servlet.jsp.tagext.Tag parent) { this.parent=parent; }

/** Method for retrieving the parent * @return the parent */ public javax.servlet.jsp.tagext.Tag getParent() { return parent; } }

Page 17: Advanced Java Server Pages An more detailed look at JSPs.

Simple Tag

All methods in the interface must be implemented! All work is done in the doStartTag() and doEndTag()

The doStartTag() is called at the start of the tag The doEndTag() is called at the start of the tag Instead of implementing Tag, extend TagSupport

A helper class included in the package that has implementation for all required methods. Just implement the one that you will use!

Page 18: Advanced Java Server Pages An more detailed look at JSPs.

Simple Tag, with TagSupport (complete) import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*;

public class HelloWorldTag extends TagSupport { private PageContext pageContext; private Tag parent;

/** * Method Called at end of Tag * @return either EVAL_PAGE or SKIP_PAGE */ public int doEndTag() throws javax.servlet.jsp.JspTagException { try { pageContext.getOut().write("Hello World!"); } catch(java.io.IOException e) { throw new JspTagException("IO Error: " + e.getMessage()); } return EVAL_PAGE; } }

Page 19: Advanced Java Server Pages An more detailed look at JSPs.

Simple Tags

In other words, use TagSupport! The HelloTag implements our simple tag Custom Tags are defined in a Tag Library

Descriptor (tld)

Page 20: Advanced Java Server Pages An more detailed look at JSPs.

Tag Library Descriptor

<taglib> <tlibversion>1.0</tlibversion> <jspversion>1.1</jspversion> <shortname>mt</shortname> <uri>/mytag</uri> <info>My first Tag library</info> <tag> <name>helloWorld</name> <tagclass>tags.HelloWorldTag</tagclass> <bodycontent>empty</bodycontent> <info>A Hello world Tag</info> </tag> </taglib>

Page 21: Advanced Java Server Pages An more detailed look at JSPs.

Web.xml

The tld is referenced in web.xml Web.xml binds a tag library to a web application

<taglib>

<taglib-uri>mytags</taglib-uri>

<taglib-location>/WEB-INF/taglib.tld</taglib-location>

</taglib>

Page 22: Advanced Java Server Pages An more detailed look at JSPs.

Using your tag

The uri specified in web.xml is used in the JSP

<%@ taglib uri="mytags" prefix="mt" %> At the start of the page

<mt:helloTag /> Prints “Hello World”

Page 23: Advanced Java Server Pages An more detailed look at JSPs.

Parameterized tags

Both Tag and BodyTag can take parameters <mt:helloTag name=“Fredrik” />

Just as in JavaBeans, use a set-method and a member variable private String name=“”; public void setName(_name){ name=_name;}

Page 24: Advanced Java Server Pages An more detailed look at JSPs.

Parameterized tags

import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*;

public class HelloNameTag extends TagSupport{ private String name=“”; public void setName(String _name){name=_name;} public int doEndTag() throws javax.servlet.jsp.JspTagException{ try{ pageContext.getOut().write("Hello “ + name); } catch(java.io.IOException e){

throw new JspTagException("IO Error: " + e.getMessage()); } return EVAL_PAGE; } }

Page 25: Advanced Java Server Pages An more detailed look at JSPs.

TLD for HelloName

<tag><name>hello</name><tagclass>HelloNameTag</tagclass><bodycontent>empty</bodycontent><info>A Hello Tag</info><attribute><name>name</name><required>false</required><rtexprvalue>false</rtexprvalue></attribute>

</tag>

Page 26: Advanced Java Server Pages An more detailed look at JSPs.

Using the parameterized tag

<mt:hello name=“Fredrik”/>

Page 27: Advanced Java Server Pages An more detailed look at JSPs.

BodyTag

Just as with Tag, there are a lot of methods to write if implementing BodyTag

There is a helper class call BodyTagSupport just as with TagSupport

BodyTag is often used for looping over some result

Page 28: Advanced Java Server Pages An more detailed look at JSPs.

Body Tag

package se.upright.education.uu.pvk.tags;

import javax.servlet.jsp.*; import javax.servlet.jsp.tagext.*;

public class LoopTag extends BodyTagSupport {

private int iterations = 5;

/** * Method used by the JSP container to set the parameter Name. */ public void setIterations(int _iterations) { this.iterations=_iterations; } /** * Method called by the Container to set the BodyContent */ public void setBodyContent(BodyContent bodyContent) { this.bodyContent=bodyContent; }

Page 29: Advanced Java Server Pages An more detailed look at JSPs.

Body Tag

public int doAfterBody() throws JspTagException { if(iterations>1) { //decrease the number of iterations left to do iterations--; return EVAL_BODY_TAG; } else { return SKIP_BODY; } }

Page 30: Advanced Java Server Pages An more detailed look at JSPs.

Body Tag

/** * Method Called at end of tag * @return either EVAL_PAGE or SKIP_PAGE */ public int doEndTag() throws JspTagException { try { if(bodyContent != null) // Check if we even entered the body bodyContent.writeOut(bodyContent.getEnclosingWriter()); } catch(java.io.IOException e) { throw new JspTagException("IO Error: " + e.getMessage()); } return EVAL_PAGE; } }

Page 31: Advanced Java Server Pages An more detailed look at JSPs.

Body Tag tld

<tag> <name>loop</name> <tag-class>se.upright.education.uu.pvk.tags.LoopTag</tag-class> <body-content>JSP</body-content> <attribute> <name>iterations</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>

Page 32: Advanced Java Server Pages An more detailed look at JSPs.

Using Body Tag

<uu:loop iterations="10">

Looping <br />

</uu:loop>

Page 33: Advanced Java Server Pages An more detailed look at JSPs.

Next Time

Java Standard Tag Library A Tag Library with common functionallity

XML XSLT