Why one more layer? – for the Business thing

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 of the “business” logic.
The entire 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/yyyy format etc…
Another example is the Home lookup code, or the get sql Connection code.
These kinds of tasks need to 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 the 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 bean methods 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 numberOfDays;
	// getters and setters for above

Now a servlet gets values from the request object (typed into the preceding page).

public class InterestServlet extends HttpServlet {
public service(request, response) {
	double principal = request.getParameter("principal");
	int numberOfDays = request.getParameter("numberOfDays");

	// now set these into the value object
	InterestVO interestVO = new InterestVO();

	//use helper class to lookup InterestSessionBean

	// get a session bean and invoke its method -- the passed in param interestVO has values that are needed
	double interestValue = interestSessionBean.calcInterest(interestVO);

	// set the retrieved value into request for display in the next page
	request.setAttribute("interestCalculated", new Double(interestValue));

	// forward to next page

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

public class InterestSessionBean {

public double calcInterest(InterestVO interestVO) {

	// get Values from interestVO
	double principal = interestVO.getPrincipal();
	int numberOfDays = interestVO.getNumberOfDays();

	double interestCalculated = 0;

	// interact with entity beans to lookup the rate of interest.
	// use some formulas 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: