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
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 ]
Joined: Nov 27, 2001
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
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
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).
subject: Is it Ok to let non-servlet classes handle HttpServletRequest,/Response tasks ?