473,546 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ 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 24495
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.c om> wrote in message
news:cf******** *************** ***@posting.goo gle.com...
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
14913
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) { document.images.src = eval("mt" +menu+ ".src") } alert("imgOff_hidemenu"); hideMenu=setTimeout('Hide(menu,num)',500);
5
2814
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 invisible() { alert("invisible() called"); } }
2
7665
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 like mine to run immediately after it. So in the code below, what JS would i need to add to my "myfile.inc" page so that I could guarantee this...
2
12668
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
931
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 make function calls from my ASP.NET project. Is it possible to do this? If so, how...can I somehow compile them into my project? Thanks,...
8
5083
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 constructors (these three I knew about), but also conditional function definitions, as described in...
3
3633
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' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take...
0
1174
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 Jobs'!1:65536,5,FALSE) How can I incorporate this function so it will insert the result of the lookup into the "To" box ? ...
3
1659
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 computers which cannot, but either way they are all on netscape), the current onclick command looks like this: <A onclick="wopen('page url', 'popup', 670,...
0
7504
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7694
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7947
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7461
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5360
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5080
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1921
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 we have to send another system

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.