A
A
Arthur Braver2016-01-20 11:34:17
Java
Arthur Braver, 2016-01-20 11:34:17

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

2 answer(s)
E
Evhen, 2016-01-20
@EugeneP2

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

JSP /home.jsp
<%@ 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>

PS
JSP is also a servlet.

A
Arthur Braver, 2016-01-20
@Zurus

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 question

Ask a Question

731 491 924 answers to any question