c. Define a class GetAvailableRoomsByResidence will be a business logic class which will have all the business logic to pull the available rooms. As I mentioned earlier, ideally this data should come from database but here in this example we will return some dummy data.
package com.servlet.tutorial;
import java.util.ArrayList;
import java.util.List;
public class GetAvailableRoomsByResidence {
public static List<Room> getAvailableRoomsByResidence(String residenceCode)
{
List<Room> availableRooms = new ArrayList<Room>();
if(residenceCode.equals("Residence 1"))
{
Room room = new Room();
room.setFloorNumber( "2" );
room.setResidenceCode( residenceCode );
room.setRoomNumber( "R001" );
room.setRoomRent(200);
availableRooms.add(room);
room = new Room();
room.setFloorNumber( "1" );
room.setResidenceCode( residenceCode );
room.setRoomNumber( "R002" );
room.setRoomRent(230);
availableRooms.add(room);
room = new Room();
room.setFloorNumber( "4" );
room.setResidenceCode( residenceCode );
room.setRoomNumber( "R003" );
room.setRoomRent(400);
availableRooms.add(room);
}
else if(residenceCode.equals("Residence 2"))
{
Room room = new Room();
room.setFloorNumber( "1" );
room.setResidenceCode( residenceCode );
room.setRoomNumber( "R011" );
room.setRoomRent(240);
availableRooms.add(room);
room = new Room();
room.setFloorNumber( "11" );
room.setResidenceCode( residenceCode );
room.setRoomNumber( "R022" );
room.setRoomRent(330);
availableRooms.add(room);
room = new Room();
room.setFloorNumber( "4" );
room.setResidenceCode( residenceCode );
room.setRoomNumber( "R013" );
room.setRoomRent(300);
availableRooms.add(room);
}
else if(residenceCode.equals("Residence 3"))
{
Room room = new Room();
room.setFloorNumber( "1" );
room.setResidenceCode( residenceCode );
room.setRoomNumber( "R111" );
room.setRoomRent(440);
availableRooms.add(room);
room = new Room();
room.setFloorNumber( "21" );
room.setResidenceCode( residenceCode );
room.setRoomNumber( "R222" );
room.setRoomRent(340);
availableRooms.add(room);
room = new Room();
room.setFloorNumber( "4" );
room.setResidenceCode( residenceCode );
room.setRoomNumber( "R213" );
room.setRoomRent(300);
availableRooms.add(room);
room = new Room();
room.setFloorNumber( "5" );
room.setResidenceCode( residenceCode );
room.setRoomNumber( "R513" );
room.setRoomRent(360);
availableRooms.add(room);
}
else if(residenceCode.equals("Residence 4"))
{
// No Rooms available
}
return availableRooms;
}
}
d. Define Entry of “GetRoomsByResidenceServlet” in web.xml
<servlet> <servlet-name>GetRoomsByResidenceServlet</servlet-name> <servlet-class>com.servlet.tutorial.GetRoomsByResidenceServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>GetRoomsByResidenceServlet</servlet-name> <url-pattern>/GetRoomsByResidenceServlet</url-pattern> </servlet-mapping>
e. Write Servlet “GetRoomsByResidenceServlet” Code. Calling of Business classes and delegate to different presentation based on business logic results are highlighted in below code
package com.servlet.tutorial;
import java.io.IOException;
import java.util.List;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class GetRoomsByResidenceServlet extends HttpServlet {
@Override
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException
{
String residenceCode= request.getParameter("Residence");
// invoke Business logic
List<Room> availableRooms = GetAvailableRoomsByResidence.getAvailableRoomsByResidence(residenceCode);
// store the result in requst
request.setAttribute("availableRooms", availableRooms);
if(availableRooms.size()>0)
{
RequestDispatcher rd = request.getRequestDispatcher("displayRooms.jsp");
rd.forward(request, response);
}
else
{
RequestDispatcher rd = request.getRequestDispatcher("noRooms.jsp");
rd.forward(request, response);
}
}
}
