0

I'm fairly new to Eclipse/JSP/Beans, but I have to make a project where I use Beans in my JSP file. I have two projects. A)Maven project that includes the JSP file B)EJB project with the Beans

I tried to import a bean in my JSP file with the following line:

<%@page import="beans.TestSessionBean"%>

I also added the EJB project to Project A's build path(right click Project A > Build Path > Configure Builde Path >Projects > Add), but that didn't seem to help.

So whenever I try to run my JSP file, I get the following message:

Only a type can be imported. beans.TestSessionBean resolves to a package

After looking around the internet.. it seems like Project A wouldn't include the files (beans) from Project B. At lasts thats how the content of my WAR file looks:

my-webapp
|-- META-INF
|   `-- MANIFEST.MF
|--maven
|  `-- directory
|      `-- directory
|         |--pom.properties
|         |--pom.xml
|-- WEB-INF
|   |-- classes
|   |   |-- files
|   |   |   `-- some_file.java
|   |-- glassfish-web.xml
|   `-- web.xml
`-- index.jsp

but, I saw in another page that its also supposed to include the bean files from Project B, right? (or do I mix up things here?)

I also wanted to know if I might have to edit some XML file to get this stuff working. I keep running into search results that mention the WEB.xml file, but I have no idea if I'm supposed to add anything to it or not. So my WEB.xml looks like this:

    <!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >

    <web-app>
      <display-name>Archetype Created Web Application</display-name>
      <servlet>
        <servlet-name>tesLet</servlet-name>
        <display-name>tesLet</display-name>
        <description></description>
        <servlet-class>servlet.tesLet</servlet-class>
      </servlet>
      <servlet-mapping>
        <servlet-name>tesLet</servlet-name>
        <url-pattern>/tesLet</url-pattern>
      </servlet-mapping>
    </web-app>

I also had two more questions. What happens if I go to the project properties and add a Project to the Project Preferences settings? (I mean if I check one of the check boxes next to a project)

And what does the checkbox do in the "Order and Export" tab under Build Path?

user1047630
  • 21
  • 1
  • 3

1 Answers1

0

Only a type can be imported. beans.TestSessionBean resolves to a package

This is a misleading error. This is under the covers actually a NoClassDefFoundError. This means that the given class isn't present in the classpath.

Now, your projcect structure says

|-- WEB-INF
|   |-- classes
|   |   |-- files
|   |   |   `-- some_file.java

This is however wrong. You shouldn't have .java files in there, but compiled .class files. Make sure that you've a /WEB-INF/classes/beans/TestSessionBean.class. This should fix the particular error.


Unrelated to the concrete problem: attempting to import an EJB in a JSP indicates a major design mistake. I suggest to invest some time in learning servlets. Also, your web.xml specifies Servlet 2.3 which is over a decade old. It's not clear if you're also really deploying to a Servlet 2.3 container or not, but I'd also upgrade it to newest Servlet API version as possible.

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • I get the class thing, but I'm starting to wonder if its a problem with the Maven eclipse plugin. Perhaps, thers a problem with it or I might have to actually configure it somehow to include Project B, even if its in the list of Maven Dependencies. I'm deploying it to JBoss 5.0, but I have no idea which Servlet version its using. I also have no idea how I would go about updeting to a higher Servlet version in eclipse. :| I will look into Servlets, but I would like to know why its bad to use Beans in JSP's. I only kept seeing people stating not to use "useBean", but to use imports. – user1047630 Nov 15 '11 at 14:36
  • I don't do Maven, so I can't go in detail. All you need to ensure is that the Maven-built WAR file contains the proper class file in the proper location. That must solve the particular error you're getting. JBoss 5.0 is a Servlet 2.5 container. As to using beans; an EJB is certainly not "just" a bean. It's a business service bean. Last but not least, it's **both** `jsp:useBean` and `@page import` which must be avoided in JSP. – BalusC Nov 15 '11 at 14:44
  • So it seems like I have to use the MVC.. emm, forgot that term. So that I end up with something like Bean > Servlet > Jsp? Meaning that Beans aren't actually used in JSP's, but the Servlets? I just have a hard time understand what the beans are good for. I read a bit abut them, but I don't understand what they are meant to be used for/in. – user1047630 Nov 15 '11 at 14:52
  • Normal javabeans represent the data model. Enterprise javabeans represent the business model. See also http://stackoverflow.com/questions/1727603/places-where-javabeans-are-used, http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files and http://stackoverflow.com/tags/servlets/info – BalusC Nov 15 '11 at 14:56
  • Thanks, that was some good info. Goes that I will have to change a few things then. :) Just have to figure out the build problem now.. :| – user1047630 Nov 15 '11 at 15:40