By default there are some implicit objects are available in jsp. we don’t create those implicit objects in jsp or we don’t get those objects from any scope. we can directly use those objects on jsp in scriptlet. some of those objects are request, response, out etc. eg.
<% out.print("hello from out implicit object..."); %>
if there is a requirement in our project to create an implicit object in jsp we can create it using custom tags api of j2ee.
Benefits of implicit objects.
- No need to take the object form any scope.
- We can directly use those object in jsp so the code will be reduced.
- The code will be modulated.
Drawback
- The object creation logic is hidden from jsp developers.
Steps for creating your own custom implicit object in jsp.
Here is my project structure for this project.
Image may be NSFW.
Clik here to view.
Step1: Create the class for which you want to create the implicit object. i have created the following below class.
package com.custom.defined.object;
/**
*
* @author CodingLoading.com
*
*/
public class MyCustomClass {
public String callMethod(){
System.out.println("method called from implicit object...");
return "method called from implicit object...";
}
}
Step2: Create the custom tag class and one TagExtraInfo class. i have created as below. according to the tag class our custom implicit object name will be “myCustomObject”.and this object will be used on jsp.
package com.custom.defined.object;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagData;
import javax.servlet.jsp.tagext.TagExtraInfo;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.tagext.VariableInfo;
/**
*
* @author CodingLoding.com
*
*/
public class DefineObjectsTag extends TagSupport {
MyCustomClass myCustomObject;
public int doStartTag() throws JspException {
System.out.println("inside dostarttag....");
String[] implicitObjects = new String[] { "myCustomObject", };
// Set attributes only once
for (int i = 0; i < implicitObjects.length; i++) {
if (pageContext.getAttribute(implicitObjects[i]) == null) {
pageContext.setAttribute(implicitObjects[i],
resolveVariable(implicitObjects[i]),
PageContext.PAGE_SCOPE);
}
}
return SKIP_BODY;
}
private Object resolveVariable(String pName) {
if ("myCustomObject".equals(pName)) {
return new MyCustomClass();
}
return null;
}
/*
* TagExtraInfo class for providing the implicit object information to the tag
*/
public static class TEI extends TagExtraInfo {
public VariableInfo[] getVariableInfo(TagData tagData) {
VariableInfo[] info = new VariableInfo[] { new VariableInfo(
"myCustomObject",
"com.custom.defined.object.MyCustomClass", true,
VariableInfo.AT_BEGIN) };
return info;
}
}
}
Step3: Create the tld file including those information. i have created codingloading.tld file inside /WEB-INF/tld folder. and mention the tag class and tag Extra info class.
<?xml version="1.0" encoding="ISO-8859-1" ?>
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>CodingLoading</shortname>
<uri>http://www.codingloading.com/customDefinedObject</uri>
<tag>
<name>defineObjects</name>
<tagclass>com.custom.defined.object.DefineObjectsTag</tagclass>
<tei-class>com.custom.defined.object.DefineObjectsTag$TEI</tei-class>
<bodycontent>empty</bodycontent>
</tag>
</taglib>
Step4: Mention that tld file in web.xml to use the tag in jsp.
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <display-name>sample-jsp-implicit-objects</display-name> <jsp-config> <taglib> <taglib-uri>http://www.codingloading.com/customDefinedObject</taglib-uri> <taglib-location>/WEB-INF/tags/codingloading.tld</taglib-location> </taglib> </jsp-config> <welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> </web-app>
Step5: use the defineObjects tag on jsp to use custom implicit Objects on that jsp.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib uri="http://www.codingloading.com/customDefinedObject" prefix="CodingLoading" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<CodingLoading:defineObjects/>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Home Page</title>
</head>
<body>
<h1>Custom implicit objects in jsp hello</h1>
<%
out.print(myCustomObject.callMethod());
%>
<h1>Hello</h1>
</body>
</html>
Step6: Run the application you will see the custom implicit object is calling the method.
Image may be NSFW.
Clik here to view.
you can download the sample project sample-jsp-implicit-objects here.
Enjoy…
The post Creating custom implicit objects in jsp appeared first on CodingLoading.