Reputation: 529
I know putting java code in jsp pages in an old school way of doing things. Can anyone please point me to an exhaustive tutorial online that teaches the best way to separate your java code from jsp code?
I am building a web application that is going to make use of several java classes.
Thanks
Upvotes: 0
Views: 1240
Reputation: 11226
You can call java code from JSP pages in the following ways.
Place all Java code in JSP page. Appropriate only for very small amounts of code.
Develop separate utility classes. Insert into JSP page only the Java code needed to invoke the utility classes.
Develop separate utility classes structured as beans. Use jsp:useBean, jsp:getProperty, and jsp:setProperty to invoke the code.
Have a servlet respond to original request, look up data, and store results in beans. Forward to a JSP page to present results. You could also use frameworks like Struts.
Use shorthand syntax to access and output object properties. Usually used in conjunction with beans and MVC.
Develop tag handler classes. Invoke the tag handlers with XML-like custom tags.
Now to learn all this there are lots of materials out there. Don't try to learn it like a subject but rather search and try solutions for your real or practice problems.
Stackoverflow itself has some interesting posts on these subjects.
How to avoid Java code in JSP files?
Hidden features of JSP/Servlet
Upvotes: 0
Reputation: 1031
Separation of code from display (in simple terms, that is,) is the focus of the Model-View-Controller (MVC) architecture. Struts is one such framework. You could do a search for MVC approach using Servlets and JSP. It will give quite an amount of resource to start with.
Upvotes: 2