Answer the question
In order to leave comments, you need to log in
Calling jsp from Servlet/ JAVA?
Please tell me how to call a JSP file from a servlet and pass a collection of String type to it.
Answer the question
In order to leave comments, you need to log in
It's done like this
Servlet
@WebServlet(urlPatterns = "/") // javax.servlet-api 3.0
public class HomeServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
List<String> users = Arrays.asList("Vaya", "Petya", "Fedya");
req.setAttribute("users", users); // с помощью атрибутов передаются данные между сервлетами
req.getRequestDispatcher("/home.jsp").forward(req,resp);
}
}
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<html>
<body>
<h2>Hello World!</h2>
<c:forEach items="${users}" var="usr">
<p>${usr}</p>
</c:forEach>
</body>
</html>
Thank you! And what will be the script in jQuery (Javascript) that accepts this collection?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question