Trouble with importing javaBean. pls help.

Discussions

Web tier: servlets, JSP, Web frameworks: Trouble with importing javaBean. pls help.

  1. Trouble with importing javaBean. pls help. (5 messages)

    Hello ppl. I've had about 6 months experience with JSP/Servlets/JavaBeans/Tomcat. I've only EVER used JSPs not Servlets or JavaBeans (i do know how to write them tho). I've decided to extend my applications by implementing JavaBeans. My senario is that I have a login.jsp page which handles admin login requests to the admin control panel. I took some general validation logic out of the login.jsp and converted it into a java class (javaBean). But im having trouble importing the bean. Im using the following tags to implement the bean:

    <%@ page import="validation" %>
    <jsp:useBean id="validation" class="validation" />

    When I load the login.jsp page, tomcat throws 4 errors:

    org.apache.jasper.JasperException: Unable to compile class for JSP

    An error occurred at line: 23 in the jsp file: /myadmin/index.jsp

    Generated servlet error:
        [javac] Compiling 1 source file

    C:\Program Files\Apache Group\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\myadmin\index_jsp.java:7: '.' expected
    import validation;
                     ^
    C:\Program Files\Apache Group\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\myadmin\index_jsp.java:65: cannot resolve symbol
    symbol : class validation
    location: class org.apache.jsp.myadmin.index_jsp
          validation validation = null;
          ^



    An error occurred at line: 23 in the jsp file: /myadmin/index.jsp

    Generated servlet error:
    C:\Program Files\Apache Group\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\myadmin\index_jsp.java:67: cannot resolve symbol
    symbol : class validation
    location: class org.apache.jsp.myadmin.index_jsp
            validation = (validation) pageContext.getAttribute("validation", PageContext.PAGE_SCOPE);
                          ^



    An error occurred at line: 23 in the jsp file: /myadmin/index.jsp

    Generated servlet error:
    C:\Program Files\Apache Group\Tomcat 5.0\work\Catalina\localhost\_\org\apache\jsp\myadmin\index_jsp.java:70: cannot resolve symbol
    symbol : class validation
    location: class org.apache.jsp.myadmin.index_jsp
                validation = (validation) java.beans.Beans.instantiate(this.getClass().getClassLoader(), "validation");
                              ^
    4 errors


    at org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:120)
    at org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:307)
    at org.apache.jasper.compiler.Compiler.generateClass(Compiler.java:420)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:460)
    at org.apache.jasper.compiler.Compiler.compile(Compiler.java:444)
    at org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:603)
    at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:300)
    at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:293)
    at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:240)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:856)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:286)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:256)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:562)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:974)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:205)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:562)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:974)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:177)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
    at org.apache.catalina.valves.ErrorDispatcherValve.invoke(ErrorDispatcherValve.java:171)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:172)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:149)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:562)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:974)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:156)
    at org.apache.catalina.core.StandardValveContext.invokeNext(StandardValveContext.java:151)
    at org.apache.catalina.core.StandardPipeline.invoke(StandardPipeline.java:562)
    at org.apache.catalina.core.ContainerBase.invoke(ContainerBase.java:974)
    at org.apache.coyote.tomcat5.CoyoteAdapter.service(CoyoteAdapter.java:203)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:637)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:484)
    at org.apache.tomcat.util.net.TcpWorkerThread.runIt(PoolTcpEndpoint.java:568)
    at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:631)
    at java.lang.Thread.run(Thread.java:536)

    I have the java class (javaBean) in the $CATALINA_HOME/webapps/ROOT/WEB-INF/ folder, ive also tried the /WEB-INF/classes folder but no success. Whats going wrong? do I need to edit the web.xml file or define any packages or something? pls help becuase I have no practical experience with JavaBeans. Thanks.
  2. I think it's looking for your class in a package called validation,
    ie your JavaBean may have full path of validation.YourBean .....
    Make sure it's in your class path. Be sure to restart the server after adding any classes.

    If it's a class put it in WEB-INF/classes, again of your class is in package, you have to store it with the expanded directory stcucture.

    If it's in a jar put it in WEB-INF/lib
  3. index.jsp in a folder called myadmin (webapps/ROOT/myadmin) and the class is in webapps/ROOT/WEB-INF/classes. What do I need to do to get the bean working?
  4. Does your class has a pakage name. If not try to make your class belong to a package. For Example:

    package myutils;
    public class MyValidator {
       ........
    }

    Put this class in webapps/ROOT/WEB-INF/classes/myutils/MyValidator.class

    In your JSP page import use:
    "myutils.*"
  5. Thanks, That worked nicely :) But what I can't understand is that why can't I just have the bean in the classes/ folder w/o being in a package?
  6. bcos thatz the way JSP engine has been designed!