jsp ... how to solve

Discussion in 'Java' started by pet0etie, Apr 5, 2005.

  1. pet0etie

    pet0etie Guest

    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
    pet0etie, Apr 5, 2005
    #1
    1. Advertising

  2. pet0etie

    kaeli Guest

    In article <4Ty4e.56010$-ops.be>,
    enlightened us with...
    >
    > 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
    kaeli, Apr 5, 2005
    #2
    1. Advertising

  3. pet0etie

    shakah Guest

    pet0etie wrote:
    > 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.
    shakah, Apr 5, 2005
    #3
  4. pet0etie

    pet0etie Guest

    tnx for the replies both of u

    "shakah" <> wrote in message
    news:...
    > pet0etie wrote:
    > > 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.
    >
    pet0etie, Apr 6, 2005
    #4
  5. pet0etie

    Rivky Guest

    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.
    Rivky, Apr 6, 2005
    #5
  6. pet0etie

    pet0etie Guest


    > 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
    pet0etie, Apr 7, 2005
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Matthias Nietz
    Replies:
    1
    Views:
    3,446
    VisionSet
    Nov 12, 2003
  2. Raymond DeCampo

    Custom tags and <jsp:plugin> / <jsp:param>

    Raymond DeCampo, Feb 18, 2004, in forum: Java
    Replies:
    0
    Views:
    625
    Raymond DeCampo
    Feb 18, 2004
  3. Timo Nentwig

    JSP 2.0: <jsp:attribute> in <c:if>

    Timo Nentwig, May 10, 2004, in forum: Java
    Replies:
    0
    Views:
    621
    Timo Nentwig
    May 10, 2004
  4. Sean Aitken
    Replies:
    3
    Views:
    11,018
    Alex Hunsley
    Oct 21, 2004
  5. Replies:
    0
    Views:
    4,357
Loading...

Share This Page