Thanks, I'm glad to hear the positive feedback.
The reason that processRequest can be deprecated if Pagelets exists
is because it becomes redundant to init(PageContext) and is less
flexible.
The init(PageContext) method would be called on every JSP page that
uses the bean, just like processRequest is now.
Here is the flow for .92:
_jspService() {
// bean creation
bean.processRequest(request);
// rest of jsp code
}
The flow with Pagelets would be:
_jspService() {
// bean creation
bean.init(pageContext); // pageContext contains request
// rest of jsp code
bean.destroy();
}
If we kept processRequest it would look like:
_jspService() {
// bean creation
bean.init(pageContext); // pageContext contains request
bean.processRequest(request);
// rest of jsp code
bean.destroy();
}
Everything that happens in processRequest(HttpServletRequest) could
really be done in init(PageContext).
Also Pagelet.init(PageContext) needs to be called on each page even if
the Pagelet is long lived and stored in the session since it needs to
at least get the request. What is really great is that PageContext gives
access to everything.
Here is an almost complete list of the things the PageContext provides:
Object getAttribute(String name)
Enumeration getAttributesInScope(int scope)
int getAttributesScope(String name)
Exception getException()
JspFactory getFactory()
JspWriter getInitialOut()
boolean getNeedsSession()
JspWriter getOut()
int getOutBufferSize()
Servlet getServlet()
ServletConfig getServletConfig()
ServletContext getServletContext()
ServletRequest getServletRequest()
ServletResponse getServletResponse()
HttpSession getSession()
abstract void forward(String relativeUrlPath)
abstract void include(String relativeUrlPath, JspWriter out)
void removeAttribute(String name)
void removeAttributeInScope(String name, int scope)
void setAttribute(String name, Object attribute)
void setAttributeWithScope(String name, Object o, int scope)
boolean isAutoFlush()
boolean isOutBuffered()
With Pagelets you can extend your servlet configuration to your beans.
Consider if the code in the Pagelets.init(PageContext) does different
things based on the ServletContext attributes or the ServletConfig
init parameters. This is not possible with
processRequest(HttpServletRequest).
Going back to the example of a the jdbcConnection pagelet, this pagelet
can open a connection to a database using a JDBC URL that it got from
pageContext.getServletConfig().getInitParameter("com.mydomain.myJdbcUrl").
Regarding Thread-Safety, since a Pagelet could copy objects from the
pageContext for use outside of init(PageContext). The JSP environment
would need to ensure that a Pagelet is in use by only one request at
a time. Locks would not even need to be issued on Pagelets with a scope
of request or page.
Here is an example database Pagelet:
public class JdbcPagelet implements javax.servlet.jsp.Pagelet {
public static final String
JDBC_URL_PARAM = "com.mydomain.myJdbcUrl",
TABLE_PARAM = "tableName";
private PageContext pageContext = null;
private Connection connection = null;
private String tableName = null;
public void init(PageContext pageContext) {
this.pageContext = pageContext;
ServletConfig config = pageContext.getServletConfig();
String jdbcUrl = config.getInitParameter(JDBC_URL_PARAM);
tableName = pageContext.getRequest().getParameter(TABLE_PARAM);
try {
connection = DriverManager.getConnection(jdbcUrl);
} (SQLException e) {
// Oops, something went wrong, Hey I can log it :-)
pageContext.getServletContext().log("init: "+e.toString());
}
}
public String getMyTableRecordCount() {
if (connection == null)
return "[Database Unavailable]";
if (tableName == null)
return "[Which Table?]";
try {
int recordCount = 0;
Statement st = connection.createStatement();
ResultSet rs = st.executeQuery("select count(*) from " +
tableName);
if (rs.next())
recordCount = rs.readInt(1);
rs.close();
st.close();
return Integer.toString(recordCount);
} (SQLException e) {
pageContext.getServletConext().log("getRecordCount:
"+e.toString());
}
return "[RecordCount Not Available]";
}
public void destroy() {
try {
if (connection != null)
connection.close();
} (SQLException e) {
pageContext.getServletConext().log("destroy: "+e.toString());
}
}
}
Now of course the SQL code should be in the init method so that if
getMyTableRecordCount is called multiple times the database is not
hit each time.
More importantly this example shows:
* Configuring itself with the ServletConfig
* Keeping the database connection open for the life of the Pagelet
* Logging Errors
These are 3 thing that cannot currently be done with JSP beans.
-James Klicman
Walter Jerusalinsky wrote:
>
> GOOD! I like it too, I vote for it.-
>
> But I don't think that init() would replace processRequest(), since init()
> should be called when the bean is created and processRequest() should be
> called every request even if the bean has application or session scope.-
>
> Walter Jerusalinsky
===========================================================================
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff JSP-INTEREST". For general help, send email to
[EMAIL PROTECTED] and include in the body of the message "help".