Link to home
Start Free TrialLog in
Avatar of agsingh
agsingh

asked on

Taglibs increasing the size of JSP.

Hi,
For my internaionalization project i have used taglibs for conversion of property file keys.

Since there is lot of data to be converted, i have to use lot of tags. Excess use of Tag (50-60 in number) has increased the size of my jsp when its parsed to java. And i am getting java.lang.VerifyError: Illegal Jump or branch start

How can i use my tag class effectively so that its not overhead to the JSP
Avatar of agsingh
agsingh

ASKER

Here is my Tag Class
========================
import java.io.IOException;

import java.text.MessageFormat;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletContext;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.BodyTagSupport;


public class MessageTag extends BodyTagSupport
{
    // instance variables used during processing
    private String               _key = null;
    private String               _value = null;
    private ResourceBundle       _bundle = null;
    private boolean              _debug = false;

    // these are reused for each message tag; luckily tags are thread-safe
    private MessageFormat  _messageFormat = new MessageFormat("");
    private List           _arguments = new ArrayList();

    /**
     *  sets the key to use when looking up the value in the bundle
     */
    public final void setKey(String key)
    {
        _key = key;
    }

    /**
     *  sets the bundle to an actual ResourceBundle object<br>
     *  defined on the page by the i18n:bundle tag
     */
    public final void setBundle(ResourceBundle aBundle)
    {
        _bundle = aBundle;
    }

    /**
     *  @return the bundle to use
     */
    private ResourceBundle getBundle() throws JspException
    {
            if(_bundle == null)
            {
                  BundleTag bundleTag = (BundleTag)this.findAncestorWithClass(this,BundleTag.class);
                  if (bundleTag != null)
                  {
                        System.out.println("from bundle if");
                        return bundleTag.getBundle();
            }
            }
        return _bundle;
    }

    /**
     * Turn debugging log messages on or off
     */
    public final void setDebug(boolean value)
    {
        _debug = value;
    }

    /**
     *  clears the argument list so that sub tags can call addArgument
     *  clears out any previous key and value in case key is not provided,
     *  or the value is null
     */
    public final void release()
    {
        super.release();
        _key = null;
        _value = null;
        _bundle = null;
    }


      /**
       * To Print debug statements when _debug is true
       */
      private void debugLog(String message)
      {
            if(_debug)
            {
                        ServletContext sc = pageContext.getServletContext();
                        sc.log(message);
            }
      }


  /**
   *  locates the bundle and gets the value
   */
  public final int doStartTag() throws JspException
    {
       // Reset value for resource bundle key
       _value = null;

       // ensure we have a key
        if ( _key == null ) {
            throw new JspTagException("i18n:message tag requires a key attribute.");
        }

        ResourceBundle bundle = this.getBundle();
        if ( bundle == null ) {
            throw new JspTagException(
                "i18n:message tag, no bundle available for use.");
        }


        // see if the bundle has a value, if not, we default to the tag contents
        try {
            _value = bundle.getString(_key);
            debugLog("i18n:message tag: template for " + _key + " is: " + _value);
        } catch (java.util.MissingResourceException e) {
            ServletContext sc = pageContext.getServletContext();
            sc.log("i18n:message tag, value not found for key:" + _key);
        }

        return EVAL_BODY_TAG;
    }

    /**
     *  Performs the proper runtime substitution. If an id attribute was
     *  specified, then it is assumed that this tag is merely defining a
     *  string variable; otherwise output is provided.
     */
    public final int doEndTag() throws JspException
    {
        try {
            // if the value is null, use the body content
            if ( _value == null ) {
                _value = bodyContent.getString();
                bodyContent.clear();
            }
                  // print the value to the JspWriter
                  this.pageContext.getOut().print(_value);

        } catch (java.io.IOException e)
        {
            throw new JspTagException("i18n:message tag IO Error: " + e.getMessage());
        }

        // only process the body once
        return EVAL_PAGE;
    }

}
Avatar of agsingh

ASKER

Here is my Tag Class
========================
import java.io.IOException;

import java.text.MessageFormat;

import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import java.util.ResourceBundle;

import javax.servlet.ServletContext;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTag;
import javax.servlet.jsp.tagext.BodyTagSupport;


public class MessageTag extends BodyTagSupport
{
    // instance variables used during processing
    private String               _key = null;
    private String               _value = null;
    private ResourceBundle       _bundle = null;
    private boolean              _debug = false;

