
Add Business Method :
Before we start implementing our servlet, add one more business method to StoreAccess Bean called getAllItems() with the following signature:
public java.util.ArrayList getAllItems()
This method will return all the items in MyStore by invoking the finder method in ItemsLocalHome named findAll, as shown below (code snippet from StoreAccess bean):

Now let's start implementing the generated methods in the servlet.
Implement init method :
This method is responsible for initializing servlets, so it is invoked when the servlet is first created and is not called again for each user request.
We will cache the references for our StoreAccess Bean in this method, as all the client interfaces available are exposed in StoreAccess.
So, first create a context. Then get a reference to a StoreAccess object by looking up the StoreAccess bean via the JNDI API.
Add a variable storeAccessHome of type StoreAccessHome to store the reference obtained by narrowing the object.
Create helper methods for getting Context, Home and assigning the reference to storeAccessHome as shown below.

Note : We have to narrow this object because we are accessing a remote interface. If it was a local interface, we wouldn't need to narrow the object.
Implement methods doGet and doPost :
In order to implement these two methods we will create a helper method to provide the functionality for both of them. The request is delegated to this method where all processing of information takes place. Once this business logic processing is complete it dispatches the request to the appropriate view for display, ie. JSP pages.
Note : This approach is based on the Front Controller pattern, where the controller acts as a central point of contact for handling requests, delegating business processing, and coordinating with dispatcher components, whilst dispatchers are responsible for view management and navigation. This pattern suggests centralizing the handling of all requests in this way, but also allows for different handler methods to be used in processing different request types.
Add a method named procesRequest with the following signature:
protected void processRequest (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
Delegate request from doGet and doPost method to this method as shown below.

Now implement the processRequest() method.
This method is structured such that it will check for the parameter useraction within the request object. If the useraction parameter is empty then it will build a url for the login screen and generate a login screen page. If useraction has some value then it examines it. If it finds the request is to display items then it builds the url for that and generates a page displaying all the items. Finally, for errors a page is generated to display the error.
Add following attributes shown below, for building different urls.

Create a session object and get value from useraction parameter of request object as shown below in code snippet.






