By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,538 Members | 1,130 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,538 IT Pros & Developers. It's quick & easy.

Incorporating a function in a jsp

P: n/a
Dear all,
I've written a jsp which is very large and monolithic, and what I want
to do is refactor it to provide functions that can operate on global
jsp variables.

For instance, what I have at the moment, in pseudo code is:

MyJsp.jsp
<% a = some code
b = more code %>

//perform operations on a and b

But what I really want is something like:

MyJsp.jsp
<% a = some code
b = more code %>

<!-- HTML here -->

<!-- function here -->

<%public void MyFunc()
{

// operations on a and b here
}
%>

but when I do this latter approach, I get a "token not found",
referring to a missing ;, +, ++ etc. in my code, and relating to my
defined function. People have suggested using a class declaration
within the jsp that contains the functions, or maybe using a bean, but
it seems slightly problematic to use global jsp references when doing
it this way.

Cheers

Paul
Jul 17 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a
You need to get a clearer picture of how a JSP is handled by the Engine.
JSP's get translated into a Servlet, and the code goes into the Servlet's
'service' method.

When you use <% <type> a; %> you create a local variable to the scope of the
'service' method of the resultant Servlet. If you want a variable with scope
outside the 'service' method, use: <%! <type> a; %>

Note the '!', this tells the JSP compiler that the enclosed code should be
declared at the class level of the JSP.

Same thing to define a method: <%! <type> <name>() {...} %>. These methods
will then be able to access the member variables. However, you have a very
serious danger here. Remember that a Servlet/JSP is not thread safe, so, if
two requests come in for that JSP, there is a danger of trampling on your
'global' variables.

A better solution to this is to use a model-view-controller (MVC)
architecture like Struts with view helpers.

"Paul Lee" <pa**@paullee.com> wrote in message
news:cf**************************@posting.google.c om...
Dear all,
I've written a jsp which is very large and monolithic, and what I want
to do is refactor it to provide functions that can operate on global
jsp variables.

For instance, what I have at the moment, in pseudo code is:

MyJsp.jsp
<% a = some code
b = more code %>

//perform operations on a and b

But what I really want is something like:

MyJsp.jsp
<% a = some code
b = more code %>

<!-- HTML here -->

<!-- function here -->

<%public void MyFunc()
{

// operations on a and b here
}
%>

but when I do this latter approach, I get a "token not found",
referring to a missing ;, +, ++ etc. in my code, and relating to my
defined function. People have suggested using a class declaration
within the jsp that contains the functions, or maybe using a bean, but
it seems slightly problematic to use global jsp references when doing
it this way.

Cheers

Paul

Jul 17 '05 #2

P: n/a
Hi Paul --

You don't want to do it this way. This is a poor design. An important
aspect of J2EE development is the separation of presentation logic from
business logic. A JSP page should contain as little business logic as
possible, and in most cases it's possible to remove all scriptlet code.

If you are trying to refactor this, try to apply some design patterns.
Or, even just create some helper objects that you call from the JSP. To
truly eliminate all embedded Java code from the JSP, create custom tags
to use your helper objects (or use the <jsp:useBean> tags).

Joe

Paul Lee wrote:
Dear all,
I've written a jsp which is very large and monolithic, and what I want
to do is refactor it to provide functions that can operate on global
jsp variables.

For instance, what I have at the moment, in pseudo code is:

MyJsp.jsp
<% a = some code
b = more code %>

//perform operations on a and b

But what I really want is something like:

MyJsp.jsp
<% a = some code
b = more code %>

<!-- HTML here -->

<!-- function here -->

<%public void MyFunc()
{

// operations on a and b here
}
%>

but when I do this latter approach, I get a "token not found",
referring to a missing ;, +, ++ etc. in my code, and relating to my
defined function. People have suggested using a class declaration
within the jsp that contains the functions, or maybe using a bean, but
it seems slightly problematic to use global jsp references when doing
it this way.

Cheers

Paul

Jul 17 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.