    // these are reused for each message tag; luckily tags are thread-safe
    private MessageFormat  _messageFormat = new MessageFormat("");
    private List           _arguments = new ArrayList();

    /**
     *  sets the key to use when looking up the value in the bundle
     */
    public final void setKey(String key)
    {
        _key = key;
    }

    /**
     *  sets the bundle to an actual ResourceBundle object<br>
     *  defined on the page by the i18n:bundle tag
     */
    public final void setBundle(ResourceBundle aBundle)
    {
        _bundle = aBundle;
    }

    /**
     *  @return the bundle to use
     */
    private ResourceBundle getBundle() throws JspException
    {
            if(_bundle == null)
            {
                  BundleTag bundleTag = (BundleTag)this.findAncestorWithClass(this,BundleTag.class);
                  if (bundleTag != null)
                  {
                        System.out.println("from bundle if");
                        return bundleTag.getBundle();
            }
            }
        return _bundle;
    }

    /**
     * Turn debugging log messages on or off
     */
    public final void setDebug(boolean value)
    {
        _debug = value;
    }

    /**
     *  clears the argument list so that sub tags can call addArgument
     *  clears out any previous key and value in case key is not provided,
     *  or the value is null
     */
    public final void release()
    {
        super.release();
        _key = null;
        _value = null;
        _bundle = null;
    }


      /**
       * To Print debug statements when _debug is true
       */
      private void debugLog(String message)
      {
            if(_debug)
            {
                        ServletContext sc = pageContext.getServletContext();
                        sc.log(message);
            }
      }


  /**
   *  locates the bundle and gets the value
   */
  public final int doStartTag() throws JspException
    {
       // Reset value for resource bundle key
       _value = null;

       // ensure we have a key
        if ( _key == null ) {
            throw new JspTagException("i18n:message tag requires a key attribute.");
        }

        ResourceBundle bundle = this.getBundle();
        if ( bundle == null ) {
            throw new JspTagException(
                "i18n:message tag, no bundle available for use.");
        }


        // see if the bundle has a value, if not, we default to the tag contents
        try {
            _value = bundle.getString(_key);
            debugLog("i18n:message tag: template for " + _key + " is: " + _value);
        } catch (java.util.MissingResourceException e) {
            ServletContext sc = pageContext.getServletContext();
            sc.log("i18n:message tag, value not found for key:" + _key);
        }

        return EVAL_BODY_TAG;
    }

    /**
     *  Performs the proper runtime substitution. If an id attribute was
     *  specified, then it is assumed that this tag is merely defining a
     *  string variable; otherwise output is provided.
     */
    public final int doEndTag() throws JspException
    {
        try {
            // if the value is null, use the body content
            if ( _value == null ) {
                _value = bodyContent.getString();
                bodyContent.clear();
            }
                  // print the value to the JspWriter
                  this.pageContext.getOut().print(_value);

        } catch (java.io.IOException e)
        {
            throw new JspTagException("i18n:message tag IO Error: " + e.getMessage());
        }

        // only process the body once
        return EVAL_PAGE;
    }

}
Avatar of Mick Barry
what does your jsp look like?
Avatar of agsingh

ASKER

pasting a portion of my JSP ..if it help
==========================================

<span id="alert"><center><font size=4 color="red">
<%if(msg!=null && !msg.equals("")) { %>
<i18n:message key='<%="tops.text." + ((String)(cc_abObj.getParams("uc15ErrMsg")))%>' debug="true" ></i18n:message><br>
 <%}%>

<%if(msg1!=null && !msg1.equals("")) { %>
<i18n:message key='<%="tops.text." + ((String)(cc_abObj.getParams("SuccMsg")))%>' debug="true" ></i18n:message>
 <i18n:message key='<%="tops.listbox." + ((String)vecCollectShift.get(vecCollectShift.indexOf(strShiftName)+1))%>' debug="true" ></i18n:message>
 <i18n:message key='tops.text.Successfully' debug="true" ></i18n:message>
<!--%=(cc_abObj.getParams("excludeDays"))%-->
 <i18n:message key='tops.text.From' debug="true" ></i18n:message>  <i18n:formatDate value='<%=((String)(cc_abObj.getParams("startDate")))%>' />
 <i18n:message key='tops.text.To' debug="true" ></i18n:message>  <i18n:formatDate value='<%=((String)(cc_abObj.getParams("endDate")))%>' /><br>
 <%}%>

