Sunday, 27 August 2017

java - How do I add a servlet to replace scriptlets in a JSP?




I have inherited a large legacy web application that is constructed mainly of JSP files linked together with HTML frames. I've been reading Working Effectively with Legacy code and I've found a good small piece to break off in a simple step toward refactoring.



The page is a basic listing page. It queries the database with certain request parameters and displays a list of matching rows along with buttons for applicable actions. Thankfully, most of the business logic on this particular page was already in a POJO, and I've figured out how to replace the presentation logic using JSTL.




From other questions/answers I've read here, the best strategy seems to be to extract the remaining scriptlet into a servlet that does the necessary pre-processing based on the request parameters.



How do I get the servlet to intercept the request and then render the JSP with minimal side-effects on the rest of the application for the time being?



The file I am trying to replace is /welcome/TopFrameList.jsp.


Answer



If i understood the scenario completely,



Steps:





  1. You could send the control to the Servlet instead of the jsp
    directly. i.e. you will have to change the url slightly to point to
    Servlet not to the jsp.


  2. Let the Servlet pre-process the request parameters (and some
    business logic if required)


  3. The Servlet then forwards the request
    to the jsp which finally renders the view.





That should work, as the only change to the existing application is a slight url change.



Caution: Hoping you have tests which test this part of the application !


No comments:

Post a Comment

casting - Why wasn't Tobey Maguire in The Amazing Spider-Man? - Movies & TV

In the Spider-Man franchise, Tobey Maguire is an outstanding performer as a Spider-Man and also reprised his role in the sequels Spider-Man...