Advanced Java Server Pages An more detailed look

  • Slides: 33
Download presentation
Advanced Java Server Pages An more detailed look at JSPs

Advanced Java Server Pages An more detailed look at JSPs

Custom methods in JSPs n n n Just as Servlets, JSPs have methods for

Custom methods in JSPs n n n Just as Servlets, JSPs have methods for initialization end destruction Jsp. Init() corresponds to init() in Servlets Jsp. Destroy corresponds to destroy() in Servlets Both Jsp. Init() and Jsp. Destroy() can implemented in your JSPs, i. e you can override the base class Think before you use them q If you need the init and destroy, is it really a JSP you should be writing? Probably not!

Servlets and JSPs n Servlets should be used as front ends for JSPs q

Servlets and JSPs n Servlets should be used as front ends for JSPs q q n 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?

Before the explanation n Assume there is a Bean, Data. Bean that fetches records

Before the explanation n Assume there is a Bean, Data. Bean that fetches records from a database q q The records are fetched to the bean with the fetch() method The records are returned from the bean with the get. Records() method

The Servlet part n n n Create the Bean instance Call your methods on

The Servlet part n n n Create the Bean instance Call your methods on the Bean Add the bean to the request or the session Get a Request. Dispatcher Use the forward() method in the Request. Dispatcher

