| Author |
How do you write a stand alone function for use in a JSP page?
|
Jeff Grant
Ranch Hand
Joined: Dec 19, 2001
Posts: 169
|
|
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
Joined: Jan 07, 1999
Posts: 6913
|
|
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 ?
|
A Convergent Visionary ~ Frank's Punchbarrel Blog ~ LinkedIn profile
|
 |
Jeff Grant
Ranch Hand
Joined: Dec 19, 2001
Posts: 169
|
|
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
Joined: Jan 20, 2002
Posts: 17
|
|
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
Joined: Jan 07, 1999
Posts: 6913
|
|
|
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
Joined: Dec 19, 2001
Posts: 169
|
|
|
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 opinionated walrus
Marshal
Joined: Jan 10, 2002
Posts: 50691
|
|
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
|
[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
|
 |
Jeff Grant
Ranch Hand
Joined: Dec 19, 2001
Posts: 169
|
|
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.
|
 |
 |
|
|
subject: How do you write a stand alone function for use in a JSP page?
|
|
|