File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes JSP and the fly likes Tomcat 7 compile error for simple JSP/Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Tomcat 7 compile error for simple JSP/Class " Watch "Tomcat 7 compile error for simple JSP/Class " New topic
Author

Tomcat 7 compile error for simple JSP/Class

ben fran
Greenhorn

Joined: Nov 01, 2012
Posts: 7
Hi,

I am a newbie to Java technologies, and so my question might sound very simple. I searched a little and got some tips to solve my problem in some forums, but they didn’t work. So I am seeking coderanch people help.
Please have a look at it, and see if I am missing any obvious steps(I don’t think I am, but the task is so basic I have my doubts).

Objective:
o To run a JSP page which calls a java class, both residing on tomcat server
o Then proceed to run a JSP page which calls a java class, which in turn calls a jdbc driver class, residing on tomcat server

Environment
o Server environment:Linux
o Server:Tomcat7.0..27
o IDE: none


JSP (all it does is display a string)
o

JSP location
o TOMCAT/webapps/hello/hello.jsp

Java class(all it does is return a string)
o

Java class location: TOMCAT/webapps/hello/WEB-INF/classes/HelloWorld/HelloWorld.class
ava class compile action: javac -classpath TOMCAT/lib/servlet-api.jar:. -d /home/user/ HelloWorld.java

WEB.XML
o


Actions:
o Copy java class to directory
o Clean TOMCAT/work directory
o Shutdown TOMCAT
o Startup TOMCAT
o Open server:8080/hello/hello.jsp


Issue:
• HTTP Status 500
• org.apache.jasper.JasperException: Unable to compile class for JSP: An error occurred at line: 14 in the generated java file The import HelloWorld cannot be resolved An error occurred at line: 8 in the jsp file: /hello.jsp HelloWorld cannot be resolved 5: </HEAD> 6: <BODY> 7: <H1>Hello World</H1> 8: String is : <%= HelloWorld.HelloWorld.display() %> 9: </BODY> 10: </HTML> Stacktrace: org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:102)



There is no syntax error in calling PackageName.ClassName.StringReturningMethod() in JSP
The class is in package folder.

What is causing it?

Thanks,


I hope I didn’t give too much useless details. :)
But, I may have put something wrong in config as well. So better to put all the details iMO.





Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61764
    
  67

Welcome to the Ranch!

