473,480 Members | 1,968 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Incorporating a function in a jsp

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
2 24488
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
14898
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
5
2805
by: phil_gg04 | last post by:
Dear Javascript Experts, Opera seems to have different ideas about the visibility of Javascript functions than other browsers. For example, if I have this code: if (1==2) { function...
2
7658
by: laredotornado | last post by:
Hello, I am looking for a cross-browser way (Firefox 1+, IE 5.5+) to have my Javascript function execute from the BODY's "onload" method, but if there is already an onload method defined, I would...
2
12660
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
1
924
by: ezelasky | last post by:
I have been handed a group of .asp javascript pages that will provide a set of javascript objects and computational functions, no controls or html. I need to be able to create these objects and...
8
5073
by: Olov Johansson | last post by:
I just found out that JavaScript 1.5 (I tested this with Firefox 1.0.7 and Konqueror 3.5) has support not only for standard function definitions, function expressions (lambdas) and Function...
3
3621
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
0
1170
by: mikelever | last post by:
Hi, i have the following sendmail macro. However I need the "To" section to be the result of a lookup in a worksheet. The specific function that i need it to be is: =VLOOKUP(A837,'Tenacity...
3
1653
by: topherknowles | last post by:
Hello, I have a number of links opening in a fixed window that certain users cannot access because they are on netscape 4.6 (I think this can handle the links and it is actually their ancient...
0
7059
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7103
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6758
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7010
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
4499
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3011
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3003
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1311
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
572
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.