How to use Session Beans, Helper Classes and Value Objects

Servlets would have logic that only deals with web-related stuff.
Like.. getting or setting values from request/ session, redirecting to the next JSP etc..
Session beans (and not Servlets) ideally would contain all the “business” logic.
All the heavy programming that interacts with Entity beans should be inside the session beans.
Helper classes would have logic that will be used by different parts of the application.
Usually this is menial tasks like formatting a Date in MM/dd/yy format etc..
Another example is get SQL connection code.
These kinds of tasks need t be done all over the place and is not specific to any single part of the application.
When you port the logic from Servlets to session beans, Session beans will not be dealing with request object or any other Servlet API.
Instead the Servlet will be responsible for extracting values from the request object.
These values will be passed as parameters into the method of the session bean.
The session beans will use the values passed in, and do complicated logic or interaction with entity beans, and return the result to the Servlet.
For example this is how the code would look like:
Let’s say we have one “value object” class that we will use to hold values for us.
Let’s call it “Interest” class.

public class InterestVO {
	private double principal;
	private int getNumberOfDays;
	// getters and setters for the above follow
}

Now a Servlet gets values from the request object

public class InterestServlet extends HttpServlet {
	public void service(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		double principal = request.getParameter("principal");
		int numberOfDays = request.getParameter("numberOfDays");
		// now set these into the value object
		InterestVO interestVO = new InterestVO();
		InterestVO.setPrincipal(principal);
		InterestVO.setNumberOfDays(numberOfDays);
		// use helper class to look up InterestSessionBean
		// ...
		// get a session bean and invoke its method –
		// the passed in param interestVO has all the
		// values needed
		double interestValue = interestSessionBean.calcInterest(InterestVO);
		// set the retrieved values into request for display of the next page.
		request.setAttribute("interestCalculated", new Double(interestValue));
		// forward to next page
		getServletConfig().getServletContext()
				.getRequestDispatcher("/page2.jsp").forward(request, response);
	}
}

The session bean will have all the logic for calculating interest, interacting with entity beans, etc..

public class InterestSessionBean implements SessionBean {
	// get values from interestVO
	double principal = interestVO.getPrincipal();
	int numberOfDays = interestVO.getNumberOfDays();
	double interestCalculated = 0;
	// interact with entity beans to lookup rate of interest
	// use some formalae to calculate interest
	// return interestCalculated;
}

About cuppajavamattiz
Matty Jacob - Avid technical blogger with interests in J2EE, Web Application Servers, Web frameworks, Open source libraries, Relational Databases, Web Services, Source control repositories, ETL, IDE Tools and related technologies.

Comments are closed.

%d bloggers like this: