2. Java Servlet Controller -> Model
package com.business; import java.io.*; import javax.servlet.*; import javax.servlet.http.*; @SuppressWarnings("serial") public class bomSvlt extends HttpServlet{ @Override public void doGet (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{ String rv = ""; req.setCharacterEncoding("utf-8"); res.setContentType("text/html; charset=utf-8"); PrintWriter out; out = res.getWriter(); String table = req.getParameter("table"); //(1) String mode = req.getParameter("mode"); //(2) bomBean bom = new bomBean(); if(table.equals("item_info")) { if(mode.equals("rev")) { // For Cloud SQL String item_no = req.getParameter("item_no"); rv = bom.revltemInfo(item_no); }else if(mode.equals("rev_kv")) { // For Bigtable String item_no = req.getParameter("item_no"); rv = bom.revltemInfo_kv(item_no); } } : out.println(rv); out.close(); } public void doPost (HttpServletRequest req, HttpServletResponse res) throws ServletException, IOException{ String rv = ""; req.setCharacterEncoding("utf-8"); res.setContentType("text/html; charset=utf-8"); PrintWriter out; out = res.getWriter(); String table = req.getParameter("table"); String mode = req.getParameter("mode"); bomBean bom = new bomBean(); if(table.equals("item_info")){ String item_no = req.getParameter("item_no"); String item_name = req.getParameter("item_name"); String final_flag = req.getParameter("final_flag"); String vendor_no = req.getParameter("vendor_no"); String vendor_name = req.getParameter("vendor_name"); // For Cloud SQL if(mode.equals("add")){ rv = bom.addItemInfo(item_no, item_name, final_flag, vendor_no, vendor_name); }else if(mode.equals("upd")){ rv = bom.updItemInfo(item_no, item_name, final_flag, vendor_no, vendor_name); }else if(mode.equals("del")){ rv = bom.delItemInfo(item_no); } // For Bigtable1(パターン1) else if (mode.equals("add_kv")){ rv = bom.addItemInfo_kv(item_no, item_name, final_flag, vendor_no, vendor_name); }else if (mode.equals("upd_kv")){ rv = bom.updItemInfo_kv(item_no, item_name, final_flag, vendor_no, vendor_name); }else if (mode.equals("del_kv")){ rv = bom.delItemInfo_kv(item_no); } // For Bigtable2(パターン2) else if (mode.equals("add_kv2")){ rv = bom.addItemInfo_kv2(item_no, item_name, final_flag, vendor_no, vendor_name); }else if (mode.equals("upd_kv2")){ rv = bom.updItemInfo_kv2(item_no, item_name, final_flag, vendor_no, vendor_name); } : } out.println(rv); out.close(); } }
jQueryから送信されたリクエストは、リスト2のサーブレット(bomSvlt)では参照系はdoGetで、更新系はdoPostで受け取り、(1)で操作対象のテーブル名(Bigtableではkind名)、(2)では処理モード(登録・参照・更新・削除)を指定し、対応するビーンズメソッドを呼び出します。