The Servlet part, using the request public void do. Get(Http. Servlet. Request request, Http.

The Servlet part, using the request public void do. Get(Http. Servlet. Request request, Http. Servlet. Response response) throws Servlet. Exception, IOException { Data. Bean db = new Data. Bean(); db. fetch(); request. set. Attribute("db. Bean", db); Request. Dispatcher rd = request. get. Request. Dispatcher("jsppage. jsp"); rd. forward(request, response); }

The Servlet part, using the session public void do. Get(Http. Servlet. Request request, Http.

The Servlet part, using the session public void do. Get(Http. Servlet. Request request, Http. Servlet. Response response) throws Servlet. Exception, IOException { Http. Session ses = request. get. Session(); Data. Bean db = new Data. Bean(); db. fetch(); ses. set. Attribute("db. Bean", db); Request. Dispatcher rd = request. get. Request. Dispatcher("jsppage. jsp"); rd. forward(request, response); }

The JSP part n n The JSP uses the <jsp: use. Bean. . >

The JSP part n n The JSP uses the <jsp: use. Bean. . > to define the bean, but it should not result in a new instance. The instance that was added in the Servlet is used q q n The same scope in <jsp: use. Bean …> as in the Servlet (request and session in our examples) The same id in <jsp: use. Bean …> as the name in the Servlet The JSP uses <jsp: get. Property …> to call methods in the Bean

The JSP part, with request <jsp: use. Bean id=“db. Bean" class=“beans. Data. Bean" scope=“request">

The JSP part, with request <jsp: use. Bean id=“db. Bean" class=“beans. Data. Bean" scope=“request"> Error, the bean should have been created in the servlet! </jsp: use. Bean> <jsp: get. Property name=“db. Bean” property=“records” /> <%-- property=“records” will look for get. Records() in the bean --%>

The JSP part, with session <jsp: use. Bean id=“db. Bean" class=“beans. Data. Bean" scope=“session">

The JSP part, with session <jsp: use. Bean id=“db. Bean" class=“beans. Data. Bean" scope=“session"> Error, the bean should have been created in the servlet! </jsp: use. Bean> <jsp: get. Property name=“db. Bean” property=“records” /> <%-- property=“records” will look for get. Records() in the bean --%>

Custom Tags n n n We don’t want Java code in our JSPs The

Custom Tags n n n 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 q Custom Tags

Custom Tags n n Custom Tags separates the logic from the presentation even more

Custom Tags n n Custom Tags separates the logic from the presentation even more that <jsp: use. Bean …> In our Book. Shop for example, the JSP with a book list and the shopping cart can consist of two lines of code q q <bookshop: book. List /> <bookshop: shopping. Cart />

Two types of Custom Tags n Simple Tag (Tag) q q q n A

Two types of Custom Tags n Simple Tag (Tag) q q q n A tag with or without arguments Doesn’t use the Tag body Implements javax. servlet. jsp. tagext. Tag Body Tag q q With or without arguments Evaluate the body of the tag n q <bookshop: a. Tag>This is the body</bookshop: a. Tag> Implements javax. servlet. jsp. tagext. Body. Tag

Simple Tag, an example n n n n n n import javax. servlet. jsp.

Simple Tag, an example n n n n n n import javax. servlet. jsp. *; import javax. servlet. jsp. tagext. *; public class Hello. World. Tag implements Tag { private Page. Context page. Context; private Tag parent; /** * Constructor */ public Hello. World. Tag() { super(); } /** * Method called at start of Tag * @return either a EVAL_BODY_INCLUDE or a SKIP_BODY */ public int do. Start. Tag() throws javax. servlet. jsp. Jsp. Tag. Exception { return SKIP_BODY; }

Simple Tag, an example n n n n n n n /** * Method

Simple Tag, an example n n n n n n n /** * Method Called at end of Tag * @return either EVAL_PAGE or SKIP_PAGE */ public int do. End. Tag() throws javax. servlet. jsp. Jsp. Tag. Exception { try { page. Context. get. Out(). write("Hello World!"); } catch(java. io. IOException e) { throw new Jsp. Tag. Exception("IO Error: " + e. get. Message()); } return EVAL_PAGE; } /** * Method called to releases all resources */ public void release() {} /** Method used by the JSP container to set the current Page. Context * @param page. Context, the current Page. Context */ public void set. Page. Context(final javax. servlet. jsp. Page. Context page. Context) { this. page. Context=page. Context; }

Simple Tag, an example n n n n /** Method used by the JSP

Simple Tag, an example n n n n /** Method used by the JSP container to set the parent of the Tag * @param parent, the parent Tag */ public void set. Parent(final javax. servlet. jsp. tagext. Tag parent) { this. parent=parent; } /** Method for retrieving the parent * @return the parent */ public javax. servlet. jsp. tagext. Tag get. Parent() { return parent; } n n n n }

Simple Tag n All methods in the interface must be implemented! q n n

Simple Tag n All methods in the interface must be implemented! q n n n All work is done in the do. Start. Tag() and do. End. Tag() The do. Start. Tag() is called at the start of the tag The do. End. Tag() is called at the start of the tag Instead of implementing Tag, extend Tag. Support q A helper class included in the package that has implementation for all required methods. Just implement the one that you will use!

Simple Tag, with Tag. Support (complete) n n n n n n import javax.

Simple Tag, with Tag. Support (complete) n n n n n n import javax. servlet. jsp. *; import javax. servlet. jsp. tagext. *; public class Hello. World. Tag extends Tag. Support { private Page. Context page. Context; private Tag parent; /** * Method Called at end of Tag * @return either EVAL_PAGE or SKIP_PAGE */ public int do. End. Tag() throws javax. servlet. jsp. Jsp. Tag. Exception { try { page. Context. get. Out(). write("Hello World!"); } catch(java. io. IOException e) { throw new Jsp. Tag. Exception("IO Error: " + e. get. Message()); } return EVAL_PAGE; } }

Simple Tags n n n In other words, use Tag. Support! The Hello. Tag

Simple Tags n n n In other words, use Tag. Support! The Hello. Tag implements our simple tag Custom Tags are defined in a Tag Library Descriptor (tld)

Tag Library Descriptor n n n n <taglib> <tlibversion>1. 0</tlibversion> <jspversion>1. 1</jspversion> <shortname>mt</shortname> <uri>/mytag</uri>

Tag Library Descriptor n n n n <taglib> <tlibversion>1. 0</tlibversion> <jspversion>1. 1</jspversion> <shortname>mt</shortname> <uri>/mytag</uri> <info>My first Tag library</info> <tag> <name>hello. World</name> <tagclass>tags. Hello. World. Tag</tagclass> <bodycontent>empty</bodycontent> <info>A Hello world Tag</info> </taglib>

Web. xml n The tld is referenced in web. xml q Web. xml binds

Web. xml n The tld is referenced in web. xml q 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>

Using your tag n n The uri specified in web. xml is used in

Using your tag n n The uri specified in web. xml is used in the JSP <%@ taglib uri="mytags" prefix="mt" %> q n At the start of the page <mt: hello. Tag /> q Prints “Hello World”

Parameterized tags n Both Tag and Body. Tag can take parameters q n <mt:

Parameterized tags n Both Tag and Body. Tag can take parameters q n <mt: hello. Tag name=“Fredrik” /> Just as in Java. Beans, use a set-method and a member variable q q private String name=“”; public void set. Name(_name){ name=_name; }

Parameterized tags n n n n n import javax. servlet. jsp. *; import javax.

Parameterized tags n n n n n import javax. servlet. jsp. *; import javax. servlet. jsp. tagext. *; public class Hello. Name. Tag extends Tag. Support{ private String name=“”; public void set. Name(String _name){name=_name; } public int do. End. Tag() throws javax. servlet. jsp. Jsp. Tag. Exception{ try{ page. Context. get. Out(). write("Hello “ + name); } catch(java. io. IOException e){ throw new Jsp. Tag. Exception("IO Error: " + e. get. Message()); q n } return EVAL_PAGE; n } n n }

TLD for Hello. Name <tag> <name>hello</name> <tagclass>Hello. Name. Tag</tagclass> <bodycontent>empty</bodycontent> <info>A Hello Tag</info> <attribute>

TLD for Hello. Name <tag> <name>hello</name> <tagclass>Hello. Name. Tag</tagclass> <bodycontent>empty</bodycontent> <info>A Hello Tag</info> <attribute> <name>name</name> <required>false</required> <rtexprvalue>false</rtexprvalue> </attribute> </tag>

Using the parameterized tag n <mt: hello name=“Fredrik”/>

Using the parameterized tag n <mt: hello name=“Fredrik”/>

Body. Tag n n n Just as with Tag, there a lot of methods

Body. Tag n n n Just as with Tag, there a lot of methods to write if implementing Body. Tag There is a helper class call Body. Tag. Support just as with Tag. Support Body. Tag is often used for looping over some result

Body Tag n package se. upright. education. uu. pvk. tags; n import javax. servlet.

Body Tag n package se. upright. education. uu. pvk. tags; n import javax. servlet. jsp. *; import javax. servlet. jsp. tagext. *; n n n public class Loop. Tag extends Body. Tag. Support { n private int iterations = 5; n /** * Method used by the JSP container to set the parameter Name. */ public void set. Iterations(int _iterations) { this. iterations=_iterations; } n n n n /** * Method called by the Container to set the Body. Content */ public void set. Body. Content(Body. Content body. Content) { this. body. Content=body. Content; }

Body Tag n n n n public int do. After. Body() throws Jsp. Tag.

Body Tag n n n n public int do. After. Body() throws Jsp. Tag. Exception { if(iterations>1) { //decrease the number of iterations left to do iterations--; return EVAL_BODY_TAG; } else { return SKIP_BODY; } }

Body Tag n n n n n /** * Method Called at end of

Body Tag n n n n n /** * Method Called at end of tag * @return either EVAL_PAGE or SKIP_PAGE */ public int do. End. Tag() throws Jsp. Tag. Exception { try { if(body. Content != null) // Check if we even entered the body. Content. write. Out(body. Content. get. Enclosing. Writer()); } catch(java. io. IOException e) { throw new Jsp. Tag. Exception("IO Error: " + e. get. Message()); } return EVAL_PAGE; } n n }

Body Tag tld <tag> <name>loop</name> <tag-class>se. upright. education. uu. pvk. tags. Loop. Tag</tag-class> <body-content>JSP</body-content>

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

Using Body Tag <uu: loop iterations="10"> Looping </uu: loop>

Using Body Tag <uu: loop iterations="10"> Looping </uu: loop>

Next Time n Java Standard Tag Library q n n A Tag Library with

Next Time n Java Standard Tag Library q n n A Tag Library with common functionallity XML XSLT