• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Usebean and class/type

 
Henrik Krievs
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Howdy !

I found an inconsistency in jsp:usebean I can't figure out:

Howcome - the class attribute must be fully-qualified - but the type attribute can "use" an <%@page import ... %> - and thus - only state the class name ??

Rgds, Henri
 
Nitish Bahadur
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe, inadvertently you have used a construct that makes it work.

<%@ page language="java" import="java.util.*" %>
<HTML>
<HEAD>
<TITLE>A JSP USEBEAN APPLICATION</TITLE>
</HEAD>
<BODY BGCOLOR="white">
<jsp:useBean id="col"
scope="session" class="java.util.ArrayList" type="Collection"/>
Class Name = <%= col.getClass().getName() %>
</BODY>
</HTML>

This only works because of the import="java.util.*". Remove that and you will see that it will stop working.

Reason: In Tomcat the equivalent JSP is translated to
...
import java.util.*;
...
col = (Collection) _jspx_page_context.getAttribute("col", PageContext.SESSION_SCOPE);
 
Henrik Krievs
Ranch Hand
Posts: 60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah - thats my point:

Why can the "type" attribute "see" the import statement - but the "class" attribute don't ?

See my point ?

Rgds, Henrik
 
Nitish Bahadur
Ranch Hand
Posts: 118
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you make the following change to $tomcat-home/conf/web.xml
<servlet>
jsp
...
<init-param>
<param-name>errorOnUseBeanInvalidClassAttribute</param-name>
<param-value>false</param-value>
</init-param>
...
</servlet>

<%@ page language="java" import="java.util.*" %>
...
<jsp:useBean id="col" scope="session" class="ArrayList" type="Collection"/>
Class name = <%= col.getClass().getName() %>

Should work too.

The only problem is that we have now moved a lot of checking from compile time to runtime. Additionally, this url might be helpful.
http://article.gmane.org/gmane.comp.jakarta.tomcat.devel/35870
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic