JSP Help Needed

Discussion in 'Java' started by sdavids, Jun 8, 2006.

  1. sdavids

    sdavids Guest

    Hey everyone I am trying to learn JSP although keep running into a
    brick wall. I set up Tomcat 5.5 which is running fine, but I am trying
    to import a Java class into a JSP page and it doesnt seem to work, this
    is what my class looks like:

    package steve;

    public class TestMe {

    public TestMe(){
    System.out.println("INSIDE CONSTRUCTOR");
    }

    public int getNumber(){
    return 10;
    }
    }

    I saved the class as TestMe, and saved it in "\WEB-INF\classes\steve"

    Now my JSP code is as follows:

    <html>
    <%@ page import="steve.TestMe.*" %>
    <body>
    <%
    TestMe temp = new TestMe();
    out.print("NUM=" + temp.getNumber());
    %>
    </body>
    </html>

    The error which I recieve when I run the jsp file is: TestMe cannot be
    resolved to a type.

    Any help would be phenominal.
    sdavids, Jun 8, 2006
    #1
    1. Advertising

  2. sdavids

    Guest

    There is a problem in your import.

    <%@ page import="steve.TestMe.*" %>

    The above line should be like

    <%@ page import="steve.*" %>
    or
    <%@ page import="steve.TestMe %>

    sdavids wrote:
    > Hey everyone I am trying to learn JSP although keep running into a
    > brick wall. I set up Tomcat 5.5 which is running fine, but I am trying
    > to import a Java class into a JSP page and it doesnt seem to work, this
    > is what my class looks like:
    >
    > package steve;
    >
    > public class TestMe {
    >
    > public TestMe(){
    > System.out.println("INSIDE CONSTRUCTOR");
    > }
    >
    > public int getNumber(){
    > return 10;
    > }
    > }
    >
    > I saved the class as TestMe, and saved it in "\WEB-INF\classes\steve"
    >
    > Now my JSP code is as follows:
    >
    > <html>
    > <%@ page import="steve.TestMe.*" %>
    > <body>
    > <%
    > TestMe temp = new TestMe();
    > out.print("NUM=" + temp.getNumber());
    > %>
    > </body>
    > </html>
    >
    > The error which I recieve when I run the jsp file is: TestMe cannot be
    > resolved to a type.
    >
    > Any help would be phenominal.
    , Jun 8, 2006
    #2
    1. Advertising

  3. sdavids

    sdavids Guest

    p.s. I didnt do anything with the web.xml file, I dont know if that is
    a problem or not, I really dont know what to do with it - I was under
    the impression you only needed to modify it if you wanted to do
    scriplets and such.
    sdavids, Jun 8, 2006
    #3
  4. sdavids

    sdavids Guest

    I still get the same errors after changing the import statements with
    both of yours
    sdavids, Jun 8, 2006
    #4
  5. sdavids

    Guest

    Did you compile your java file? If not compile your java file and put
    it in WEB-INF\classes\steve.

    Also import should be like.

    <%@ page import="steve.*" %>
    or
    <%@ page import="steve.TestMe"%>

    sdavids wrote:
    > I still get the same errors after changing the import statements with
    > both of yours
    , Jun 8, 2006
    #5
  6. sdavids schrieb:
    > Hey everyone I am trying to learn JSP although keep running into a
    > brick wall. I set up Tomcat 5.5 which is running fine, but I am trying
    > to import a Java class into a JSP page and it doesnt seem to work, this
    > is what my class looks like:
    >
    > package steve;
    >
    > public class TestMe {
    >
    > public TestMe(){
    > System.out.println("INSIDE CONSTRUCTOR");
    > }
    >
    > public int getNumber(){
    > return 10;
    > }
    > }
    >
    > I saved the class as TestMe, and saved it in "\WEB-INF\classes\steve"

    You have to save the class file as "TestMe.class", not as "TestMe".


    --
    Thomas
    Thomas Fritsch, Jun 8, 2006
    #6
  7. sdavids

    sdavids Guest

    Yes I have compiled the class, and I placed TestMe.class as well as
    TestMe.java into the directory. Is there anything that I need to do in
    the web..xml file in order for the jsp and java file to talk to one
    another?
    sdavids, Jun 8, 2006
    #7
  8. sdavids

    Timo Stamm Guest

    sdavids schrieb:
    > Yes I have compiled the class, and I placed TestMe.class as well as
    > TestMe.java into the directory.


    Into which directory? It should be in

    <webroot>/WEB-INF/classes/steve/TestMe.class


    The JSP page can be anywhere in <webroot> (except restricted
    directories). The import statement must be

    steve.TestMe

    or

    steve.*


    If that doesn't work, try importing a standard Java class like
    java.util.ArrayList. If that works, add

    static void main(String[] args) {System.out.println("hello world");}

    to TestMe and try to run it with javac.

    In other words: isolate the problem.


    > Is there anything that I need to do in
    > the web..xml file in order for the jsp and java file to talk to one
    > another?


    No.


    Timo
    Timo Stamm, Jun 8, 2006
    #8
  9. sdavids

    sdavids Guest


    > The JSP page can be anywhere in <webroot> (except restricted
    > directories). The import statement must be
    >
    > steve.TestMe


    When I do that I get the error:
    Generated servlet error:
    Only a type can be imported. steve.TestMe resolves to a package


    > or
    >
    > steve.*
    >

    Yet again it wont let me declare a new item of the TestMe type within
    the JSP file, saying "TestMe cannot be resolved to a type"
    sdavids, Jun 8, 2006
    #9
  10. sdavids wrote:
    >>The JSP page can be anywhere in <webroot> (except restricted
    >>directories). The import statement must be
    >>
    >> steve.TestMe

    >
    >
    > When I do that I get the error:
    > Generated servlet error:
    > Only a type can be imported. steve.TestMe resolves to a package

    To me this error message sounds pretty much that you might have a
    *directory* "steve/TestMe" in your WEB-INF/classes area, instead of
    having a *file* "steve/TestMe.class". I would suggest to carefully check
    your WEB-INF/classes directory tree and mnaybe to delete and rebuild it
    from scratch.
    >
    >
    >
    >>or
    >>
    >> steve.*
    >>

    >
    > Yet again it wont let me declare a new item of the TestMe type within
    > the JSP file, saying "TestMe cannot be resolved to a type"
    >


    --
    Thomas
    Thomas Fritsch, Jun 9, 2006
    #10
  11. sdavids

    sdavids Guest

    Nope, my folder setup is as follows: "\WEB-INF\classes\steve" and
    within the steve directory, I have TestMe.class and TestMe.java
    sdavids, Jun 9, 2006
    #11
  12. sdavids

    giotto41

    Joined:
    Oct 3, 2006
    Messages:
    1
    Hey, I dont' know if you found out the problem or not but I just had the same problem, did everything in my power to figure it out... finally I rebooted tomcat and voila... the world was right again. I'm pretty sure that's all you're going to have to do.
    Now every time I update a class, I have to reboot tomcat so it can register that I've changed a library file. There's probably a way to do it without rebooting but who cares. It works.
    giotto41, Oct 3, 2006
    #12
  13. sdavids

    ostatnibard

    Joined:
    May 8, 2009
    Messages:
    1
    I had the same problem. :captain:

    I had all nesessary .jar's included (commons-fileupload-1.2.1.jar AND commons-io-1.4.jar).

    However the error ocurred still.

    In my case the reason for the error was that one of dependency .jar's (commons-io-1.4.jar)

    was included, BUT not yet phisically present in my "lib" folder: \build\web\WEB-INF\lib\



    SOLUTION: hit "Clear and Build" !
    and the error is gone.
    ostatnibard, May 8, 2009
    #13
  14. sdavids

    vaibhav.pansambal

    Joined:
    Oct 30, 2012
    Messages:
    2
    basically i m also facing the same problem can anyone help me out
    vaibhav.pansambal, Oct 30, 2012
    #14
  15. sdavids

    vaibhav.pansambal

    Joined:
    Oct 30, 2012
    Messages:
    2
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"%>
    <html>
    <head>
    <%@ page import ="net.sf.mpxj.ProjectFile ,net.sf.mpxj.MPXJException ,net.sf.mpxj.Resource ,
    net.sf.mpxj.Task ,
    net.sf.mpxj.mpp.MPPReader ,
    net.sf.mpxj.reader.ProjectReader;" %>

    <title>Insert title here</title>
    </head>
    <body>


    <%
    final class Mpx
    {

    public void mppReader()
    {
    ProjectReader reader = new MPPReader();
    ProjectFile project;
    System.out.print("before reading");
    try
    {

    project = reader.read("b4ubuild_sample_2k-03.mpp");
    //b4ubuild_sample_2k-03
    }
    catch (MPXJException e)
    {

    e.printStackTrace();
    }

    for (Resource resource : project.getAllResources())
    {

    System.out.println("Resource: " + resource.getName() + " \n(Unique ID=" +resource.getUniqueID() + ")");
    System.out.println("\n");
    }


    for (Task task : project.getAllTasks())
    {
    System.out.println("Task: " + task.getName() + " ID=" + task.getID() + " Unique ID=" +
    task.getUniqueID()+ "\nStart Date : " +task.getStart()+"\nFinish Date : "+task.getFinish()+ "\nDuration : "
    +task.getDuration().getDuration());
    System.out.println("\n");
    }

    System.out.println("after reading");
    }
    public void main(String[] args)
    {
    Mpx fileReader = new Mpx();
    fileReader.mppReader();
    }
    }%>
    </body>
    </html>
    vaibhav.pansambal, Oct 30, 2012
    #15
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Miles Davenport
    Replies:
    4
    Views:
    548
    Andrew Thompson
    Feb 6, 2004
  2. Replies:
    0
    Views:
    4,350
  3. Replies:
    5
    Views:
    473
  4. ros
    Replies:
    1
    Views:
    562
    Richard Senior
    May 12, 2007
  5. kal
    Replies:
    0
    Views:
    369
Loading...

Share This Page