File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Servlets and the fly likes Functions in JSP Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Functions in JSP" Watch "Functions in JSP" New topic

Functions in JSP

Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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)
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
Paul Wetzel
Ranch Hand

Joined: Nov 02, 1999
Posts: 107
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
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
Frank Carver

Joined: Jan 07, 1999
Posts: 6920
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: ex.jsp

To try this out, compile 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.

Read about me at ~ Raspberry Alpha Omega ~ Frank's Punchbarrel Blog
Ranch Hand

Joined: Nov 22, 2008
Posts: 18944
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
I agree. Here's the link:
subject: Functions in JSP
It's not a secret anymore!