1

I hope you can help handle problem with endless loop in servlet. Such problems usually causes wrong servlet-mapping (usualy it's "/*"). But in my case, it has specific value - name of conrete jsp file.

Servlet:

public class TrainsListServlet extends HttpServlet {
private static final long serialVersionUID = 1L;

protected void processRequest(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    EmployeeService employeeService = new EmployeeServiceImpl();
    List<Train> trains = (List<Train>) employeeService.getTrains();
    request.setAttribute("trains", trains);
    RequestDispatcher dispatcher = getServletContext()
            .getRequestDispatcher("/getTrainsList.jsp");
    dispatcher.forward(request, response);

}

protected void doGet(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);
}

protected void doPost(HttpServletRequest request,
        HttpServletResponse response) throws ServletException, IOException {
    processRequest(request, response);

}

}

web.xml:

...
<servlet>
    <servlet-name>TrainsList</servlet-name>
    <display-name>TrainsList</display-name>
    <description></description>
    <servlet-class>ru.tsystems.jsproject.sbb.Servlets.TrainsListServlet</servlet-class>
</servlet>
<servlet-mapping>
    <servlet-name>TrainsList</servlet-name>
    <url-pattern>/getTrainsList.jsp</url-pattern>
</servlet-mapping>

...

getTrainsList.jsp:

...
    <c:forEach var="train" items="${trains}">
        <tr>
            <td><c:out value="${train.getNumber()}" /></td>
            <td><c:out value="${train.getSeatsCount()}" /></td>
            <td><c:out value="${train.getFrequence()}" /></td>
        </tr>
    </c:forEach>
...

it's all causes endless loop in processRequest method. Please help, tell me what I am doing wrong?

iartemov
  • 23
  • 6
  • You mapped /getTrainsList.jsp to your servlet, and the servlet forwards to /getTrainsList.jsp. What do you expect? – JB Nizet Oct 25 '14 at 17:00
  • How I can fix that? I don't have forms in jsp file, so I can't link them throw action attribute. – iartemov Oct 25 '14 at 17:05
  • Remove the mapping. Why did you create it in the first place? – JB Nizet Oct 25 '14 at 17:15
  • I need servlet to be called, when I open jsp page. How I can else associate them without form on jsp page and mapping? – iartemov Oct 25 '14 at 17:23
  • You should not try to "open a JSP page". Map your servlet to any URL, but not to the URL of your JSP. It could be /trains, or /trains.html, or whatever you want. But if you give it the URL of your JSP, the servlet will execute **instead** of the JSP. You should even put the jsp files uner WEB-INF, because you should never access them from the outside, but only through a controller servlet. – JB Nizet Oct 25 '14 at 17:31

1 Answers1

1

Map it with html file

<servlet-name>TrainsList</servlet-name>
<url-pattern>/getTrainsList.html</url-pattern>

And follow http://localhost:8080/.../getTrainsList.html and you will get as response from Servlet getTrainsList.jsp

njjnex
  • 1,490
  • 13
  • 23