Put scriptlet lines in a class

Discussion in 'Java' started by teser3@hotmail.com, Aug 29, 2007.

  1. Guest

    I have this scriptlet working in a JSP and was wondering how I can put
    it in a class file and call it in my JSP.


    <jsp:useBean id="pageinfo" class="mypackage.PageInfo" scope="session"/
    >

    ....
    <%
    if (pageinfo!=null) {
    if (pageinfo.isFirst()) {
    out.println("<a href=first.jsp>first</a>");
    }
    }
    %>




    Would this be close because I am not sure how to create the method?

    //in the class file
    package mypackage;
    import mypackage.pageinfo;

    public String mymethod()
    {
    if (pageinfo!=null) {
    if (pageinfo.isFirst()) {
    out.println("<a href=first.jsp>first</a>");
    }
    }
    }


    Then call it like this in JSP?

    <% mymethod(); %>



    I dont have JSTL or Struts on this Tomcat 4.1.27 container that I am
    working on.
     
    , Aug 29, 2007
    #1
    1. Advertising

  2. Daniel Pitts Guest

    On Aug 29, 8:49 am, "" <> wrote:
    > I have this scriptlet working in a JSP and was wondering how I can put
    > it in a class file and call it in my JSP.
    >
    > <jsp:useBean id="pageinfo" class="mypackage.PageInfo" scope="session"/
    >
    > ...
    > <%
    > if (pageinfo!=null) {
    > if (pageinfo.isFirst()) {
    > out.println("<a href=first.jsp>first</a>");
    > }}
    >
    > %>
    >
    > Would this be close because I am not sure how to create the method?
    >
    > //in the class file
    > package mypackage;
    > import mypackage.pageinfo;
    >
    > public String mymethod()
    > {
    > if (pageinfo!=null) {
    > if (pageinfo.isFirst()) {
    > out.println("<a href=first.jsp>first</a>");
    > }
    >
    > }
    > }
    >
    > Then call it like this in JSP?
    >
    > <% mymethod(); %>
    >
    > I dont have JSTL or Struts on this Tomcat 4.1.27 container that I am
    > working on.


    You'd have to pass a PageInfo object to "mymethod". I would suggest
    having mymethod return a string, rather than call out.println. that
    way you can call it like:

    <%= MyUtilsClass.mymethod(pageinfo) %>



    public class MyUtilsClass {

    public static String mymethod(PageInfo pageinfo) {
    if (pageinfo != null && pageinfo.isFirst()) {
    return "<a href=\"first.jsp\">first</a>
    }
    return "";
    }
    }
     
    Daniel Pitts, Aug 29, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Phil Powell
    Replies:
    0
    Views:
    1,639
    Phil Powell
    Nov 21, 2003
  2. Robert Mark Bram

    Share vars between scriptlet and jstl?

    Robert Mark Bram, Mar 20, 2005, in forum: Java
    Replies:
    1
    Views:
    15,322
    Ryan Stewart
    Mar 20, 2005
  3. Hats
    Replies:
    3
    Views:
    466
    Spartanicus
    Jun 20, 2005
  4. ramakrishna
    Replies:
    0
    Views:
    1,251
    ramakrishna
    May 4, 2006
  5. Replies:
    1
    Views:
    389
    Aj-India
    May 15, 2006
Loading...

Share This Page