Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Put scriptlet lines in a class

Reply
Thread Tools

Put scriptlet lines in a class

 
 
teser3@hotmail.com
Guest
Posts: n/a
 
      08-29-2007
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.

 
Reply With Quote
 
 
 
 
Daniel Pitts
Guest
Posts: n/a
 
      08-29-2007
On Aug 29, 8:49 am, "tes...@hotmail.com" <tes...@hotmail.com> 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 "";
}
}

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Common file which contains scriptlet code... gautam.chavan@gmail.com Java 1 05-15-2006 10:58 AM
jsp:include is not allowing me write scriptlet between <jsp:param> tags ramakrishna Java 0 05-04-2006 12:49 PM
Questions: No PHP, but SSI and Scriptlet Hats HTML 3 06-20-2005 12:01 PM
Share vars between scriptlet and jstl? Robert Mark Bram Java 1 03-20-2005 08:38 PM
Tag's handler class bsf.scriptlet not found in classpath - help Phil Powell Java 0 11-21-2003 02:33 PM



Advertisments