OK, first a few basic things:
  • Don't use all uppercase for HTML markup. It's a sure sign of a n00b. Use all lowercase.
  • Java scriptlets in JSPs have been obnsolete and discredited for over 10 years now (the constructs delimited by <%). You should be using modern techniques such as the JSTL and EL.

  • If you back up a bit and apply the 2nd of the suggestions listed above, your question becomes entirely moot as you would not be trying to directly access a Java class from the JSP in the first place.

    Is this new code, or are you trying to fix legacy code? If the former, you are certainly heading down the wrong path. (Analogy: trying to use vacuum tubes to build an HDTV.) If the latter, then you obviously need to work with what you've got.

    Regardless, I always recommend that newcomers to Java web apps read the following articles:
  • The Secret Life of JSPs
  • The Front Man

  • The first makes sure that you really understand what JSP is all about. The second addresses modern application structure in a Java web app.


    [Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61764
        
      67

    If you continue on your current path (not recommended, as discussed above) then the problem is that your class isn't being imported. Is the class file in the right place?

    Another issue: "HelloWorld" while syntactically valid is not an acceptable package name by Java conventions. Package names should be all lowercase (and simply repeating the name of the class isn't very descriptive).
    ben fran
    Greenhorn

    Joined: Nov 01, 2012
    Posts: 7
    Hi Bear Bibeault,

    I appreciate your reply. I have seen while looking at some java issues, this forum is very helpful.


    The java class is designed or rather written in this way:
    *Package: HelloWorld(I see what you meant about naming)
    *ClassName: HelloWorld(what else?)
    *Class Location: TOMCAT/webapps/hello/WEB-INF/classes/HelloWorld/HelloWorld.class

    So it appears to be in the correct location. Unless I am wrong in assuming WEB-INF/classes/(PACKAGE)/(CLASS) is the location where the TOMCAT servlet engine looks for classes which are imported by the JSP page.
    Thats one reason I said it might be a config error, but I really dont know what is there to configure here?

    I have to use JSP, for not this project :), but the reason why I am trying to get this working :)

    Thanks for the links. That will help.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61764
        
      67

    ben fran wrote:
    *Class Location: TOMCAT/webapps/hello/WEB-INF/classes/HelloWorld/HelloWorld.class

    Yup. Looks correct. Where is the JSP file?

    I have to use JSP, for not this project :), but the reason why I am trying to get this working :)

    Not getting this. Who said not to use JSP? And "for not this project"?
    ben fran
    Greenhorn

    Joined: Nov 01, 2012
    Posts: 7
    Hi Bear Bibeault,

    Let me give the full structure

    TOMCAT/webapps/hello is my root directory for the sample web app (let us call is BASE)
    there is
    BASE/hello.jsp (so my URL is server:8080/hello/hello.jsp)
    BASE/WEB-INF/web.xml
    BASE/WEB-INF/classes/HelloWorld/HelloWorld.class
    BASE/META-INF/MANIFEST.MF (empty file)



    My broad objective is to run a JSP form which refers to some data access classes, which in turn refer to a jdbc driver jar.
    Before getting my hands on them , I just want to make sure I can run a JSP on TOMCAT which refers to an external class.
    That is why I am trying this.

    thanks,
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61764
        
      67

    Again, barking up the wrong tree. JSPs should not be accessing Java classes. But if you insist on getting this working (though it will be not very useful going forward), is the method static? I've been assuming so, but...

    [Edit: never mind, I see that it is, thinking...]
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61764
        
      67

    Looking at the deployment descriptor, I assume that the leading "o" characters are an artifact of posting?

    Why do you have a servlet declaration for the JSP? Not needed. And superfluous things tend to have a way of sticking their legs out and tripping you when you least expect it. I'd clean that up before progressing.
    ben fran
    Greenhorn

    Joined: Nov 01, 2012
    Posts: 7
    Looking at the deployment descriptor, I assume that the leading "o" characters are an artifact of posting? : they are lists from MS Word

    Why do you have a servlet declaration for the JSP? Not needed. so just a web.xml will do with no elements? that was one thing I suspected may cause my issue. But it appears it wasnt.


    And superfluous things tend to have a way of sticking their legs out and tripping you when you least expect it. I'd clean that up before progressing.
    agree, let me clean web.xml and try to run it.
    UPDATE: same error
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61764
        
      67

    ben fran wrote:Looking at the deployment descriptor, I assume that the leading "o" characters are an artifact of posting? : they are lists from MS Word

    MS Word? Please tell me you are not writing code in MS Word.

    so just a web.xml will do with no elements?

    For what you are trying to do so far, yes. All you need is a skeleton deployment descriptor.

    let me clean web.xml and try to run it.

    It may not have anything to do with the issue, but it's best to clean out cruft to narrow things down.
    ben fran
    Greenhorn

    Joined: Nov 01, 2012
    Posts: 7
    No no, the code was actually writen (THIS CODE :)) in vi editor.
    But, when I was preparing the post for coderanch, I put stuff in MS word.

    Cleaned it up. Now I understand that you meant the web.xml by the term deployment decriptor

    New web.xml
    ben fran
    Greenhorn

    Joined: Nov 01, 2012
    Posts: 7
    One really basic question(little distractive)

    Is there a decent article/tutorial/(NOT 100 PAGES LONG) book chapter
    for getting
    *A simple JSP form
    *A JDBC using bean
    to work together on apache tomcat 7?


    Will that be lesser pain than to make JSP access classes?

    I am more concerned with the HOW part, because, IMO , this is the problem even now.

    thanks,
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61764
        
      67

    ben fran wrote:But, when I was preparing the post for coderanch, I put stuff in MS word.

    I beg of you not to do that. I'll send you brownies or a pie if you promise never ever ever to do that again!

    Now I understand that you meant the web.xml by the term deployment decriptor

    Yes.

    New web.xml

    Suggestions:
  • Fix the formatting for proper indentation. Makes it much easier to inspect.
  • Either provide a description, or remove the element. Remeber what I said about cruft? If it isn't needed, ditch it.
  • DTD 2.3? That's for Tomcat 4! Check out the JspFaq for a more modern deployment descriptor format (or it may be somewhere in the Tomcat docs).

  • Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61764
        
      67

    ben fran wrote:
    for getting
    *A simple JSP form
    *A JDBC using bean
    to work together on apache tomcat 7?


    Firstly, stop thinking just Tomcat. What you write should conform to the Servlet/JSP specs and not be tied to a particular server.

    Next, the 100-mile overview:
  • Never directly address a JSP, hit a servlet that serves as the page controller.
  • The controller does all the "prep work" (think of it as the sous chef for the JSP) and forwards to the JSP.
  • DB access occurs in the model layer, not the controller or view. So never in a servlet.
  • The servlet uses a model class to get stuff from the DB. It places it in request scope prior to forwarding to the JSP. That's where the JSP gets it from. This sort of data is known as a scoped variable.
  • At no time should the JSP ever have scriptlet code in it. JSTL and EL are the modern mechanisms to access the scoped variables that the controller provides.


  • The second atricle I linked to above details all this.
    ben fran
    Greenhorn

    Joined: Nov 01, 2012
    Posts: 7
    you know?
    I will look into JSTL/EL and configuring some sort of a MVC architecture IF THIS PROJECT NEEDS IT, and get back )

    thanks :)
    You have been very helpful

    but, MS word is useful
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61764
        
      67

    but, MS word is useful

    Not for code. Or for posting code.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Tomcat 7 compile error for simple JSP/Class