<%if(msg2!=null && !msg2.equals("")) {
     out.println(msg2+"<br>");
}%>

</font></center></span>

<table id="tableRow" border="1" cellspacing="2" cellpadding="2" align="center">
<tr>
<td><b><i18n:message key="tops.text.SupervisorName" debug="true" ></i18n:message></b></td>
<td>
<select multiple  size="3"  name="strlSupervisor">
<option value = "ALL"><i18n:message key="tops.listbox.SelectAll" debug="true" ></i18n:message></option>
     <%
     if(vecSupverisorList != null)
     {
               for(int i=0;i<vecSupverisorList.size();i++)
                    {
                              strEmpNo=(String)vecSupverisorList.elementAt(i);
                              strEmpName=(String)vecSupverisorList.elementAt(++i);
               %>
               <option value="<%=strEmpNo%>"

               <%
                         if(strlSupervisor != null)
                         {
                              for(int k=0;k<strlSupervisor.length;k++)
                              {
                                   if(strEmpNo.equalsIgnoreCase(strlSupervisor[k]))
                                   {
                                        out.println("selected");
                                   }
                              }
                         }
               %>>
               <%=strEmpName%></option>
               <%
                    }
               }
               %>
</select>
</td>
<td>
<input type="button" id="topsButton" name="collectEmp" value="<i18n:message key="tops.button.GetEmployee" debug="true" >Get Employees</i18n:message>" onClick="callSuperVisor(this.form)";>
</td>
</tr>
</table>

