request.getParameter在dopost()中不起作用,但它在doget()中起作用

时间:2013-12-07 12:29:46

标签: jsp servlets request getparameter

我想从url获取参数,request.getParameter在doget中工作正常,但它在dopost中不起作用。

在dopost中它返回空字符串。

我无法找到为什么会发生这种情况,请帮助我找到问题。

这是我的jsp文件中的表单标记:

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

这是我的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 个答案:

没有答案