• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Functions in JSP

 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,
I'am working on a web based application, where the JSPs are used as the presentation layer and there are three more layers as given under:
Clientbeans (client side beans(web server))
Server beans (which are deployed at application server)
Businesslogic
database access layer
In JSP's i am making many functions for common functionality, like storing session variable values, initializing variables etc etc.
At present what I've done is: Created a new set of JSPs having only the functions and included these JSPs wherever I want to use those methods.
But is this a efficient way of achieving the functionality. I think this approach is making the client side more thick.
Will it improve the performance, if I put these functions at the
application server side as utility functions (in the EJBs).
This will reduce the number of JSP files and ofcourse code.
Can somebody throw some light on this.
Thanks in advance.
waiting for reply
Shikhar
 
Ranch Hand
Posts: 107
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not sure what you are trying to accomplish but this bit of info may help: since jsp are compiled into servlets you could make a base servlet class where you would put all of you utility functions and have all your jsp extend that class. I would think that the functionality of the functions would determine where they are located. That is, if it is related to presentation layer the functions would reside there. An example of this would be to have methods in your base servlet case that set/get user name from the session. Hope this helps
pjw
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,
Thanks for the tips. This is certainly helpful.
Can you tell me which one of the following approach will lead to better performance. :
1. Put all the functions (in my case 10) in a sigle seperate JSP file and include it in the JSPs , where ever used.
2. Put all the functions in a bean and instantiating the bean in the JSP, where I want to use the function.
can you tell me which is the better approach and also (if you hace patience) why?
Thanks in advance
Shikhar
 
Sheriff
Posts: 7001
6
Eclipse IDE Python C++ Debian Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's important not to forget that JSPs, like Servlets, are just Java classes, and have access to all the functionality of the Java object model. For best performance and best maintainability, you should make your JSPs as "slim" as possible. Make sure all code which is used more than once is shared rather than copied or included.
Including code with <@include> includes at compile time, and compiles an extra copy of the code for each JSP that uses it. This makes the delivered classes bigger and slows down compilation when you change the JSP code. Including something with <jsp:include> includes it at run time, every time the JSP is executed. Accessing code with "import" is the quickest both to compile (existing un-changed classes don't need to be re-compiled) and to execute.
I recommend writing your utility methods in one or more Java classes. If your methods do not require any initialization before use, make them static methods, and just call them. If they need initialization before use, you could make the class a bean, or use the Singleton pattern to create self-initializing static methods.
An example of a JSP using an exteranl static method is as follows:
file: Utils.java

file: ex.jsp

To try this out, compile Utils.java and put the Utils.class in your WEB-INF/classes directory, then put ex.jsp in your document root. You can then call the JSP using whatever/ex.jsp to use the default string ("unknown") or as whatever/ex.jsp?hello to translate "hello".
I hope this helps.
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Frank,
Thanks a lot for such a great help. Your explanation is excellent. This has solved my problem. Now I am taking all my functions frm JSPs to Beans.
Again thanks a lot
Shikhar
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic