game.com.AjaxUpdateGameServlet.java Source code

Java tutorial

Introduction

Here is the source code for game.com.AjaxUpdateGameServlet.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package game.com;

import game.com.entity.AjaxResponseEntity;
//import game.com.entity.TuyenCapEntity;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author chieuvh
 */
public class AjaxUpdateGameServlet extends BaseServlet {

    private static final Logger logger = Logger.getLogger(AjaxUpdateGameServlet.class);

    /**
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code>
     * methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        DataAccess dataAccess = null;
        AjaxResponseEntity responseObject = new AjaxResponseEntity();
        try {
            handle(request, responseObject, dataAccess);
            outContent(responseObject.toJsonString(), response);
        } catch (Exception ex) {
            logger.error(ex.getMessage(), ex);
        } finally {
            if (dataAccess != null) {
                dataAccess.closeConnection();
            }
        }

    }

    private void handle(HttpServletRequest request, AjaxResponseEntity responseObject, DataAccess dataAccess)
            throws Exception {
        GsonBuilder gsonBuilder = new GsonBuilder().setDateFormat(AppConfig.dateFormat);
        Gson gson = gsonBuilder.create();
        String Id = Util.getParameter(request, "Id");
        if (StringUtils.isEmpty(Id)) {
            responseObject.returnMessage = "M s tuyn cp khng hp l";
            return;
        }
        String Ten = Util.getParameter(request, "Ten");
        if (StringUtils.isEmpty(Ten)) {
            responseObject.returnMessage = "Vui lng nhp tn tuyn cp";
            return;
        }
        String X1 = Util.getParameter(request, "X1");
        if (StringUtils.isEmpty(X1)) {
            responseObject.returnMessage = "Vui lng nhp t?a  X1";
            return;
        }
        String Y1 = Util.getParameter(request, "Y1");
        if (StringUtils.isEmpty(Y1)) {
            responseObject.returnMessage = "Vui lng nhp t?a  Y1";
            return;
        }
        String X2 = Util.getParameter(request, "X2");
        if (StringUtils.isEmpty(X2)) {
            responseObject.returnMessage = "Vui lng nhp t?a  X2";
            return;
        }
        String Y2 = Util.getParameter(request, "Y2");
        if (StringUtils.isEmpty(Y2)) {
            responseObject.returnMessage = "Vui lng nhp t?a  Y2";
            return;
        }

        float x1, y1, x2, y2;
        try {
            x1 = Float.parseFloat(X1);
        } catch (Exception ex) {
            responseObject.returnMessage = "T?a  X1 khng hp l";
            return;
        }
        try {
            y1 = Float.parseFloat(Y1);
        } catch (Exception ex) {
            responseObject.returnMessage = "T?a  Y1 khng hp l";
            return;
        }
        try {
            x2 = Float.parseFloat(X2);
        } catch (Exception ex) {
            responseObject.returnMessage = "T?a  X2 khng hp l";
            return;
        }
        try {
            y2 = Float.parseFloat(Y2);
        } catch (Exception ex) {
            responseObject.returnMessage = "T?a  Y2 khng hp l";
            return;
        }
        int id = 0;
        try {
            id = Integer.parseInt(Id);
            if (id < 1) {
                responseObject.returnMessage = "M s tuyn cp khng hp l";
                return;
            }
        } catch (Exception ex) {
            responseObject.returnMessage = "M s tuyn cp khng hp l";
            return;
        }
        //        TuyenCapEntity entity = new TuyenCapEntity();
        //        entity.Id = id;
        //        entity.Ten = Ten;
        //        entity.X1 = x1;
        //        entity.Y1 = y1;
        //        entity.X2 = x2;
        //        entity.Y2 = y2;
        //
        //       
        //        int row = dataAccess.updateTuyenCap(entity);
        //        if (row >= 0) {
        //            responseObject.data = entity.toJsonString();
        //            responseObject.returnCode = 1;
        //            responseObject.returnMessage = "Cp nht tuyn cp thnh cng";
        //        } else {
        //            responseObject.returnMessage = "Li h thng, vui lng th li sau";
        //        }

    }

}