简体   繁体   中英

How to call servlet from a jsp

I have a question, how to call a servlet from a jsp(chart.jsp) without using <jsp:include page="/servletURL" /> because I tried before and I don't know if this is the right reason but it crashes when I use this code above. I put in my doGet() method to retrieve information from DB and populate my dropdownlist (in chart.jsp) using JSTL+option and then redirect to my page (the same page), what I believe is that everytime the browser writes a new page using c:forEach tag it calls again my servlet and there is a never ending loop (Again, that's just my presumptions) Here is my code to make it more clear:

my servlet:

ArrayList<Machine> foundMachines = MachineDB.getAllMachines();
request.getSession().setAttribute("foundMachineList", foundMachines);
RequestDispatcher rd = request.getRequestDispatcher("charts/chart.jsp");
rd.forward(request, response);

my jsp:

<jsp:include page="/searchServlet" />
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<c:forEach var="machine" items="${sessionScope.foundMachineList}">
 <option value="${machine.machineId}">${Machine.machineName}
 </option>
</c:forEach>

So my question is why my <jsp:include page="/servletURL" /> tag crashes my page and how to fix it. Any sugestion is welcome

采用

 response.sendRedirect("//your servlet name");

可能不相关但变化

    ${Machine.machineName} to ${machine.machineName}     

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM