为什么我们不应该在dopost中使用请求调度程序?

时间:2016-12-04 12:32:14

标签: jsp servlets requestdispatcher

我正在使用servlet jsp代码。我从jsp添加了一条记录到数据库。为了显示插入的记录,我使用了requestdispacther。但记录不断添加(如无限循环)。 然后我用sendredirect替换代码并且工作正常。 这段代码的内容:

response.sendRedirect("AddNewProductOnRent?action=currentOnRentlist"); 

我正在使用:

RequestDispatcher rs = request.getrequestDispatcher("AddNewProductOnRent?action=currentOnRentlist");
forward(request,response)

......但得到了错误

我的错误已经解决,但我想知道原因

我的servlet代码:

package com.twd.rent.servlet;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.twd.rent.bean.RentBean;
import com.twd.rent.controller.Controller;



/**
    *  Servlet implementation class AddNewProductOnRent
    */
    @WebServlet("/AddNewProductOnRent")
    public class AddNewProductOnRent extends HttpServlet {
    private static final long serialVersionUID = 1L;

/**
 * @see HttpServlet#HttpServlet()
 */
public AddNewProductOnRent() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse   response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse  response) throws ServletException, IOException {
    // TODO Auto-generated method stub

    String action = request.getParameter("action");

    if(action.equalsIgnoreCase("addnewproductonrent")){

        RequestDispatcher rs =   request.getRequestDispatcher("addnewproductonrent.jsp");
        rs.forward(request, response);

    }else if (action.equalsIgnoreCase("currentOnRentlist")) {
        System.out.println("in current on rent list");
        List<RentBean>list=new ArrayList<RentBean>(); 
        list = new Controller().getListOfCurrentOnRent();

        System.out.println(list);
        request.setAttribute("plist",list);
        String forword = "rentlist.jsp";    

        RequestDispatcher rs = request.getRequestDispatcher(forword);
        rs.forward(request, response);
        }
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse  response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub



    String trid = request.getParameter("trid");
    String customertname = request.getParameter("customertname");
    String contact = request.getParameter("contact");
    String date = request.getParameter("date");
    String address = request.getParameter("address");

    RentBean rb = new RentBean();
    rb.setCustomer_name(new String(customertname.getBytes("ISO-8859-1"),"UTF-8"));
    rb.setContact(contact);
    rb.setDateofissue(date);
    rb.setAddress(new String(address.getBytes("ISO-8859-1"),"UTF-8"));
    System.out.println(rb.getCustomer_name());

    if(trid.isEmpty()){
        System.out.println("In add");

        Controller c = new Controller();
        c.addproductonrent(rb);
        response.sendRedirect("AddNewProductOnRent?action=currentOnRentlist");
    }
    else{
        System.out.println("in update");
    }
}

}

1 个答案:

答案 0 :(得分:0)

您转发到同一个servlet,请求仍然使用相同的方法(POST),因此它再次执行相同的doPost()方法。

您可以转发到视图,但即使这样,重定向也是更好的选择:

  • 浏览器显示详细信息URL而不是用于POST的URL,因此可以添加书签,刷新或发送此URL
  • 用户无需重新发布即可刷新

请参阅https://en.wikipedia.org/wiki/Post/Redirect/Get