You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Martin Wells <ma...@dot.net.au> on 2000/07/25 01:32:35 UTC

JSP Methods?

Hi all,

I'd like to be able to output certain segments of HTML multiple times, in
different cases.

The example below would be perfect, alas I think it's not supported by the
JSP implementation (since a declaration is handled before the HTML pass).
Correct me if I'm wrong please. :)

Example:

<%! void printHello()
    { %>
	<B>HELLO WORLD</B>
<%! } %>

<HTML>
<BODY>
	I'm saying, <%= printHello() %>
</BODY>
</HTML>


Any ideas on achieving something like this?



==============================================================
_/_/  Martin J. Wells            http://www.tasman-studios.com
_/  CTO, Tasman Studios        mailto:marty@tasman-studios.com
==============================================================


Re: JSP Methods?

Posted by Eduardo Pelegri--Llopart <Ed...@eng.sun.com>.
This cannot be done in JSP 1.1.  Variations of this request appear with
some frequency.  I cluster them into "JSP as tags" (although that is
more of a solution than a requirement) in that what you want is to take
a collection of tags (+ template text) and encapsulate it
(efficiently).  That requirement has been and it is being discussed in
the context of JSP 1.2.

	- eduard/o 

Martin Wells wrote:
> 
> Hi all,
> 
> I'd like to be able to output certain segments of HTML multiple times, in
> different cases.
> 
> The example below would be perfect, alas I think it's not supported by the
> JSP implementation (since a declaration is handled before the HTML pass).
> Correct me if I'm wrong please. :)
> 
> Example:
> 
> <%! void printHello()
>     { %>
>         <B>HELLO WORLD</B>
> <%! } %>
> 
> <HTML>
> <BODY>
>         I'm saying, <%= printHello() %>
> </BODY>
> </HTML>
> 
> Any ideas on achieving something like this?
> 
> ==============================================================
> _/_/  Martin J. Wells            http://www.tasman-studios.com
> _/  CTO, Tasman Studios        mailto:marty@tasman-studios.com
> ==============================================================

Re: JSP Methods?

Posted by William Brogden <wb...@bga.com>.

Martin Wells wrote:
> 
> Hi all,
> 
> I'd like to be able to output certain segments of HTML multiple times, in
> different cases.
> 
> The example below would be perfect, alas I think it's not supported by the
> JSP implementation (since a declaration is handled before the HTML pass).
> Correct me if I'm wrong please. :)
> 
> Example:
> 
> <%! void printHello()
>     { %>
>         <B>HELLO WORLD</B>
> <%! } %>
> 
=======

why not
   String printHello() { return "<b>HELLO WORLD</b>" ;}

since
  <%= printHello() %>

outputs the String value of the method call.

-- 
WBB - wbrogden@bga.com  Chief Scientist, LANWrights, Inc.
Java Programmer Certification information and mock exam
at  http://www.lanw.com/java/javacert/

RE: JSP Methods?

Posted by "Rob S." <rs...@home.com>.
Hi Martin,

When compiled into a servlet, that method has no access to the response
object.  I think the two alternatives are:

1) Return a String and <%=...%> it, like William suggested.
2) Add a param for the response object, and then inside your method,
response.write everything.

HTH!

- r

> -----Original Message-----
> From: Martin Wells [mailto:marty@dot.net.au]
> Sent: July 24, 2000 4:33 PM
> To: tomcat-user@jakarta.apache.org
> Subject: JSP Methods?
>
>
> Hi all,
>
> I'd like to be able to output certain segments of HTML multiple times, in
> different cases.
>
> The example below would be perfect, alas I think it's not supported by the
> JSP implementation (since a declaration is handled before the HTML pass).
> Correct me if I'm wrong please. :)
>
> Example:
>
> <%! void printHello()
>     { %>
> 	<B>HELLO WORLD</B>
> <%! } %>
>
> <HTML>
> <BODY>
> 	I'm saying, <%= printHello() %>
> </BODY>
> </HTML>
>
>
> Any ideas on achieving something like this?
>
>
>
> ==============================================================
> _/_/  Martin J. Wells            http://www.tasman-studios.com
> _/  CTO, Tasman Studios        mailto:marty@tasman-studios.com
> ==============================================================
>