<%
Vector vecWorkerList = (Vector)cc_abObj.getObject("vecWorkerList");
if(vecWorkerList != null)
{
if(vecWorkerList.size() != 0)
{
%>
<table id="tableRow" border="1" cellspacing="2" cellpadding="2" align="center">
<tr>
          <td align="left" >
          <table id="tableRow" border="1" cellspacing="2" cellpadding="2" align="center">
          <tr valign = "top">
          <td valign = "top"><b><i18n:message key="tops.text.EmployeeNameNumber" debug="true" >Employee Name/Number</i18n:message></b></td>
          </tr>
====================================
Have a look at the generated Java to see what is causing the exception.
ASKER CERTIFIED SOLUTION
Avatar of doddjames
doddjames

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of agsingh

ASKER

as a workaround i am using <%=myBundle.getString( "myKey" )%>... But i am getting error in a very unexpected fashion. Same file which is working fine, if it changed a bit and happen to recompile gives me Verify error.

I am pasting converted java file for tag
+++++++++++++++++++++++++++++++++
release() is called for every invocation, apparently its not recycling
=================================
// begin message custom tag block... //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
com.gepower.gees.tops.i18n.MessageTag _com_gepower_gees_tops_i18n_MessageTag_0 = null; //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
/*** declare AT_BEGIN TagExtra Vars here ***/ //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
try { // begin instantiate/release try/catch/finally block... //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
_com_gepower_gees_tops_i18n_MessageTag_0 = new com.gepower.gees.tops.i18n.MessageTag(); //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
_com_gepower_gees_tops_i18n_MessageTag_0.setPageContext(pageContext); //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
_com_gepower_gees_tops_i18n_MessageTag_0.setParent(_com_gepower_gees_tops_i18n_BundleTag_0); //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
_com_gepower_gees_tops_i18n_MessageTag_0.setDebug(Boolean.valueOf(weblogic.utils.StringUtils.valueOf("true")).booleanValue()); //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
_com_gepower_gees_tops_i18n_MessageTag_0.setKey(weblogic.utils.StringUtils.valueOf("tops.text.AssignShift")); //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
int _int_1 = _com_gepower_gees_tops_i18n_MessageTag_0.doStartTag(); //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
if (_int_1 == Tag.EVAL_BODY_INCLUDE) { //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
    throw new JspTagException("Since tag class com.gepower.gees.tops.i18n.MessageTag implements BodyTag, it cannot return Tag.EVAL_BODY_INCLUDE"); //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
} //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
/*** sync AT_BEGIN TagExtra Vars here ***/ //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
if (_int_1 != Tag.SKIP_BODY) { // begin !SKIP_BODY... //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
    if (_int_1 != Tag.EVAL_BODY_INCLUDE) { //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
      out = pageContext.pushBody(); //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
      _com_gepower_gees_tops_i18n_MessageTag_0.setBodyContent((BodyContent)out); //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
    } //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
    try { // begin pushBody try/finally  //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
      _com_gepower_gees_tops_i18n_MessageTag_0.doInitBody(); //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
      do { //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
          /*** declare & sync NESTED TagExtra Vars here ***/ //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
          //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
          out.print("Assign Shift");
          //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
      } while (_com_gepower_gees_tops_i18n_MessageTag_0.doAfterBody() == BodyTag.EVAL_BODY_TAG); //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
    } finally { // end pushBody try/finally //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
      if (_int_1 != Tag.EVAL_BODY_INCLUDE) //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
      out = pageContext.popBody(); //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
    } //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
} // end !SKIP_BODY //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
if (_com_gepower_gees_tops_i18n_MessageTag_0.doEndTag() == Tag.SKIP_PAGE) return; //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
} catch (java.lang.Exception _java_lang_Exception_0) { // instantiate/release try/catch/finally //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
throw new ServletException("runtime failure in custom tag 'message'", _java_lang_Exception_0); //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
} finally { // instantiate/release try/catch/finally block... //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
if (_com_gepower_gees_tops_i18n_MessageTag_0 != null) _com_gepower_gees_tops_i18n_MessageTag_0.release(); //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
} //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
//end message custom.... //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
/*** declare & sync AT_END TagExtra Vars here ***/ //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
/*** sync AT_BEGIN TagExtra Vars here ***/ //[ /tops3/tnl/jsp/tops3AssignShift.jsp; Line: 69]
Avatar of agsingh

ASKER

I m using weblogic 510 SP 8

The other approach you could use would be to contextualize your i18n lookups, e.g.

<i18n:bundle>
    <i18n:lookup key="myKey"/>
    <i18n:whatever/>
</i18n:bundle>

Your child tags get the loaded bundle from the parent.

As an aside, many people have written i18n libraries, so why not use one?

http://jakarta.apache.org/taglibs/doc/i18n-doc/intro.html
Avatar of agsingh

ASKER

This is what i am doing..
<%@ taglib uri="tops-i18n.tld" prefix="i18n" %>
<i18n:bundle id="_bundle" debug="true" baseName="properties.tops3.i18n.language">
.
.
.

<i18n:message key="myKey" debug="true" >Assign Shift</i18n:message>
.
.


</i18n:bundle>
message tag is getting bundle from the bundle tag

I am using jakarta taglibs, with minor modification
-Ag

Ah, sorry.  I thought you were writing your own.

The verify error must be being caused by a fault in weblogic's jsp compiler.  BEA do make reference to this problem..

"-noTryBlocks

If a JSP file has numerous or deeply nested custom JSP tags and you receive a java.lang.VerifyError exception when compiling, use this flag to allow the JSPs to compile correctly."

According to what I've read on the net, you should be able to configure weblogic.xml to supply this parameter to the compiler:

<jsp-param>
  <param-name>compileFlags</param-name>
  <param-value>"-noTryBlocks"</param-value>
</jsp-param>




Avatar of agsingh

ASKER

thats a relief..!!
I have not deployed my app as a webapplication so not using web.xml, using weblogic.properties
Any idea how to pass -noTryBlocks through it,
Also can u give me a ref where u found this fault in weblogic JSP compiler.

Thanks,

Hmm.. That option won't work with a WL5.1 jspc (need WL6.0+).  They didn't address the problem with a compiler option until 6.0.

http://www.weblogic.com/docs51/classdocs/API_jsp.html#compiler
http://edocs.bea.com/wls/docs60/jsp/reference.html#57794

You could also try finding a different jsp compiler and configuring WL to use that instead.  Maybe it'll produce more efficient code.

The root problem seems to be that your generated class files are too big.  The elimination of all the try/catch blocks generated by the jsp compiler would help (if you could do it with WL5.1, which you don't seem to be able to :-( ), but failing that you're just going to have to slim down or refactor your JSP into more manageable chunks and <jsp:include/> bits and bobs here and there.

http://www.caucho.com/support/resin-interest/0005/0251.html

Avatar of agsingh

ASKER

getting lots of problems when compiling using java weblogic.jspc
[jspc] parsed .\tnl\jsp\calledbackreport.jsp in 661 ms.
[jspc] generated java file c:\abhi\jsp_servlet\__46_\_tnl\_jsp\_calledbackreport.java, invoking java compiler:
Exec failed .. exiting

:(
Avatar of agsingh

ASKER

didnt get my answer..perhaps it was a wrong implementation of Taglibs.
There is lot behind the scenes. Thanks James for clearing out some of them..!!