This week's book giveaway is in the Kotlin forum.
We're giving away four copies of Kotlin in Action and have Dmitry Jemerov & Svetlana Isakova on-line!
See this thread for details.
Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

How do you write a stand alone function for use in a JSP page?  RSS feed

 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I swear this sounds way too simple a question to be asked even in the beginner forum, but I must...
How do you write a stand alone function to be called in a JSP page? I know that typically in Java you write it as part of a class... but JSPs don't appear to use classes as I just write everything in the .jsp page and it compiles to HTML through Tomcat. However, there are segments of code that I use over and over again which I just copy and paste at the moment.
There MUST be an easier way to do this. I am currently using JSPs for database access (no problems) but am an overall beginner with JSP. Any help / suggestions / examples would be apprecaited.
Thanks!
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Each JSP compiles to a separate class, so there are classes involved. If you are reusing the code chunks in the same JSP, then you can declare a method to appear in the JSP class:

Note the !
If you want to use your code in several JSPs, you can either create a new class containing the method and tell your JSPs to inherit from that class rather than JSPServlet, or put your method in a completely different class and refer to it from your JSPs. The second case is usually easier, you just have to remember to "import" the appropriate class (see the <@page import='...'> directive)
Has that helped ?
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tossed this at the top to test it out..

Here is the error it gives upon compilation by Tomcat...

I have used the out.write() function before... elsewhere in that file as a matter of fact.
[ January 30, 2003: Message edited by: Jeff Grant ]
 
RAJESH RASTOGI
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear Jeff Grant,
out is the defined variable of PrintWriter class in jsp. its scope is local n itz only in _jspService().
u r declaring a global method named blah().
so it's displaying the compile-time error.
hope this will help u.
 
Frank Carver
Sheriff
Posts: 6920
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you note, in my example, I passed in the Writer as a parameter. I often find that this is a useful technique if you need to write to the page from a separate method.
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have no idea how to pass the writer in as a parameter... all I am looking to do is write a function. Can't anyone give a basic example?
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66184
146
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No need to get testy. Frank gave you a basic example. Nothing in your post indicated that you were a rank beginner in Java and didn't know how to pass parameters.
You would define your method as:

and then would invoke it within your JSP body as

Another technique, which avoids passing the Writer around, could be

and then

Of course, when you get down to it, the best way to deal with repeating elements in JSP pages is to define custom tags. But little methods like these have their place too.
hth,
bear
 
Jeff Grant
Ranch Hand
Posts: 169
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, sorry about my previous reply.
Here is the function I finally tweaked to get to work with your help.

...called from...

Thanks again. I am not new to Java... just semi-new to JSPs.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!