aspose file tools*
The moose likes Servlets and the fly likes Is it Ok to let non-servlet classes handle HttpServletRequest,/Response tasks ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Is it Ok to let non-servlet classes handle HttpServletRequest,/Response tasks ?" Watch "Is it Ok to let non-servlet classes handle HttpServletRequest,/Response tasks ?" New topic
Author

Is it Ok to let non-servlet classes handle HttpServletRequest,/Response tasks ?

Ian Yang
Ranch Hand

Joined: Nov 27, 2001
Posts: 44
I have a "MyServlet.java" which is a servlet.
Since for some reason my business logic is complicated and I don't want to make this servlet
code too big, so I have several Task performing type java classes like "Task1.java", "Task2.java", they are NOT servlet. The scenario is:
"MyServlet.java" get some input parameters and then based on input, it calls "Task1.java" or "Task2.java" and pass the HttpServletRequest/response objects and other param to these non-servlet java classes. In these non-servlet classes, they handle business logics and then forward the result to "result.jsp" page by using the request/responses object passed from "MyServlet". In other word, I use some non-servlet classes handle request/response activities plus business logics, is this OK and is it a good way to do it ?
Thanks,
Ian
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

I do this sort of thing all the time, with a small modification.

I'd let the flow of control pass back to the servlet, and use the 'original' request object forward to the next JSP in the chain.



Something else you might consider. If the 'helping class' doesn't need to modify anything in the response or request (not likely, but there could be a case), then don't send it the original request/response objects. Pull out the data you need, and send just that data to your helper class. This way, the helper class can't "accidentaly" tromp on your request or response objects. This keeps the principal of "least privelege" alive and well.
[ February 13, 2002: Message edited by: Mike Curwen ]
Ian Yang
Ranch Hand

Joined: Nov 27, 2001
Posts: 44
Thanks lot, Mike. Now one more question: if you want to do "req.setAttribute("MyObj", o)" kind thing, and forward this "MyObj" to the next jsp page, then should you do this in the HelperClass too ? i.e. if you set the attribute in HelperClass, AFTER the line --
helperClass.dothis(req, res);

has this "req" been associated with the "MyObj" already ?
Thanks,
Ian
Bosun Bello
Ranch Hand

Joined: Nov 06, 2000
Posts: 1510
Assuming I understand you correctly, you can have your JSP's participate in a session, and set the scope appropriately. Once that's done, attributes that you sent in your page should be availabe to the other JSP's.


Bosun (SCJP, SCWCD)
So much trouble in the world -- Bob Marley
Mike Curwen
Ranch Hand

Joined: Feb 20, 2001
Posts: 3695

If you place MyObj into the request, it is available in the request, no matter 'where' you do it. Whether you do it in the servlet, or the helper class depends mostly on what makes the most sense.

Bottom line: When you place something into the request, it is available through request.getAttribute() until the request itself goes out of scope (which it will do, right after the JSP you forward it to finishes processing).
 
wood burning stoves
 
subject: Is it Ok to let non-servlet classes handle HttpServletRequest,/Response tasks ?