Sure, I use the same technique sometimes as well.
As an alternative to returning a String you could also pass the bean a
PrintWriter or use a StringBuffer.
That way you avoid excessive sting addition (which is bad for performance).
It's better than a design where you pass request and/or response to your
bean, because that would make your classes too much dependent on the
'servlet' context.
Geert Van Damme
> -----Original Message-----
> From: A mailing list about Java Server Pages specification and reference
> [mailto:[EMAIL PROTECTED]]On Behalf Of Garcia, Cristina
> Sent: dinsdag 6 juni 2000 12:58
> To: [EMAIL PROTECTED]
> Subject: Avoiding Java Code in the JSP Page.
>
>
> Hi gurus!
>
> trying to avoid the Java code inside the JSP page, I thought
> about writting
> getter methods in a Bean so that it returns a String containing HTML code
> together with values I had to calculate. What do you think about the idea?
>
> Thanks, Cristina.
>
> Before: (In Calendar.jsp)
>
> %@ page import="java.util.*" %>
> <%! int hDay %>
>
> <% Calendar ch = Calendar.getInstance();
> ch.add(Calendar.DATE,7);
> %>
>
> ...
> <select name="seDH" >
> <% for (int i=1; i<32;i++) {
> if (i==hDay) { %>
> <option selected><%=i %>
> <% }else{ %>
> <option><%=i %>
> <% }} %>
> </option></select>
>
> After: (In Calendar.jsp):
>
> <jsp:useBean id="FlyDate" class="FlyDateBean" />
> ...
> <select name="seDH" >
> <jsp:getProperty name="FlugDatum" property="hDays" />
> </select>
>
> (In FlyDateBean.java):
>
> import java.util.*;
>
> public class FlyDateBean {
>
> private int hDays;
> ...
> public String getHDays () {
> String HDays = "";
> for (int i=1; i<32; i++) {
> if (i==this.getHDay()) {
> HDays = HDays + "<option selected>"+ Integer.toString(i);
> }
> else {
> HDays = HDays + "<option>" + Integer.toString(i);
> }
> }
> return (HDays);
> }
>
> ==================================================================
> =========
> To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff
> JSP-INTEREST".
> Some relevant FAQs on JSP/Servlets can be found at:
>
> http://java.sun.com/products/jsp/faq.html
> http://www.esperanto.org.nz/jsp/jspfaq.html
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
> http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets
>
>
===========================================================================
To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST".
Some relevant FAQs on JSP/Servlets can be found at:
http://java.sun.com/products/jsp/faq.html
http://www.esperanto.org.nz/jsp/jspfaq.html
http://www.jguru.com/jguru/faq/faqpage.jsp?name=JSP
http://www.jguru.com/jguru/faq/faqpage.jsp?name=Servlets