facebook

Simple JSP Application cannot see class [Closed]

  1. MyEclipse IDE
  2.  > 
  3. General Development
Viewing 14 posts - 1 through 14 (of 14 total)
  • Author
    Posts
  • #231307 Reply

    wsheaffer
    Member

    This is probably a pretty simple problem but I can’t seem to find a solution for it. I have created a simple JSP application, the directory structure is correct but my application cannot seem to locate the Java class which is in the src directory. I get the red cross marks and it says the class is undefined.

    Is there a special pathing I must do when I add a class to the project? I am only using it to store form data.

    Thanks in advance for any help.
    🙁

    #231310 Reply

    Scott Anderson
    Participant

    I have created a simple JSP application, the directory structure is correct but my application cannot seem to locate the Java class which is in the src directory.

    It’s not just the directory structure that needs to be correct. Is it a MyEclipse Web Project, or just a plain Java project? If it’s a MyEclipse Web Project, is your workspace set to build automatically (Project > Build Automatically). For information on getting a sample web project running quickly, please see our web project quickstart in the documentation section here: http://www.myeclipseide.com/images/tutorials/quickstarts/webprojects/

    #231313 Reply

    wsheaffer
    Member

    Thanks for your reply. Yes the automatic build is on, I checked it first thing. Yes the tutorial is great and I have got through several of them from start to finish.

    This problem however is rather vexing for me and I am relatively confident that there is a simple solution which I am not aware of.

    The application is simple, an index.jsp calls a html form, GetName.html which in turn uses NextPage.jsp and SaveName.jsp to store the data from the form in the Java class UserData.java.

    There are no syntax errors that I can find and following JSP generates and error:

    <%@ page language=”java” import=”java.util.*” %>
    <jsp:useBean id=”user” class=”UserData” scope=”session”/>
    <HTML>
    <BODY>
    You entered<BR>
    Name: <%= user.getUsername() %><BR>
    Email: <%= user.getEmail() %><BR>
    Age: <%= user.getAge() %><BR>
    </BODY>
    </HTML>

    It says that it cannot resolve the class “UserData”. Yet UserData.java is located in the “src” directory as per the design.

    I am running
    Win XP Pro
    java 1.4.2_08
    Eclipse
    MyEclipse

    Any further suggestions would be greatly appreciated.

    #231320 Reply

    Scott Anderson
    Participant

    I believe you’re going to need an import statement for UserData, just like the one you have for java.util.*. Otherwise, the generated Java doesn’t know what package to look in for it.

    #231335 Reply

    wsheaffer
    Member

    Thank you for your reply.

    I took my project out of the MyEclipse hierarchy and simply wrote it and deployed it by hand to Tomcat.

    It works fine. I then downloaded a similar project with the sample configuration and deployed the associated “war” file to Tomcat. It expanded the war and the application ran fine.

    The issue, it seems is that I do not know to configure either:

    1. An environmental variable, such as a classpath

    2. A project setting within the environment

    3. An installation setting

    This problem only occurs in web apps and the MyEclipse IDE properly identifys that the Java Class which is part of the project, and properly in the src directory cannot be seen and therefore I get an error which states that the class is not identified.

    On jarring up and moving the MyEclipse directory structure to Tomcat, the application terminates when I try to instaniate an object of the Java Class which is clearly a part of my project.

    If you can answer this query I would be grateful. I have spent about 10 hours now with your documentation to no avail.

    Thank you in advance.

    #231336 Reply

    Scott Anderson
    Participant

    When you deploy your application using MyEclipse, are the class files deployed in WEB-INF/classes? Is there any difference you can see between the deployment you did manually and either the packaged or exploded one you did with the IDE? There must be something if one is working and the other is not.

    #231337 Reply

    wsheaffer
    Member

    Yes there is. In the MyEclipse hierarchy you separate source into the src directory and classes go into the WEB-INF/classes directory. In my deployment and in development I have them in the same directory. The problem it seems is this separation of Java code into one directory and the classes into another. This is why I feel I may have some type of system classpath problem, or perhaps the path is not being interpreted correctly from within the IDE.

    Thank you for your prompt reply.

    #231349 Reply

    Scott Anderson
    Participant

    In the MyEclipse hierarchy you separate source into the src directory and classes go into the WEB-INF/classes directory. In my deployment and in development I have them in the same directory.

    That may be true, but Java source does not need to be deployed. As long all the classes are in WEB-INF/classes, your JSPs are under your web root directory in the deployed version, *and* you’ve added the import statements I mentioned in my earlier post then the project should work fine. Are all three of these the case?

    #231416 Reply

    wsheaffer
    Member

    i have done all that you said, refreshed the project to no avail. I still believe this is some sort of build path issue and there is a relatively simple solution to it.

    #231430 Reply

    Scott Anderson
    Participant

    For closure, the project was sent in and the error was that the JSP page did not include an import statment for the Java class being referenced. As I instructed in my second followup post, once the import was added, validation worked as expected.

    #231536 Reply

    wsheaffer
    Member

    Hello my name is Tim Willson, ITS 5 with the Minneosota Department of Finance.

    Warren Shaffer is conducting a training class and we have hit this bean problem.

    On Warren’s machine the project runs correctly but with the student machine we see a 500 error. I’ve zipped the project into a file that I can send you. “org.apache.jasper.JasperException: /SaveName.jsp(28,2) The value for the useBean class attribute UserData is invalid.”

    Basically the

    <%@ page import="us.mn.state.finance.*" %>
      <jsp:useBean id="user" class="us.mn.state.finance.UserData" scope="session"/>

    works correctly in the .jsp file but the

    <%@ page import="us.mn.state.finance.*" %>
      <jsp:useBean id="user" class="UserData" scope="session"/>

    Our setter.jsp file has the following code

    <%@ page import="us.mn.state.finance.*" %>
      <jsp:useBean id="user" class="us.mn.state.finance.UserData" scope="session"/>
      <jsp:setProperty name="user" property="*"/>

    works correctly in the .jsp file and

    <%@ page import="us.mn.state.finance.*" %>
      <jsp:useBean id="user" class="UserData" scope="session"/>
      <jsp:setProperty name="user" property="*"/>

    did not.

    It appears to be a default search path of sorts or a configuration setting in not finding the class.

    -tw

    #231546 Reply

    Scott Anderson
    Participant

    Tim,

    The issue is that, per the JSP spec, import statements are not taken into account when processing tag attributes. They only affect Java scriptlet code. So each time you use your class as a tag attribute, you *must* fully specify it like:

    
    <jsp:useBean id="user" class="us.mn.state.finance.UserData" scope="session"/> 
    

    not like this, since the spec does not support it.

    
    <jsp:useBean id="user" class="UserData" scope="session"/> 
    
    #242948 Reply

    emz005
    Member

    For closure, the project was sent in and the error was that the JSP page did not include an import statment for the Java class being referenced. As I instructed in my second followup post, once the import was added, validation worked as expected.

    What should the import statement look like exactly.

    #242959 Reply

    Scott Anderson
    Participant

    An import of a Java class in a JSP page is done with the ‘page’ directive as shown below:

    
    <%@ page import="java.util.Date" %>
    

    When you’re typing in a a classname in a Java scriptlet in a JSP page, typing ctrl+<space> after the name will add the import for you automatically.

Viewing 14 posts - 1 through 14 (of 14 total)
Reply To: Simple JSP Application cannot see class [Closed]

You must be logged in to post in the forum log in