import java.io.*; import java.util.*; import java.sql.*; import javax.servlet.*; import javax.servlet.http.*; import au.com.forward.webstringtemplate.*; /** * An HTTP Servlet that outputs the data definition. */ public class Bookstore4Servlet extends HttpServlet { public void init() throws ServletException { try { Class.forName("org.gjt.mm.mysql.Driver").newInstance(); } catch (Throwable t) { throw new ServletException("Error getting org.gjt.mm.mysql.Driver",t); } } public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { ServletContext application = getServletContext(); // load the template 'bookstore.st' and the data 'bookstore.data' from the 'st' sub-directory of this application. // cache them both in a group called bookstoreGroup. WebStringTemplate wst = WebStringTemplateServer.getInstanceOf(application, "bookstoreGroup", application.getRealPath("/") + "st", "bookstore"); String url = "jdbc:mysql://localhost:3306/BookDB?autoReconnect=true"; Object book = null; String bookId = ""+wst.get("FeaturedBookId"); // get the id of the current featured book PreparedStatement prepStmt=null; try { Connection con = DriverManager.getConnection(url, "bookstore","webstringtemplates"); // i.e. "myLogin", "myPassword"); String selectStatement = "select * " + "from books where id = ? "; prepStmt = con.prepareStatement(selectStatement); prepStmt.setString(1, bookId); ResultSet rs = prepStmt.executeQuery(); if (rs.next()) { book = new Book(rs.getString("id"), rs.getString("surname"), rs.getString("first_name"), rs.getString("title"), rs.getFloat("price"), rs.getInt("onSale"), rs.getInt("year"), rs.getString("description"), rs.getInt("inventory") ); } else { book = "Couldn't find book: " + bookId; } } catch (Throwable t) { book = "Couldn't get book details for "+bookId+" "+t.getMessage(); } finally { if (prepStmt!=null) { try { prepStmt.close(); } catch (Throwable t) { // write a message to the log file System.err.println(t.getMessage()); } } } wst.put("book",book); // add the book to the data map, replace any default data // update the urls with session id or cookies wst.put("bookcatalogUrl",response.encodeURL(""+wst.get("bookcatalogUrl"))); wst.put("bookdetailsUrl",response.encodeURL(""+wst.get("bookdetailsUrl"))); // set content-type header before accessing the Writer response.setContentType("text/html"); // set the buffersize from the default data for this template response.setBufferSize(wst.getOutputBufferSize()); PrintWriter out = response.getWriter(); wst.setAttribute("servletRequest",request); //wst.setAttribute(WebStringTemplate.OUTPUT_DATA_DEFINITION_FLAG,"true"); //wst.setAttribute(WebStringTemplate.OUTPUT_SHOW_ALL_FLAG ,"true"); //wst.setAttribute(WebStringTemplate.OUTPUT_SHOW_FILTERS_FLAG,"true"); // then write the data to the response wst.write(out); out.close(); } public void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, java.io.IOException { doGet(req, resp); } public String getServletInfo() { return "A simple WebStringTemplate example that just returns the default data."; } }