0

I'm using Tomcat v 7, and Jess v 7.0

This is the exception I'm getting

root cause 

javax.servlet.ServletException: java.lang.NoSuchMethodError: uges.servlets.MyQuery:       method <init>()V not found
org.apache.jasper.runtime.PageContextImpl.doHandlePageException(PageContextImpl.jav  a:911)
org.apache.jasper.runtime.PageContextImpl.handlePageException(PageContextImpl.java:840)
org.apache.jsp.catalog_jsp._jspService(catalog_jsp.java:121)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)


root cause 

java.lang.NoSuchMethodError: uges.servlets.MyQuery: method <init>()V not found
org.apache.jsp.catalog_jsp._jspService(catalog_jsp.java:69)
org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:70)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:432)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:390)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:334)
javax.servlet.http.HttpServlet.service(HttpServlet.java:722)

and this is my source code of class MyQuery package uges.servlets;

import jess.*;;

public class MyQuery
{

private static QueryResult result;
public MyQuery(Rete engine) throws JessException
{
    getQuery(engine);
}
public QueryResult getQuery(Rete engine) throws JessException
{
    result = engine.runQueryStar("all-products", new ValueVector());
    return result;
}
public String getString(String str) throws JessException
{
    String srtResult;
    srtResult = result.getString(str);
    return srtResult;

}
public Float getFloat(String str) throws JessException
{
    float flt;
    flt = result.getFloat(str);
    return flt;

}
public boolean next() throws JessException
{
    boolean next;
    next = result.next();
    return next;

}
}

and this is the Catalog Servlet package uges.servlets;

import jess.*;
import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;

public class Catalog extends BaseServlet {

public void doGet(HttpServletRequest request,
                   HttpServletResponse response)
    throws IOException, ServletException {
    checkInitialized();

    try {
        String customerId =
            (String) request.getParameter("customerId");
        if (customerId == null || customerId.length() == 0) {
            dispatch(request, response, "/index.html");
            return;
        }

        request.getSession().invalidate();
        HttpSession session = request.getSession();

        session.setAttribute("customerId", customerId);
        session.setAttribute("orderNumber",
                             String.valueOf(getNewOrderNumber()));

        ServletContext servletContext = getServletContext();
        Rete engine = (Rete) servletContext.getAttribute("engine");

        //engine.reset();
          MyQuery result = new MyQuery(engine);
          //engine.runQueryStar("all-products", new ValueVector());
            request.setAttribute("queryResult",result);               

    } catch (JessException je) {
        throw new ServletException(je);
    }

    dispatch(request, response, "/catalog.jsp");
}

private int getNewOrderNumber() throws JessException {
    ServletContext servletContext = getServletContext();
    Rete engine = (Rete) servletContext.getAttribute("engine");
    int nextOrderNumber = 
        engine.executeCommand("(get-new-order-number)").intValue(null);
    return nextOrderNumber;
}

public void destroy() {
    try {
        ServletContext servletContext = getServletContext();
        Rete engine = (Rete) servletContext.getAttribute("engine");
        String factsFileName =
            servletContext.getInitParameter("factsfile");
        File factsFile = new File(factsFileName);
        File tmpFile =
            File.createTempFile("facts", "tmp", factsFile.getParentFile());
        engine.executeCommand("(save-facts " + tmpFile.getAbsolutePath() +
                              " order recommend line-item next-order-number)");
        factsFile.delete();
        tmpFile.renameTo(factsFile);

    } catch (Exception je) {
        // Log error
    } 
}


}

the JSP catalog.jsp

<HTML>
<%@ page import="jess.*" %>
<jsp:useBean id="queryResult" class="uges.servlets.MyQuery" scope="request"/>

<HEAD>
<TITLE>Ordering from Tekmart.com</TITLE>
</HEAD>

<BODY>
<H1>Tekmart.com Catalog</H1>
Select the items you wish to purchase and press "Check Order" to continue.
<FORM action="/Order/recommend" method="POST">
<TABLE border="1">
<TR><TH>Name</TH>
    <TH>Catalog #</TH>
    <TH>Price</TH>
    <TH>Purchase?</TH>
</TR>
<% while (queryResult.next()) {
    String partNum =
            queryResult.getString("part-number");%> 
   <TR>
     <TD><%= queryResult.getString("name") %></TD>
     <TD><%= queryResult.getString("part-number") %></TD>
     <TD><%= queryResult.getFloat("price") %></TD>
     <TD><INPUT type="checkbox" name="items"
                value=<%= '"' + partNum + '"'%>></TD>
   </TR> 
 <% } %>                 
</TABLE>
<INPUT type="submit" value="Check Order">
</FORM>
</BODY>
</HTML>

Any clue will be appreciated. Thanks,

Lama
  • 21
  • 1
  • 5

2 Answers2

2

The JSP engine is trying to instantiate your class via reflection using a no-arg constructor. You have not defined a no-arg constructor, so this error occurs. A bean must have a no-arg constructor. If you need to set properties on the bean, use jsp:set-property.

If you absolutely cannot add a no-arg constructor, then you must instantiate it outside of a jsp:use-bean tag and add it to the proper context yourself, as in

< %   
  pkg.Foo foo = new pcg.Foo(constructor-arg);  
  context.setAttribute("foo", foo);  
% >  

This violates the basic JavaBean requirement for a no-arg constructor.

I believe once it's been created in the proper scope, other JSPs can refer to it with jsp:use-bean.

Jim Garrison
  • 85,615
  • 20
  • 155
  • 190
  • thanks :) but what if I can't add a no-arg constructor .. how can I set properties? could you please give me an example! another thing I've googled a lot and found that such error is because something wrong with jars' versions! any clue?! – Lama May 10 '12 at 20:31
  • Thanks for this answer, but why some beans need to implement Serialize interface? – Plain_Dude_Sleeping_Alone Sep 11 '15 at 05:58
0

You've in the preprocessing servlet already set the queryResult in the request scope.

request.setAttribute("queryResult",result);               

So you do not need to use <jsp:useBean> at all. Remove the following line altogether:

<jsp:useBean id="queryResult" class="uges.servlets.MyQuery" scope="request"/>

Because your servlet has set it as a request attribute, the object is already in EL available by ${queryResult}. But due to the strange design of the class itself, it's hard to properly access it in EL, so you'll ineed need to resort to old school scriptlets. You only need to obtain it first by request.getAttribute("queryResult").

For a better design approach, check the servlets tag wiki page and Show JDBC ResultSet in HTML in JSP page using MVC and DAO pattern.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555