SringSring
Wednesday, 23 September 2015
Tuesday, 8 September 2015
JSP CUSTOM TAGLIBS
A custom tag is a user-defined JSP language element. When a JSP page containing a custom tag is translated into a servlet, the tag is converted to operations on an object called a tag handler. The Web container then invokes those operations when the JSP page's servlet is executed.
JSP tag extensions let you create new tags that you can insert directly into a JavaServer Page just as you would the built-in tags you learned about in earlier chapter. The JSP 2.0 specification introduced Simple Tag Handlers for writing these custom tags.
To write a customer tab you can simply extend SimpleTagSupport class and override thedoTag() method, where you can place your code to generate content for the tag.
Create "Hello" Tag:
Consider you want to define a custom tag named <ex:Hello> and you want to use it in the following fashion without a body:
<ex:Hello />
To create a custom JSP tag, you must first create a Java class that acts as a tag handler. So let us create HelloTag class as follows:
package com.tutorialspoint; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport { public void doTag() throws JspException, IOException { JspWriter out = getJspContext().getOut(); out.println("Hello Custom Tag!"); } }
Above code has simple coding where doTag() method takes the current JspContext object using getJspContext() method and uses it to send "Hello Custom Tag!" to the current JspWriter object.
Let us compile above class and copy it in a directory available in environment variable CLASSPATH. Finally create following tag library file: <Tomcat-Installation-Directory>webapps\ROOT\WEB-INF\custom.tld.
<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>Example TLD</short-name> <tag> <name>Hello</name> <tag-class>com.tutorialspoint.HelloTag</tag-class> <body-content>empty</body-content> </tag> </taglib>
Now it's time to use above defined custom tag Hello in our JSP program as follows:
<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%> <html> <head> <title>A sample custom tag</title> </head> <body> <ex:Hello/> </body> </html>
Try to call above JSP and this should produce following result:
Hello Custom Tag!
Accessing the Tag Body:
You can include a message in the body of the tag as you have seen with standard tags. Consider you want to define a custom tag named <ex:Hello> and you want to use it in the following fashion with a body:
<ex:Hello> This is message body </ex:Hello>
Let us make following changes in above our tag code to process the body of the tag:
package com.tutorialspoint; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport { StringWriter sw = new StringWriter(); public void doTag() throws JspException, IOException { getJspBody().invoke(sw); getJspContext().getOut().println(sw.toString()); } }
In this case, the output resulting from the invocation is first captured into a StringWriter before being written to the JspWriter associated with the tag. Now accordingly we need to change TLD file as follows:
<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>Example TLD with Body</short-name> <tag> <name>Hello</name> <tag-class>com.tutorialspoint.HelloTag</tag-class> <body-content>scriptless</body-content> </tag> </taglib>
Now let us call above tag with proper body as follows:
<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%> <html> <head> <title>A sample custom tag</title> </head> <body> <ex:Hello> This is message body </ex:Hello> </body> </html>
This will produce following result:
This is message body
Custom Tag Attributes:
You can use various attributes along with your custom tags. To accept an attribute value, a custom tag class needs to implement setter methods, identical to JavaBean setter methods as shown below:
package com.tutorialspoint; import javax.servlet.jsp.tagext.*; import javax.servlet.jsp.*; import java.io.*; public class HelloTag extends SimpleTagSupport { private String message; public void setMessage(String msg) { this.message = msg; } StringWriter sw = new StringWriter(); public void doTag() throws JspException, IOException { if (message != null) { /* Use message from attribute */ JspWriter out = getJspContext().getOut(); out.println( message ); } else { /* use message from the body */ getJspBody().invoke(sw); getJspContext().getOut().println(sw.toString()); } } }
The attribute's name is "message", so the setter method is setMessage(). Now let us add this attribute in TLD file using <attribute> element as follows:
<taglib> <tlib-version>1.0</tlib-version> <jsp-version>2.0</jsp-version> <short-name>Example TLD with Body</short-name> <tag> <name>Hello</name> <tag-class>com.tutorialspoint.HelloTag</tag-class> <body-content>scriptless</body-content> <attribute> <name>message</name> </attribute> </tag> </taglib>
Now let us try following JSP with message attribute as follows:
<%@ taglib prefix="ex" uri="WEB-INF/custom.tld"%> <html> <head> <title>A sample custom tag</title> </head> <body> <ex:Hello message="This is custom tag" /> </body> </html>
This will produce following result:
This is custom tag
Hope above example makes sense for you. It would be worth to note that you can include following properties for an attribute:
JDBC 2.0
Overview of JDBC 2.0 Features
Table 4-1 lists key areas of JDBC 2.0 functionality and points to where you can go in this manual for more information about Oracle support.
Table 4-1 Key Areas of JDBC 2.0 Functionality
| Feature | Comments and References |
|---|---|
update batching
|
Also available previously as an Oracle extension. With release 8.1.6 and higher, under either JDK 1.2.x or JDK 1.1.x you can use either the standard update batching model or the Oracle model.
See "Update Batching" for information.
|
result set enhancements (scrollable and updatable result sets)
|
See Chapter 12, "Result Set Enhancements" for information.
|
fetch size / row prefetching
|
With release 8.1.6 and higher, the JDBC 2.0 fetch size feature is also available under JDK 1.1.x as an Oracle extension.
|
use of JNDI (Java Naming and Directory Interface) to specify and obtain database connections
| |
connection pooling (framework for connection caching)
|
This requires the JDBC 2.0 Optional Package (JDBC 2.0 Standard Extension API) in the javax.sql package. With release 8.1.6 and higher this is available under either JDK 1.2.x or 1.1.x.
See "Connection Pooling" for information.
|
connection caching (sample Oracle implementation)
|
This requires the JDBC 2.0 Optional Package (JDBC 2.0 Standard Extension API) in the javax.sql package. With release 8.1.6 and higher, this is available under either JDK 1.2.x or 1.1.x.
See "Connection Caching" for information.
|
distributed transactions / XA functionality
|
This requires the JDBC 2.0 Optional Package (JDBC 2.0 Standard Extension API) in the javax.sql package. With release 8.1.6 and higher,, this is available under either JDK 1.2.x or 1.1.x.
See Chapter 16, "Distributed Transactions" for information.
|
miscellaneous getXXX() methods
|
See "Other getXXX() Methods" for information about which getXXX() methods are Oracle extensions under JDK 1.2.x and 1.1.x, and about any differences in functionality with JDBC 2.0.
|
miscellaneous setXXX() methods
|
See "Other setXXX() Methods" for information about which setXXX() methods are Oracle extensions under JDK 1.2.x and 1.1.x, and about any differences in functionality with JDBC 2.0.
|
1.Moving the Cursor in Scrollable Result Sets
------------------------------------------------
In the JDBC 1.0 API, the only way to move the cursor was to call the method next . But in JDBC 2.0 API you have many other ways to move the cursor. The counterpart to the method next is the new method previous , which moves the cursor backward
2.Making Updates to Updatable Result Sets
----------------------------------------
The new methods in JDBC 2.0 API, which JDBC 1.0 API doesnt have, for the ResultSet interface is: insert a new row into ResultSet object, delete an existing row from ResultSet object, or modify a column value in ResultSet object.
3.Updating, Inserting and Deleting a Result Set Programmatically
----------------------------------------------------------------
Using the JDBC 1.0 API, the update statemnet would look something like this:
stmt.executeUpdate("UPDATE COFFEES SET PRICE = 10.99" +
"WHERE COF_NAME = FRENCH_ROAST_DECAF");
The following code fragment shows another way to accomplish the update, this time using the JDBC 2.0 API:
uprs.last();
uprs.updateFloat("PRICE", 10.99);
uprs.updateRow();
(Here uprs is the ResultSet object)
Update operations in the JDBC 2.0 API affect column values in the row where the cursor is positioned, so in the first line the ResultSet uprs calls the method last to move its cursor to the last row . Once the cursor is on the last row, all of the update methods you call will operate on that row until you move the cursor to another row. The second line changes the value in the PRICE column to 10.99 by calling the method updateFloat. This method is used because the column value we want to update is a float in the Java programming language. To make the update take effect in the database and not just the result set, we must call the ResultSet methodupdateRow.
Similarly, to insert a row, instead of using executeUpdate(...), you should invoke the method moveToInsertRow, then set a value for each column in the row. You do this by calling the appropriate updateXXX method for each value and then call uprs.insertRow().
Now, to delete a row, say 4th row from a table, you call:
uprs.absolute(4);
uprs.deleteRow();
This will remove fourth row from ResultSet object and also from the database.
4. Using Statement Objects for Batch Updates
------------------------------------------------
In the JDBC 1.0 API, Statement objects submit updates to the database individually with the method executeUpdate. Multiple executeUpdate statements can be sent in the same transaction, but even though they are committed or rolled back as a unit, they are still processed individually. The interfaces derived from Statement, PreparedStatement and CallableStatement, have the same capabilities, using their own version of executeUpdate.
With the JDBC 2.0 API, Statement, PreparedStatement, and CallableStatement objects have the ability to maintain a list of commands that can be submitted together as a batch. They are created with an associated list, which is initially empty. You can add SQL commands to this list with the method addBatch, and you can empty the list with the method clearBatch. You send all of the commands in the list to the database with the method executeBatch.
5.Using SQL3 Datatypes
-----------------------
The datatypes commonly referred to as SQL3 types are the new datatypes being adopted in the next version of the ANSI/ISO SQL standard. The JDBC 2.0 API provides interfaces that represent the mapping of these SQL3 datatypes into the Java programming language. With these new interfaces, you can work with SQL3 datatypes the same way you do other datatypes.
------------------------------------------------
In the JDBC 1.0 API, the only way to move the cursor was to call the method next . But in JDBC 2.0 API you have many other ways to move the cursor. The counterpart to the method next is the new method previous , which moves the cursor backward
2.Making Updates to Updatable Result Sets
----------------------------------------
The new methods in JDBC 2.0 API, which JDBC 1.0 API doesnt have, for the ResultSet interface is: insert a new row into ResultSet object, delete an existing row from ResultSet object, or modify a column value in ResultSet object.
3.Updating, Inserting and Deleting a Result Set Programmatically
----------------------------------------------------------------
Using the JDBC 1.0 API, the update statemnet would look something like this:
stmt.executeUpdate("UPDATE COFFEES SET PRICE = 10.99" +
"WHERE COF_NAME = FRENCH_ROAST_DECAF");
The following code fragment shows another way to accomplish the update, this time using the JDBC 2.0 API:
uprs.last();
uprs.updateFloat("PRICE", 10.99);
uprs.updateRow();
(Here uprs is the ResultSet object)
Update operations in the JDBC 2.0 API affect column values in the row where the cursor is positioned, so in the first line the ResultSet uprs calls the method last to move its cursor to the last row . Once the cursor is on the last row, all of the update methods you call will operate on that row until you move the cursor to another row. The second line changes the value in the PRICE column to 10.99 by calling the method updateFloat. This method is used because the column value we want to update is a float in the Java programming language. To make the update take effect in the database and not just the result set, we must call the ResultSet methodupdateRow.
Similarly, to insert a row, instead of using executeUpdate(...), you should invoke the method moveToInsertRow, then set a value for each column in the row. You do this by calling the appropriate updateXXX method for each value and then call uprs.insertRow().
Now, to delete a row, say 4th row from a table, you call:
uprs.absolute(4);
uprs.deleteRow();
This will remove fourth row from ResultSet object and also from the database.
4. Using Statement Objects for Batch Updates
------------------------------------------------
In the JDBC 1.0 API, Statement objects submit updates to the database individually with the method executeUpdate. Multiple executeUpdate statements can be sent in the same transaction, but even though they are committed or rolled back as a unit, they are still processed individually. The interfaces derived from Statement, PreparedStatement and CallableStatement, have the same capabilities, using their own version of executeUpdate.
With the JDBC 2.0 API, Statement, PreparedStatement, and CallableStatement objects have the ability to maintain a list of commands that can be submitted together as a batch. They are created with an associated list, which is initially empty. You can add SQL commands to this list with the method addBatch, and you can empty the list with the method clearBatch. You send all of the commands in the list to the database with the method executeBatch.
5.Using SQL3 Datatypes
-----------------------
The datatypes commonly referred to as SQL3 types are the new datatypes being adopted in the next version of the ANSI/ISO SQL standard. The JDBC 2.0 API provides interfaces that represent the mapping of these SQL3 datatypes into the Java programming language. With these new interfaces, you can work with SQL3 datatypes the same way you do other datatypes.
Subscribe to:
Comments (Atom)