Java tutorial
package gan.ism.ws; import gan.ism.util.MssqlDB; import java.io.IOException; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.util.HashMap; import java.util.Map; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import net.sf.json.JSONObject; public class DrmLicense extends HttpServlet { private static final long serialVersionUID = 4343259990835574782L; private static Log mLogger = LogFactory.getFactory().getInstance(DrmLicense.class); private static final String str = "select t.id as order_id from transaction t, cas_transaction cas_t where t.c_user_id =? and cas_t.commodity_id =? and t.id=cas_t.transaction_id "; @Override protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * UserID commodity_id ? ,? * ?? * {"result":0,"msg":??, order_id":1111, "expired_date":? 2012-12-27 12:59:59?,"playback_count":30 } * {"result":-1,"msg":???} */ @Override protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { String user_id = request.getParameter("user_id"); String commodity_id = request.getParameter("commodity_id"); int status = -1; String msg = "?"; Long order_id = null; mLogger.debug("select t.id as order_id from transaction t, cas_transaction cas_t where t.c_user_id =" + user_id + " and cas_t.commodity_id =" + commodity_id + " and t.id=cas_t.transaction_id and t.payment_state=1 "); Connection conn = MssqlDB.createConnection(); ResultSet rs = MssqlDB.getInstance().executeQuery(conn, str, new Object[] { user_id, commodity_id }); try { if (rs.next()) { order_id = rs.getLong("order_id"); status = 0; msg = ""; } rs.close(); conn.close(); } catch (SQLException e) { e.printStackTrace(); } Map<String, Object> map = new HashMap<String, Object>(); map.put("result", status); map.put("msg", msg); if (status == 0) { map.put("order_id", order_id); map.put("expired_date", ""); //? map.put("playback_count", -1); //? } JSONObject jo = JSONObject.fromObject(map); response.setContentType("text/html; charset=UTF-8"); mLogger.debug("C?" + jo.toString()); response.getWriter().write(jo.toString()); } }