user1192724
user1192724

Reputation: 529

Removing Java code from JSP pages

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

Answers (2)

John Eipe
John Eipe

Reputation: 11226

You can call java code from JSP pages in the following ways.

  • Call Java code directly

Place all Java code in JSP page. Appropriate only for very small amounts of code.

  • Call Java code indirectly

Develop separate utility classes. Insert into JSP page only the Java code needed to invoke the utility classes.

  • Use beans

Develop separate utility classes structured as beans. Use jsp:useBean, jsp:getProperty, and jsp:setProperty to invoke the code.

  • Use the MVC architecture

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 the JSP EL

Use shorthand syntax to access and output object properties. Usually used in conjunction with beans and MVC.

  • Use custom tags

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

vaisakh
vaisakh

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

Related Questions