request.getParameter does not work in dopost() but it works in doget()

933 views Asked by At

i want to get parameters from url, request.getParameter is working fine in doget but it doesn't work in dopost.

in dopost it returns empty string.

i couldn't find why this is happening, PLEASE HELP me find the problem.

here is form tag in my jsp file:

<form id="frmMerchant" method="post" action="MerchantController"
    onsubmit="return validate(this)" name="entry">

and here is my servlet:

package com.tms.controller;

import java.io.IOException;


import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tms.dao.MerchantDao;
import com.tms.model.Merchant;

import java.sql.SQLException;
import java.util.logging.Logger;

public class MerchantController extends HttpServlet {
    Logger logger = Logger.getLogger("Logging");
    private static final long serialVersionUID = 1L;
    private static String INSERT_OR_EDIT = "/Merchant.jsp";
    private MerchantDao dao;

    public MerchantController() {
        super();
        dao = new MerchantDao();
    }

    protected void doGet(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {
        try {
            String forward = "";
            String action = request.getParameter("action");

            if (action.equalsIgnoreCase("delete")) {
                int merchantId = Integer.parseInt(request.getParameter("id"));
                dao.deleteMerchant(merchantId);
                forward = INSERT_OR_EDIT;
                request.setAttribute("merchants", dao.getAllMerchants());
                HttpSession session = request.getSession();
                session.setAttribute("message", "Succesfully delete");
            } else if (action.equalsIgnoreCase("edit")) {
                forward = INSERT_OR_EDIT;
                String merchantId = request.getParameter("id");
                Merchant merchant = dao.getMerchantById(merchantId);
                request.setAttribute("merchant", merchant);
            } else if (action.equalsIgnoreCase("listMerchant")) {
                forward = INSERT_OR_EDIT;
                request.setAttribute("merchant", dao.getAllMerchants());
            } else {
                forward = INSERT_OR_EDIT;
            }

            RequestDispatcher view = request.getRequestDispatcher(forward);
            view.forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info("Exception has occured " + ex);
        }
    }

    protected void doPost(HttpServletRequest request,
            HttpServletResponse response) throws ServletException, IOException {

        try {
            HttpSession session = request.getSession();
            Merchant merchant = new Merchant();
            String merchantId = request.getParameter("id");
            merchantId = request.getQueryString();


            merchant.setCityID(Integer.parseInt(request.getParameter("ddlCity")));
            merchant.setAddress(request.getParameter("txtAddress"));
            merchant.setContract(request.getParameter("txtContract"));
            merchant.setMobile(request.getParameter("txtMobNum"));
            merchant.setName(request.getParameter("txtName"));

            if (merchantId == null || merchantId.isEmpty()) {
                dao.addMerchant(merchant);
                session.setAttribute("message", "Inserted successfully");
            } else {
                merchant.setID(merchantId);
                dao.updateUser(merchant);
                session.setAttribute("message", "Edited successfully");
            }
            RequestDispatcher view = request.getRequestDispatcher(INSERT_OR_EDIT);
            request.setAttribute("merchants", dao.getAllMerchants());
            view.forward(request, response);
        } catch (Exception ex) {
            ex.printStackTrace();
            logger.info("Exception has occured " + ex);
        }
    }
}
0

There are 0 answers