JSP Servlet Video Tutorial | SCWCD Ebooks | JSP Servlet Examples source codes

JSP Servlet Video Tutorial | SCWCD Ebooks Examples | Free download links and Advance java Tutorials

Monday, April 6, 2009

Use of Custom Tags

Monday, April 6, 2009 0
Custom tags are JSP tags you create that allow you to define new functionality for your JSPs.

While this is a simple example meant to illustrate what custom tags are, in reality, custom tags are useful for a number of reasons:

  • They promote code reuse because they cut down duplication of code on your JSPs.
  • They make your JSPs more maintainable, because you can avoid using scriptlets.
  • Their usage simplifies the work of web designers, because they obviate the need for scriptlets, which are more difficult to write and use.
  • They are a key tool used to promote a separation of concerns. Most, if not all, web application frameworks, including Struts and JavaServer Faces, utilize custom tags for this reason.

Difference between JSP Forward and Redirect

Forwards:
  • Both and RequestDispatcher.forward() are what I refer to as "server-side" redirects.
  • The redirect operation is performed in the server side, and the browser is unaware of the change.
  • If page1.jsp forwards to page2.jsp, the browser's address bar will still show page1.jsp

Redirects:
  • The response.sendRedirect() is what I call a "client-side" redirect.
  • The redirect operation is passed to the browser, which is aware of the change.
  • If page1.jsp redirects to page2.jsp, the browser's address bar be updated to show page2.jsp.

Forwards vs. Redirects:


  • You choose between forwards and redirects depending on how you want the browser to be affected. Forward operations are faster, because all the processing happens server-side. Redirect operations update the browser history, however, which is often desirable.
  • If you forward between page1 and page2, if the user's clicks the "refresh" or "reload" button, both page1 and page2 are executed a second time. On the other hand, if you use a client-side redirect, the browser's address information is updated. If the user clicks refresh, only page2 are re-executed.
  • I use redirects to separate update components from display components. Suppose you have a ProcessPaymentServlet and a ShowConfirmation.jsp that informs the use that their payment has been processed. If you used a forward operation between them, if the user refreshed the page, both the servlet and the JSP would be re-executed, for a second payment.
  • On the other hand, if you used a client-side redirect, only the ShowConfirmation.jsp will be refreshed (which is probably what you want).
  • Forward operations are useful when you don't want the browser's address bar to be updated. Error pages are a good situation for using forward operations. Front Controllers in web frameworks that intercept all requests also use forward operations.