• Post Reply Bookmark Topic Watch Topic
  • New Topic

accessing non-static methods from JSP

 
emm raha
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm playing with JSPs on Tomcat. I'm trying to access class members (public classes & members in WEB-INF/classes) from a JSP. It works if the class members are static. However it gives a NullPointerException if a field is not static, and a IncompatibleClassChangeError if a method is not static. Any ideas?
 
emm raha
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tweaked my JSP some more, generating inconsistent error messages. It now accesses non-static members of the class. The change was from:

* Declaring a global variable object of the class. Instantiating it in jspInit(...). Declaring and instantiating it in a declarative (<%! ... %> block. If the object was used elsewhere in the JSP, it was null.

To:

* Declaring and instantiang the object in a non declarative block (i.e. regular <% ... %> code block.

Does anyone know why this is - feel free to tell me to read about JSP/servlet lifecycles since I just took a quick glance.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you understand the difference between using <%! %> versus <% %>?

One creates class-level declarations, while the other generates in-line code in the service method.

Understanding the difference will answer your question.

In fact, the best thing you could do is to find the .java file created on behalf of the JSP by the container. This will show you how a JSP servlet is constructed from your JSP source and will prove very enlightening.
 
emm raha
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have the same understanding of declarative and inline code blocks. But I must be missing something, because I'm still confused as to why an object variable declared and instantiated in the the declarative block would be null/inaccessible in successive inline code blocks. Thanks.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the relevant code snippets (be sure to use the UBB code tags) along with a description of what you think should happen, and the error messages that you are receiving.
 
emm raha
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Frontend.jsp code was something like:


Frontend.jsp is now:

[ May 27, 2004: Message edited by: emm raha ]
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65524
105
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Upon inspection I saw no reason why your first example should casue the backend variable to not be set. And indeed, upon trying a small test of my own (substituting a String object for Backend), worked as expected.

However, what you are doing is very very wrong. You do realize that the JspInit method is called once when the JSP is initialized upon it first reference. All subsequent "hits" to the JSP will not cause this to be called again. Hence the request and response that you pass to the Backend constructor will be out of scope for all but the first invocation of the JSP.

I suspect that the reason your first example causes an error is not because the backend variable is null (have you actually checked this?) but because somewhere in the getParam() method you are referencing out-of-scope references.

What's the purpose of Backend anyways? Seems to me that you are over-engineering something that probably should be in a controller servlet in the first place.
 
emm raha
Ranch Hand
Posts: 57
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. I didn't know jspInit() was called only at first reference, would've helped to do a little reading. But I'm just tooling around right now (no purpose). I'm pretty sure when I was getting the null error and trying to debug, that I had a 'if (backend == null)' check. Thanks for responding.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!