Question
The following example uses a Model View Controller (MVC) pattern to implement a small fortune cookie application. The application provides a random fortune cookie message
The following example uses a Model View Controller (MVC) pattern to implement a small fortune cookie application. The application provides a random fortune cookie message to the user, selected from a list of fortunes that can be updated.
You need only to fill in the functionality for the boxes indicated, and can presume that all libraries, imports, java beans and so on are already appropriately defined and available.
The application works by GET or POST parameter submission to an application controller, and it has the following functionality:
There are 2 kinds of java beans for data, with the following data element used in the application:
User
name
Fortune
message
The application is driven by a servlet controller as follows: Fortune Controller:
If the action parameter is provided, it can have two possible values with the following outcomes:
Value of clearSession clears all user and fortune data stored in the current session.
Value of showAllFortunes dispatches to a JSP view that lists all the currently available fortunes
If the setUser parameter is provided, the value is taken as the user name, so a User bean is created with that name and stored in the session.
If the addFortune parameter is provided, the value is taken as a new fortune message, so a new Fortune bean is created and stored in the currently available set of fortunes in the session.
For all cases (including no parameters) other than showAllFortunes, dispatches to a JSP view that shows a fortune cookie message.
The application has two views, as follows:
Open Fortune Cookie View (openFortuneCookie.jsp):
If a user bean is present in the session, displays the user name in greeting. If not, displays a default user greeting.
If a fortune has been sent in the request, displays the fortune message. If not, displays a default fortune message.
Show All Fortunes View (showAllFortunes.jsp):
Displays every fortune in the current fortune list on a separate line.
Servlet Component:
FortuneController.java:
@WebServlet(urlPatterns = {"/FortuneController"})
public class FortuneController extends HttpServlet {
protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//------------
// Get submitted parameters: action, setUser, addFortune
//------------
String action = _________;
String userName = ________;
String newFortune = _________;
//------------
// Create an object to store the JSP view address // default view is the openFortuneCookie.jsp String viewURL = "openFortuneCookie.jsp";
//------------
// Validate action parameter has a value. For value of "clearSession",
// clear the session data.
//------------
if (action != null && action.equals("clearSession"))
{
__________;
}
//-------------
// Validate action parameter has a value. For value of "showAllFortunes",
// dispatch to showAllFortunes JSP view. (Hint: set JSP view to showAllFortunes)
//-------------
else if (action != null && action.equals("showAllFortunes"))
{
________
}
//-------------
// Validate setUser parameter has a non-empty value. For non-empty value:
// - create a new user bean
// - set the name value in the bean
// - store the bean in the session as "theUser"
//-------------
if (userName != null && !userName.trim().equals(""))
{
__________;
_________;
_________;
}
//-------------
// The list of possible fortunes is stored in the session under the key of "fortunes".
// - get the possible fortunes list from the session
ArrayList
// - if no list exists yet, create one and add it to the session under the appropriate key.
if (fortunes == null)
{
_________;
_________;
}
//-------------
// Validate newFortune parameter has a non-empty value. For a non-empty value:
// - create a new fortune bean
// - set the fortune value in the bean to the new fortune
// - add the bean to the list of possible fortunes
if (newFortune != null && !newFortune.trim().equals(""))
{
__________;
_________;
________;
}
//-------------
// Check the size of the fortunes list. If it is not empty:
// - randomly select one of the fortunes in the list
// - add the selected fortune to the REQUEST as "theFortune"
if (fortunes.size()>0)
{
Fortune fortuneMessage = (Fortune) fortunes.get(new Random().nextInt(fortunes.size()));
___________;
}
//-------------
// If we have made it this far, everything should be set to display a fortune for the user.
// - Dispatch to the openFortuneCookie JSP view. (Hint: // Set JSP view to the proper address
_________;
}
}
// End of servlet code
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started