jsp ... how to solve

P

pet0etie

i'm currently making some tests with jsp's
but one thing i don't really know how to solve is this :

suppose i pass a parameter (string) to a certain page and i want to convert
that string into something else

solution : i just make a little function at the top of the jsp-page that
makes the conversion

now ... if i have to make that same conversion on several pages i can write
the same function on every page ...

i can make a java class that looks like this, but that doesn't seems to be
the ideal solution

public class FunctionABC {
public FunctionABC() {
//...
}

public String convertABC(String str) {
if ( str != null ) {
return "";
}
return str.toUpperCase();
}
}

i guess it's more a basic java-thing-question but i don't know how to
implement :|
someone who can help ?

thanks in advance,
pet0etie
 
K

kaeli

now ... if i have to make that same conversion on several pages i can write
the same function on every page ...

Or you could use a bean or a taglib like normal people. :p

j/k

Do look into making beans and custom tags. That's pretty much what they're
for. Well, among a crapload of other things. *g*
Get scriptlets out of your JSP code whenever possible.

--
--
~kaeli~
Those who get too big for their britches will be exposed in
the end.
http://www.ipwebdesign.net/wildAtHeart
http://www.ipwebdesign.net/kaelisSpace
 
S

shakah

pet0etie said:
i'm currently making some tests with jsp's
but one thing i don't really know how to solve is this :

suppose i pass a parameter (string) to a certain page and i want to convert
that string into something else

solution : i just make a little function at the top of the jsp-page that
makes the conversion

now ... if i have to make that same conversion on several pages i can write
the same function on every page ...

i can make a java class that looks like this, but that doesn't seems to be
the ideal solution

public class FunctionABC {
public FunctionABC() {
//...
}

public String convertABC(String str) {
if ( str != null ) {
return "";
}
return str.toUpperCase();
}
}

i guess it's more a basic java-thing-question but i don't know how to
implement :|
someone who can help ?

thanks in advance,
pet0etie

You have quite a few options, actually:

1. as you noted create the method at the top of the page and copy it
from page to page:
<%!
public static String convertABC(String str) {
...
}
%>

2. create an "include file" (say convert.inc), place the method noted
above in it, and include it at the top of each JSP page:
<%@ include file="convert.inc" %>

3. create a Java class with the method in it, compile it, and deploy
the resulting .class file(s) in your webapp's WEB-INF/class directory:
public class ConvertUtilities {
public static String convertABC(String s) {
...
}
}

4. same as (3), but create a JAR file containing the .class file(s) and
deploy the resulting JAR file via your webapp's WEB-INF/lib directory ;

5. create a tag library for it, deploy it via the webapp's WEB-INF/tld
directory.

I guess a purist's view would be to prefer 5 to 4 to 3 to 2 to 1, but
you can minimize the compile/deploy step by "progressing" from 1 to 2
to 3 to 4 as you develop your code.
 
P

pet0etie

tnx for the replies both of u

shakah said:
You have quite a few options, actually:

1. as you noted create the method at the top of the page and copy it
from page to page:
<%!
public static String convertABC(String str) {
...
}
%>

2. create an "include file" (say convert.inc), place the method noted
above in it, and include it at the top of each JSP page:
<%@ include file="convert.inc" %>

3. create a Java class with the method in it, compile it, and deploy
the resulting .class file(s) in your webapp's WEB-INF/class directory:
public class ConvertUtilities {
public static String convertABC(String s) {
...
}
}

4. same as (3), but create a JAR file containing the .class file(s) and
deploy the resulting JAR file via your webapp's WEB-INF/lib directory ;

5. create a tag library for it, deploy it via the webapp's WEB-INF/tld
directory.

I guess a purist's view would be to prefer 5 to 4 to 3 to 2 to 1, but
you can minimize the compile/deploy step by "progressing" from 1 to 2
to 3 to 4 as you develop your code.
 
R

Rivky

skagah covered a lot of the options but if you're not well versed in
tag libraries, i would suggest options 2 or 3:

Creat some kind of Utilities class. Similar to what you describe.
Except you don't need a constructor. And all your methods will be
STATIC. So something like this:

package mynewpackge.name;

public class StringManipulator {

public static String convertString(String s) {
//...
return.....
}

Then:
1) Create a jar file with all your "Utitility" type classes and put it
in the Web-INF/lib folder, as skahah suggested. Or you can just add it,
along with the packaging path, to the WEB-INF/class directory.

2) In the jsp pages: import the new Class and use it like this:
StringManipulator.convertSting(myString);

The reason why I suggest this is once you have the class created and
somehow make it accessible (thru the suggestions above). You can import
it in any jsp and use it easily without getting into the whole taglib
thing for this one use. Also, as you need more types of other
manipulation methods, just add them to the class you created,
recompile, and use in same way.
 
P

pet0etie

Creat some kind of Utilities class. Similar to what you describe.
Except you don't need a constructor. And all your methods will be
STATIC. So something like this:

package mynewpackge.name;

public class StringManipulator {

public static String convertString(String s) {
//...
return.....
}

Then:
1) Create a jar file with all your "Utitility" type classes and put it
in the Web-INF/lib folder, as skahah suggested. Or you can just add it,
along with the packaging path, to the WEB-INF/class directory.

2) In the jsp pages: import the new Class and use it like this:
StringManipulator.convertSting(myString);

The reason why I suggest this is once you have the class created and
somehow make it accessible (thru the suggestions above). You can import
it in any jsp and use it easily without getting into the whole taglib
thing for this one use. Also, as you need more types of other
manipulation methods, just add them to the class you created,
recompile, and use in same way.

great suggestion Rivky... i was looking for something like this
i'll try it out in the near future ... thanks for the help

pet0etie
 

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

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top