Using the MVC Model with JSP
Sure, you say, that all sounds smart and good. But I'm the only one who will be building and maintaining this application, and I don't have time to learn how to program servlets and EJB components, and use JMS. Not to mention the time to program them. So, now what?
Good point. And the truth is that there is a lot you can do to work toward the ideal of MVC programming. The key is to think modular; look for ways to keep the controller, model and view layers separate; and always plan out your application before you start building it.
Developing wholly in JSP is often the best choice, given the resources at hand. Fortunately, JRun offers a number of tools, such as JST, that can make this effective. That said, it is crucial to avoid the pitfalls associated with Page-centric programming:
You want to develop applications that are modular and will scale as your company grows.
You want to make sure that changes to core functions can be made in one place only and without affecting the layout.
You want to make sure that a new programmer can start programming without getting lost in your code.
You want to make sure that new applications can be built on top of (not around) your existing applications.
You want to know that if you change something in an application, you won't break something else.
To make sure that all of these things happen, it is recommended to utilize some of the aspect of MVC programming to your advantage:
Use controller templates. Allocate certain JSP pages to be the controllers of applications or subsections of applications. For example, you might allocate index.jsp to be the controller; then, based on a request parameter, that page will either include or forward the user to the appropriate page. For example, index.jsp?action=addmovie will include a page to enter a new movie.
Keep logic separate. Use JavaBeans components and custom actions wherever possible to keep the logic out of your JSP templates. As a goal, try to prevent having to use any scriptlets in your JSP pages. Often, this won't be possible or even practical, but as an ideal, this will really keep your JSP easy to read, and ensure that no crucial code is being executed in pages.
Watch for redundancies. If you find that you are always using scriptlets to get cookies, translate data types, format variables, or perform other tasks, write a custom action or bean (if you know how) that will handle those functions.
Use XML and XSLT. This is a bit advanced, but generating XML and applying the appropriate style based on your user's device can really go a long way toward keeping the View layer completely separated. Then, your designers can focus on designing XSL guides, and not have to be working in JSP directly.
Modularize design. Do everything you can to keep design templates separate from the Logic and Data layers. You can do this by using included files, using CSS style sheets, and storing content in a separate repositoryeither a directory or database. This last option can work great; there are a number of WYSIWYG tools that can auto-generate HTML on a form page. Plug one in, and let your designers and content people enter this information into a database instead of editing HTML pages.
Learn more Java. The more you know about Java and object-oriented programming, the more you will be able to utilize all of the powerful tools and constructs that are at your fingertips. Java is a deep language, and the more you understand, the more you will have control over your applications.