3

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.

Frank Riccobono
  • 1,043
  • 6
  • 13
  • 1
    Just put `<% %>` in `doGet()` which ultimately forwards to JSP and then change URL to invoke servlet instead of JSP. See also http://stackoverflow.com/questions/3177733/how-to-avoid-java-code-in-jsp-files/3180202#3180202 and http://stackoverflow.com/tags/servlets/info – BalusC Aug 09 '13 at 17:14
  • @BalusC, I did read your previous answer. It was very helpful. I guess my question was more around how to invoke the servlet once I created it. So in my case, there isn't a cleaner way than creating a url for the servlet and finding and changing links and frame source attributes where relevant? – Frank Riccobono Aug 09 '13 at 18:02

1 Answers1

2

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 !

iCrus
  • 1,210
  • 16
  • 30
  • This is probably what I'll need to do, but see my comment above. I'm afraid there might be many locations that reference this URL. Right now there a no tests other than a manual test plan that takes forever to run through. I'm fully aware of the importance of creating them though. And that's one of the things I would like to accomplish. – Frank Riccobono Aug 09 '13 at 18:04