f. Write displayRooms.jsp in WebContent directory.
<!DOCTYPE html>
<html>
<head>
<title>Available Rooms</title>
</head>
<%@ page import="java.util.List"%>
<%@ page import="java.util.ArrayList"%>
<%@ page import="com.servlet.tutorial.Room"%>
<body>
<H4>Below Rooms are available for the selected Residence.</H4>
<%
Object obj = request.getAttribute("availableRooms");
List<Room> rooms = (List<Room>)obj;
%>
<table border="1">
<thead>
<tr>
<th>Room Number</th>
<th>Residence Code</th>
<th>Floor Number</th>
<th>Room Rent</th>
</tr>
</thead>
<%
for(int i=0;i<rooms.size();i++)
{
Room room = rooms.get(i);
%>
<tr>
<td><%= room.getRoomNumber()%></td>
<td><%= room.getResidenceCode()%></td>
<td><%= room.getFloorNumber()%></td>
<td><%= room.getRoomRent()%></td>
</tr>
<%
}
%>
</table>
</body>
</html>
g. Write noRooms.jsp in WebContent directory.
<!DOCTYPE html> <html> <head> <title>No Rooms</title> </head> <body> <h4>Sorry There are no rooms available for the selected Residence!!</h4> </body> </html>
Testing
Hit http://localhost:8080/HelloWorld/SelectHostel.jsp and select any Residency other than Residence 4


Hit http://localhost:8080/HelloWorld/SelectHostel.jsp and select Residence 4


16.3 Alternate Approach.
Another approach to implement MVC is –
- Create a servlet which will just forward request to some Controller say CommandController
- All JSP will send a hidden parameter in a request
- Controller will scan the hidden parameter and based on its value, invoke business classes. Based on result, returns the display name
- Servlet will read the result and forward the request to the returned display name.
Pseudo Code
- JSP will send hidden parameter say “Request_Type” and will post to say ActionServlet
<input type=”hidden” name=”Request_Type” value=”Login”/>
- ActionServlet will have following code in doPost()
String view = CommandController.processRequest(request,response); RequestDispatcher rd = request.getRequestDispatcher(view); rd.forward(request,response);
- CommandController class will have one static method processRequest() like below
public static String processRequest(HttpServletRequest request, HttpServletResponse response )
{
String requestType = request.getParameter(“Request_Type”);
if(requestType.equals(“Login”)
{
// invoke corresponding business
return view;
}
else if(requestType.equals(“Logout”)
{
// invoke corresponding business
return view;
}
}
Advantage of this approach is that your web application will have one and only Controller which is CommandController instead of all Servlets acting as a controller.
