FFL2:FFL2 Main/Web Framework/FantacyEntityServlet Subclass
Intro
Sometimes just loading a single entity is not enough, we made need to load some additional data, do some sorting of data, etc. In this case we can subclass FantacyEntityServlet to add some additional behaviour by overriding the following method.
protected void continueProcess(ServletRequest request, Object entity, FantacySeason fantacySeason) throws DataException { // do nothing }
In this case we are passed, as the entity parameter, the object which is automatically loaded by FantacyEntityServlet (based on the URL as before). We are also passed the FantacySeason.
By convention the subclasses of FantacyEntityServlet have been placed in the uk.org.fantacy.servlet.custom package
Example
An example of when this is useful is FantacyDivisionServlet. When displaying a FantacyDivision we wish to load all the league standing for the particular season of interest. We have the FantacyDivision and the FantacySeason so this is quite straightforward. The code for this class is
package uk.org.fantacy.servlet.custom; import ... ... public class FantacyDivisionServlet extends FantacyEntityServlet { @Override protected void continueProcess(ServletRequest request, Object entity, FantacySeason fantacySeason) { // we get the division automatically, just need to cast it FantacyDivision fantacyDivision = (FantacyDivision)entity; // get the league standings FantacyDAO dao = FantacyHibernateDAO.getInstance(); List<FantacyLeagueStanding> fantacyLeagueStandings = dao .getFantacyLeagueStandings(fantacyDivision, fantacySeason); request.setAttribute("fantacyLeagueStandings", fantacyLeagueStandings); } }
All this servlet does is
- Take the FantacyDivision and the FantacySeason
- Use these to look up the FantacyLeagueStandings for that division in that season
- Add the list of FantacyLeagueStanding objects to the request so we can display it on a JSP
The code to do the actual lookup is also quite simple using hibernate
public List<FantacyLeagueStanding> getFantacyLeagueStandings( FantacyDivision fantacyDivision, FantacySeason fantacySeason) { Query qry = this.getSession().createQuery( "from FantacyLeagueStanding fls " + "where fls.fantacyDivision = :fantacyDivision " + "and fls.fantacySeason = :fantacySeason " + "order by fls.position asc"); qry.setEntity("fantacyDivision", fantacyDivision); qry.setEntity("fantacySeason", fantacySeason); List<FantacyLeagueStanding> ret = getQueryList(qry); return ret; }
Config
Having created our custom servlet, we need to configure it to be used. The config is exactly similar to that for FantacyEntityServlet. As this servlet is new we will need to declare it as well as use it in web.xml.
Step 1: Map the class to a logical name
We map the fully qualified class name to a logical name as follows. To avoid confusion it is a good idea to use the class' name as the logical name
<servlet> <servlet-name>FantacyDivisionServlet</servlet-name> <servlet-class>uk.org.fantacy.servlet.custom.FantacyDivisionServlet</servlet-class> </servlet>
Step 2: Map the URL to the servlet
Remember the URL has the format app.<entity type>, this is how FantacyEntityServlet knows what type of entity to load. Remember also that we will also dispatch the request to a JSP based on the URL, in this case fantacyDivision.jsp
<servlet-mapping> <servlet-name>FantacyDivisionServlet</servlet-name> <url-pattern>/app.FantacyDivision</url-pattern> </servlet-mapping>
That's it
Assuming the required JSP has been written there is nothing more required.