Fancy Templates

So far our servlet has only sent plain text to the browser. Rather than writing HTML inline or using Java Server Pages—the common alternative to servlets in Java—we’ll introduce a simple templating library called FreeMarker.[13] Download the GAE-compatible binary FreeMarker .jar, and drag it into your project’s war/WEB-INF/lib directory. If the JAR doesn’t show up as a Referenced Libraries, click the project’s properties, select Java Build Path, and under Libraries choose Add JARs then drill down war/WEB-INF/lib, choosing the JAR.

We need to add a method to the LunchRouletteServlet that renders a template file with some given data, and write the result to the response stream. This function assumes the FreeMarker template generator can find template files under WEB-INF/views.

chapter-2/src/test/book/glass/LunchRoulette.java
 
public​ ​static​ ​String​ render(ServletContext ctx, ​String​ template,
 
Map​<​String​, ​Object​> data)
 
throws​ IOException, ServletException
 
{
 
Configuration​ config = ​new​ ​Configuration​();
 
config.setServletContextForTemplateLoading(ctx, ​"WEB-INF/views"​);
 
config.setDefaultEncoding(​"UTF-8"​);
 
Template ftl = config.getTemplate(template);
 
try​ {
 
// use the data to render the template to the servlet output
 
StringWriter​ writer = ​new​ ​StringWriter​();
 
ftl.process(data, writer);
 
return​ writer.toString();
 
}
 
catch​ (TemplateException e) {
 
throw​ ​new​ ServletException(​"Problem while processing template"​, e);
 
}
 
}

Then change the servlet’s doGet method to populate a random food and render the template, rather than simply printing out the string. We also need to set the content type as HTML.

chapter-2/src/test/book/glass/LunchRouletteServlet.java
 
public​ ​class​ LunchRouletteServlet ​extends​ HttpServlet
 
{
 
/** Accepts an HTTP GET request, and writes a random lunch type. */
 
public​ ​void​ doGet(HttpServletRequest req, HttpServletResponse resp)
 
throws​ IOException, ServletException
 
{
 
resp.setContentType(​"text/html; charset=utf-8"​);
 
 
Map​<​String​, ​Object​> data = ​new​ ​HashMap​<​String​, ​Object​>();
 
data.put(​"food"​, LunchRoulette.getRandomCuisine());
 
 
String​ html = LunchRoulette.render(
 
getServletContext(), ​"web/cuisine.ftl"​, data);
 
resp.getWriter().append(html);
 
}
 
}

Finally, we need to create the template file under war/WEB-INF/views/web/cuisine.ftl. This is just regular HTML with an interpolated variable food inside of the ${ ... } directive.

chapter-2/war/WEB-INF/views/web/cuisine.ftl
 
<!doctype html>
 
<html>
 
<head>
 
<title>​Lunch Roulette​</title>
 
<style>
 
h2{ color:#db1; }
 
body{ background-color:black; color:white; }
 
</style>
 
</head>
 
<body>
 
<article>
 
<h2>​Your Lunch​</h2>
 
<strong>​${ food }​</strong>
 
</article>
 
</body>
 
</html>

With our code in place, now is a good time to test it. You don’t have to deploy the application to test these changes. Click the run icon in ADT (it looks like a green triangular play icon), or choose Run As -> Web Application. This runs a small GAE environment locally—basically, a Java web-app server. Then visit http://localhost:8888/. This is useful in later chapters, since you can test your Glassware without deploying your application to GAE.

If all works according to plan, you should be redirected to the Google authorization page, requesting access to view account information, your Glass timeline, and your Glass location.

This may also be a good time to choose Deploy to App Engine.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.142.235.144