This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
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
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
Joined: Nov 22, 2008
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
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
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.