Put scriptlet lines in a class

T

teser3

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.
 
D

Daniel Pitts

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 "";
}
}
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top