-1

I was working with servlet jsp code. I added one record from jsp to database. To show the inserted record I used requestdispacther. But records keep on adding( like infinite loop). then I replace code with sendredirect and worked fine. insted of this code:

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

I was using:

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

......but got error

My error solved but I want to know the reason

My servlet code:

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");
    }
}

}

PacMan
  • 1,358
  • 2
  • 12
  • 25

1 Answers1

0

You're forwarding to the same servlet, and the request still has the same method (POST), so it executes the same doPost() method again.

You could forward to the view instead, but even then, redirecting is a better choice:

  • the browser displays the detail URL rather than the URL used to POST, and can thus bookmark, refresh, or send this URL
  • the user can refresh without reposting

see https://en.wikipedia.org/wiki/Post/Redirect/Get

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255