I have a page, productListing.jsp
where, you guessed it, I list of products. When a client requests this page, I want the servlet it's mapped to -- ProductsIndexController
-- to pull some data from the DB and pass it back to productListing.jsp
.
My issue is that because (I assume) I'm forwardin the request to the same page the servlet is mapped to, it's causing infinite redirects and this error javax.servlet.ServletException: AS-WEB-CORE-00089
Is there a way around this? As a quick-fix for now I'm just calling the model methods straight from the JSP page, but I'd rather not do that.
productListing.jsp (as I'd like it to be)
<%
ArrayList<Product> products = (ArrayList<Product>)request.getAttribute("products"); %>
<!-- Display data by looping over products -->
ProductIndexController (servlet)
@WebServlet({ "/ProductIndexController", "/productListing.jsp" })
public class ProductIndexController extends HttpServlet {
private static final long serialVersionUID = 1L;
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
*/
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
ArrayList<Product> products = Product.all();
request.setAttribute("products", products);
} catch (Exception e) {
Notice notice = new Notice();
notice.addErrorMessage("Couldn't fetch products: " + e.getMessage());
request.setAttribute("notice", notice);
}
request.getRequestDispatcher("/productListing.jsp").forward(request, response);
}
What you can do is when you redirect, add a request parameter, say redirected
, and set it to redirected
in the url, then in your servlet code, just call request.getParamemter("redirected")
, then if this request param is present, don't call redirect, otherwise, call redirect
if (request.getParameter("redirected") == null)
// pay attention to the ?redirected=redirected in the url
request.getRequestDispatcher("/productListing.jsp?redirected=redirected").forward(request, response)
When that request param is null, you know the request is from your jsp, not the redirected response
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.